20250803 baekjoon
This commit is contained in:
parent
58468a6841
commit
3ff16c6bff
37
code_study/Baekjoon/python/2667.py
Normal file
37
code_study/Baekjoon/python/2667.py
Normal file
@ -0,0 +1,37 @@
|
||||
import sys
|
||||
from collections import deque
|
||||
|
||||
input = sys.stdin.readline
|
||||
|
||||
def BFS(filedMap, visited, x, y, N):
|
||||
queue = deque([(x,y)])
|
||||
visited[y][x] = True
|
||||
cnt = 1
|
||||
|
||||
dx = [1,-1,0,0]
|
||||
dy = [0,0,1,-1]
|
||||
|
||||
while len(queue):
|
||||
cx, cy = queue.popleft()
|
||||
for i in range(4):
|
||||
nx, ny = cx + dx[i], cy + dy[i]
|
||||
if 0<=nx<N and 0<=ny<N and not visited[ny][nx] and filedMap[ny][nx]==1:
|
||||
queue.append((nx,ny))
|
||||
visited[ny][nx] = True
|
||||
cnt += 1
|
||||
return cnt
|
||||
|
||||
N = int(input())
|
||||
filedMap = []
|
||||
visited = [[False]*N for _ in range(N)]
|
||||
for _ in range(N):
|
||||
filedMap.append(list(map(int,[s for s in input().rstrip()])))
|
||||
|
||||
cntEstate = []
|
||||
for i in range(N):
|
||||
for j in range(N):
|
||||
if not visited[i][j] and filedMap[i][j] > 0 :
|
||||
cntEstate.append(BFS(filedMap, visited, j, i, N))
|
||||
|
||||
result = str(len(cntEstate)) + '\n' + '\n'.join(map(str, sorted(cntEstate)))
|
||||
print(result)
|
||||
38
code_study/Baekjoon/ts/2178.ts
Normal file
38
code_study/Baekjoon/ts/2178.ts
Normal file
@ -0,0 +1,38 @@
|
||||
export {};
|
||||
|
||||
const input: string[] = require("fs").readFileSync(0,"utf8").toString().trim().split('\n');
|
||||
const [N, M]:number[] = input[0].split(' ').map(Number);
|
||||
let Meero:number[][] = Array.from({length: N+1}, () => new Array(M+1).fill(0));
|
||||
let visited:number[][] = Array.from({length: N+1}, () => new Array(M+1).fill(0));
|
||||
let queueX: number[] = []
|
||||
let queueY: number[] = []
|
||||
const dx:number[] = [1,-1,0,0];
|
||||
const dy:number[] = [0,0,1,-1];
|
||||
|
||||
Meero.forEach((_,i,arr) => {
|
||||
if(i!==0) {
|
||||
arr[i] = ('0'+input[i]).split("").map(Number);
|
||||
}
|
||||
});
|
||||
|
||||
queueX.push(1);
|
||||
queueY.push(1);
|
||||
visited[1][1] = 1;
|
||||
|
||||
while(queueX.length!==0){
|
||||
const [cx, cy]:number[] = [queueX.shift()!, queueY.shift()!];
|
||||
if(cx===M && cy===N){
|
||||
console.log(visited[N][M]);
|
||||
break;
|
||||
}
|
||||
|
||||
for(let i=0; i<4; i++){
|
||||
const[nx, ny] = [cx + dx[i], cy + dy[i]];
|
||||
const goPossible: boolean = 0<nx && nx<=M && 0<ny && ny<=N && Meero[ny][nx]===1 && visited[ny][nx]===0;
|
||||
if(goPossible){
|
||||
queueX.push(nx);
|
||||
queueY.push(ny);
|
||||
visited[ny][nx] = visited[cy][cx] + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user