티스토리 뷰

반응형


#. Problem

* The copyright in this matter is in BOJ



#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Solve

푸는 방법은 정말 많이 있는 것 같다


1. 양 주변에 울타리 치기

2. 늑대 주변에 울타리 치기

3 입력받을 때 빈칸을 다 울타리로 쳐버리기


등등..


#. Code

ㅇ늑대 주변에 울타리 쳐버리기!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
dx, dy = [0-101], [10-10]
 
def Solution():
    for x in range(R):
        for y in range(C):
            if map[x][y] == 'W':
                for i in range(4):
                    xx, yy = x + dx[i], y + dy[i]
 
                    if xx < 0 or yy < 0 or xx >= R or yy >= C:
                        continue
 
                    if map[xx][yy] == 'S':
                        print('0')
                        return
                    else:
                        if map[xx][yy] == '.':
                            map[xx][yy] = 'D'
 
    print(1)
    for r in range(R):
         print(''.join(map[r]))
    return
 
R, C = map(int, input().split())
map = [list(input()) for _ in range(R)]
 
Solution()
cs


ㅇ입력받을 때 빈칸을 다 울타리로 쳐버리는 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
dx, dy = [0-101], [10-10]
 
def Solution():
    for x in range(R):
        for y in range(C):
            if map[x][y] == 'W':
                for i in range(4):
                    xx, yy = x + dx[i], y + dy[i]
 
                    if xx < 0 or yy < 0 or xx >= R or yy >= C:
                        continue
 
                    if map[xx][yy] == 'S':
                        print('0')
                        return
 
    print(1)
    for r in range(R):
         print(''.join(map[r]))
    return
 
R, C = map(int, input().split())
map = [input().replace('.''D'for _ in range(R)]
 
Solution()
cs


C++로도 시도해봤는데 역시 C++에서 문자열 처리는 정말.. 최악이다ㅋㅋㅋ

python에서 문자열하다가 C++에서 하려니 참..


line 23) map을 입력받을 때 '.'는 울타리로 다 선치환을 해준다.

line 3~20)

line 6~8) 좌표에 늑대가 있을 경우, 늑대의 상하좌우를 탐색

line 10) 탐색 시 좌표를 벗어날 경우 continue

line 13~15) 늑대 주변에 양이 있을 경우 '0'출력 후 return 

line 17~20) 늑대와 양이 떨어져 있을 경우 결과를 출력

     join을 사용하여 반복문 하나로도 이차원 배열 출력이 가능


#. Other Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
R, C = map(int, input().split())
map = [list(input()) for _ in range(R)]
 
dx, dy = [010-1], [10-10]
 
ck = False
 
for i in range(R):
    for j in range(C):
        if map[i][j] == 'W':
            for w in range(4):
                ii, jj = i + dx[w], j + dy[w]
                if ii < 0 or ii == R or jj < 0 or jj == C: continue
                if map[ii][jj] == 'S': ck = True
 
if ck: print(0)
else:
    print(1)
    for i in range(R):
        for j in range(C):
            if map[i][j] not in 'SW':
                map[i][j] = 'D'
    for i in map:
        print(''.join(i))
cs


line 10~14) 마찬가지로 좌표에 늑대가 있을 시 늑대 상하좌우를 탐색

line 13) 좌표를 벗어날 경우 continue

line 14) 늑대 주변에 양이 있을 경우 ck 변수에 체크

line 16) 늑대 주변에 양이 있었을 경우 '0'출력

line 19~22) 'S' or 'W'를 제외하고 'D'로 치환

line 23~24) 출력

반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday