Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • JaehyukSong/mogakso_102
1 result
Show changes
Commits on Source (4)
import sys
from collections import deque
input = sys.stdin.readline
dx, dy = [-1, 1, 0, 0],[0, 0, -1, 1]
T = int(input())
for _ in range(T):
M, N, K = map(int, input().split())
graph = [[0] * M for _ in range(N)]
for _ in range(K):
a, b = map(int, input().split())
graph[b][a] = 1
def bfs(py, px):
queue = deque()
queue.append((py, px))
graph[py][px] = 2
while queue:
y, x = queue.popleft()
for i in range(4):
ny , nx = y + dy[i], x + dx[i]
if 0 <= ny < N and 0 <= nx < M and graph[ny][nx] == 1:
graph[ny][nx] = 2
queue.append((ny, nx))
cnt = 0
for y in range(N):
for x in range(M):
if graph[y][x] == 1:
bfs(y, x)
cnt += 1
print(cnt)
import sys
input = sys.stdin.readline
a, b, c = 0, 0, 0
N = int(input())
arr = [[*map(int, input().split())] for _ in range(N)]
def bfs(x, y, n):
global a, b, c
check = arr[x][y]
for i in range(x, x+n):
for j in range(y, y+n):
if (arr[i][j] != check):
for k in range(3):
for l in range(3):
bfs(x + k*n//3, y + l*n//3, n//3)
return
if check == -1:
a += 1
elif check == 0:
b += 1
else:
c += 1
bfs(0, 0, N)
print(f'{a}\n{b}\n{c}')
\ No newline at end of file
#include <iostream>
#include <vector>
using namespace std;
int a = 0;
int b = 0;
vector<int> arr;
void bfs(int x, int y, int n) {
int check = arr[x][y];
for (int i = x; i < x+n; i++) {
for (int j = y; j < y+n; j++) {
if (arr[i][j] != check) {
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
bfs(x+k*(n/2), y+l*(n/2), n/2);
}
}
return;
}
}
}
if (check == 0) a++;
else b++;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
arr.resize(N, vector<int>(N));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> arr[i][j];
}
}
bfs(0, 0, N);
cout << a << "\n" << b << endl;
return 0;
}
\ No newline at end of file
import sys
input = sys.stdin.readline
a, b = 0, 0
N = int(input())
arr = [[*map(int, input().split())] for _ in range(N)]
def bfs(x, y, n):
global a, b
check = arr[x][y]
for i in range(x, x+n):
for j in range(y, y+n):
if (arr[i][j] != check):
for k in range(2):
for l in range(2):
bfs(x + k*n//2, y + l*n//2, n//2)
return
if check == 0:
a += 1
else:
b += 1
bfs(0, 0, N)
print(f'{a}\n{b}')
\ No newline at end of file
import sys
from collections import deque
input = sys.stdin.readline
queue = deque()
cnt = 0
dx, dy, dz = [-1, 1, 0, 0, 0, 0], [0, 0, -1, 1, 0, 0], [0, 0, 0, 0, -1, 1]
M, N, H = map(int, input().split())
graph = [[[*map(int, input().split())] for _ in range(N)] for _ in range(H)]
def bfs():
while queue:
z, x, y = queue.popleft()
for i in range(6):
nx, ny, nz = x+dx[i], y+dy[i], z+dz[i]
if -1 < nx < N and -1 < ny < M and -1 < nz < H and graph[nz][nx][ny] == 0:
graph[nz][nx][ny] = graph[z][x][y] + 1
queue.append((nz, nx, ny))
for i in range(H):
for j in range(N):
for k in range(M):
if graph[i][j][k] == 1:
queue.append((i, j, k))
bfs()
for i in graph:
for j in i:
for k in j:
if k == 0:
print(-1)
exit(0)
cnt = max(cnt, k)
print(cnt-1)
\ No newline at end of file