From 4d5959cd47ca1b819973c13c5bacb3764ea60d24 Mon Sep 17 00:00:00 2001 From: jeeminimini Date: Sat, 16 Mar 2024 22:23:23 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=ED=95=AD=EC=B2=B4=20=EC=9D=B8?= =?UTF-8?q?=EC=8B=9D=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\354\262\264 \354\235\270\354\213\235.py" | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 "src/main/kotlin/jimin/54week/\355\225\255\354\262\264 \354\235\270\354\213\235.py" diff --git "a/src/main/kotlin/jimin/54week/\355\225\255\354\262\264 \354\235\270\354\213\235.py" "b/src/main/kotlin/jimin/54week/\355\225\255\354\262\264 \354\235\270\354\213\235.py" new file mode 100644 index 00000000..6c35b367 --- /dev/null +++ "b/src/main/kotlin/jimin/54week/\355\225\255\354\262\264 \354\235\270\354\213\235.py" @@ -0,0 +1,69 @@ +''' +[항체 인식](https://www.acmicpc.net/problem/22352) + +bfs를 만들어서 before에 맞춰서 after를 같이 탐색한다. +이때 after의 수가 일관되지 않으면 -1을 반환하고, +before와 after가 동일하면 0을 반환하고, +before와 after가 다르면 1을 반환한다. + +-1은 백신 조건을 위배한 것으로 바로 No를 출력하고, +0과 1은 계속 더해주면서 항체가 아예 변하지 않거나 한번만 변할 경우만 YES를 출력한다. +''' + + +import sys +from collections import deque + + +n, m = map(int, sys.stdin.readline().split()) +before = [] +after = [] +visited = [[False for _ in range(m)] for _ in range(n)] + +for i in range(n): + before.append(list(map(int, sys.stdin.readline().split()))) + +for i in range(n): + after.append(list(map(int, sys.stdin.readline().split()))) + + +def bfs(x, y): + queue = deque([[x, y]]) + data = before[x][y] + update = after[x][y] + dx = [0, 0, -1, 1] + dy = [1, -1, 0, 0] + possible = 0 + + if data != update: + possible = 1 + + while queue: + nx, ny = queue.popleft() + visited[nx][ny] = True + if after[nx][ny] != update: + possible = -1 + + for i in range(4): + if 0 <= nx + dx[i] < n and 0 <= ny + dy[i] < m: + if not visited[nx + dx[i]][ny + dy[i]] and before[nx + dx[i]][ny + dy[i]] == data: + queue.append([nx + dx[i], ny + dy[i]]) + + return possible + + +result = 0 +for i in range(n): + for j in range(m): + if not visited[i][j]: + possible = bfs(i, j) + if possible == -1: + print("NO") + sys.exit() + else: + result += bfs(i, j) + +if result <= 1 : + print("YES") +else: + print("NO") \ No newline at end of file