ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 1236 ์„ฑ ์ง€ํ‚ค๊ธฐ with Python
    PS 2022. 2. 10. 17:54
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 1236 ์„ฑ ์ง€ํ‚ค๊ธฐ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์„ฑ์˜ 1์ธต์€ ๋ช‡ ๋ช…์˜ ๊ฒฝ๋น„์›์— ์˜ํ•ด์„œ ๋ณดํ˜ธ๋˜๊ณ  ์žˆ๋‹ค.
      ์˜์‹์ด๋Š” ๋ชจ๋“  ํ–‰๊ณผ ๋ชจ๋“  ์—ด์— ํ•œ ๋ช… ์ด์ƒ์˜ ๊ฒฝ๋น„์›์ด ์žˆ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

    2. ์„ฑ์˜ ํฌ๊ธฐ์™€ ๊ฒฝ๋น„์›์ด ์–ด๋””์žˆ๋Š”์ง€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ช‡ ๋ช…์˜ ๊ฒฝ๋น„์›์„ ์ตœ์†Œ๋กœ ์ถ”๊ฐ€ํ•ด์•ผ ์˜์‹์ด๋ฅผ ๋งŒ์กฑ์‹œํ‚ค๋Š”์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

    3. 0 <= N, M <= 50

    4. ์„ฑ์˜ ์ƒํƒœ๋Š” .์€ ๋นˆ์นธ, X๋Š” ๊ฒฝ๋น„์›์ด ์žˆ๋Š” ์นธ์ด๋‹ค.

    5. ๊ตฌํ˜„, ์‹œ๋ฎฌ๋ ˆ์ด์…˜์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    
    n, m = map(int, stdin.readline().split())
    arr = []
    col = [False] * m
    row = [False] * n
    r1, r2 = 0, 0
    
    for i in range(n):
        data = list(stdin.readline().rstrip())
        arr.append(data)
    
    for i in range(n):
        for j in range(m):
            if arr[i][j] == 'X':
                row[i] = True
                col[j] = True
    
    for i in range(n):
        if not row[i]:
            r1 += 1
    
    for j in range(m):
        if not col[j]:
            r2 += 1
    
    print(max(r1, r2))

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    4 4
    ....
    ....
    ....
    ....

    ์‹คํ–‰๊ฒฐ๊ณผ

    4

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ๊ฐ ์—ด๊ณผ ํ–‰์— ๋ชจ๋‘ ๊ฒฝ๋น„์›์ด ์žˆ์—ˆ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐ์„ ํ–ˆ์œผ๋‹ˆ, ์—ด๊ณผ ํ–‰์— ๊ฒฝ๋น„์›์ด ์žˆ๋Š”์ง€ ์ฒดํฌํ•  col, row ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
      ์ด ๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ ์›์†Œ๋ฅผ False ๋กœ ์ดˆ๊ธฐํ™” ํ•œ ์ƒํƒœ๋กœ ์ƒ์„ฑํ•œ๋‹ค.

    2. ๊ฒฝ๋น„์›์˜ ๊ฐ’์„ ์ €์žฅํ•  r1, r2 ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    3. ์„ฑ์˜ ํฌ๊ธฐ๋งŒํผ ์ˆœํšŒํ•˜๋ฉด์„œ (i, j) ์— X ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, row[i], col[j] ๋ฅผ ๊ฐ๊ฐ True๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.

    4. ๊ฐ ์„ฑ์˜ ๊ฐ€๋กœ ํฌ๊ธฐ(i -> n), ์„ธ๋กœ ํฌ๊ธฐ(j -> m)๋งŒํผ ์ˆœํšŒํ•˜๋ฉด์„œ row[i], col[j] ๊ฐ€ ๊ฐ๊ฐ False ์ผ ๊ฒฝ์šฐ
      r1, r2 ์— ๊ฐ๊ฐ + 1์„ ๋”ํ•ด์ค€๋‹ค.

    5. r1, r2 ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. ๊ตฌํ˜„ ๋ฌธ์ œ์ด์ง€๋งŒ, ์–ด๋””์— ๊ฒฝ๋น„์›์ด ์žˆ์–ด์•ผ ๊ฐ ์—ด๊ณผ ํ–‰์ด ๋ชจ๋‘ True ๊ฐ’์„ ๊ฐ€์ง€๋Š”์ง€ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด ํ—ท๊ฐˆ๋ ธ๋‹ค.
    2. ๋ธŒ๋ก ์ฆˆ๋ผ๊ณ  ํ–ˆ์ง€๋งŒ, ๊ตฌํ˜„์€ ์—ญ์‹œ ๊ทธ ์ด์ƒ์˜ ํ‹ฐ์–ด๋กœ ๋ด์•ผํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
    3. ๋…ผ๋ฆฌ์ ์ธ ์‚ฌ๊ณ ๋ ฅ์ด ์•ฝํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋Š๋ผ๊ฒŒ ํ•ด์ฃผ๋Š” ๋ฌธ์ œ์˜€๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.