20250816 baekjoon
This commit is contained in:
parent
fe4f540812
commit
e436dddb74
@ -36,6 +36,42 @@ public class _10026 {
|
|||||||
}
|
}
|
||||||
return countArea;
|
return countArea;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int countAreaDFS(String[] grid, int N){
|
||||||
|
int[] dx = {1,-1,0,0};
|
||||||
|
int[] dy = {0,0,1,-1};
|
||||||
|
boolean[][] visited = new boolean[N][N];
|
||||||
|
Stack<Integer> sx = new Stack<>();
|
||||||
|
Stack<Integer> sy = new Stack<>();
|
||||||
|
int countArea = 0;
|
||||||
|
|
||||||
|
for(int x=0; x<N; x++){
|
||||||
|
for(int y=0; y<N; y++){
|
||||||
|
if(!visited[y][x]){
|
||||||
|
visited[y][x] = true;
|
||||||
|
char color = grid[y].charAt(x);
|
||||||
|
sx.push(x);
|
||||||
|
sy.push(y);
|
||||||
|
while(sx.size() != 0 && sy.size() != 0){
|
||||||
|
int cx = sx.pop();
|
||||||
|
int cy = sy.pop();
|
||||||
|
|
||||||
|
for(int d=0; d<4; d++){
|
||||||
|
int nx = cx + dx[d];
|
||||||
|
int ny = cy + dy[d];
|
||||||
|
if(0<=nx && nx<N && 0<=ny && ny<N && grid[ny].charAt(nx) == color && !visited[ny][nx]){
|
||||||
|
sx.add(nx);
|
||||||
|
sy.add(ny);
|
||||||
|
visited[ny][nx] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
countArea += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return countArea;
|
||||||
|
}
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
int N = Integer.parseInt(sc.nextLine());
|
int N = Integer.parseInt(sc.nextLine());
|
||||||
@ -49,7 +85,7 @@ public class _10026 {
|
|||||||
sc.close();
|
sc.close();
|
||||||
|
|
||||||
int areaCount_normal = countAreaBFS(grid_normal, N);
|
int areaCount_normal = countAreaBFS(grid_normal, N);
|
||||||
int areaCount_blindness = countAreaBFS(grid_blindness, N);
|
int areaCount_blindness = countAreaDFS(grid_blindness, N);
|
||||||
|
|
||||||
System.out.println(areaCount_normal + " " + areaCount_blindness);
|
System.out.println(areaCount_normal + " " + areaCount_blindness);
|
||||||
}
|
}
|
||||||
|
|||||||
4
code_study/Baekjoon/ts/2587.ts
Normal file
4
code_study/Baekjoon/ts/2587.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export {};
|
||||||
|
|
||||||
|
const numList: number[] = require("fs").readFileSync(0,"utf8").toString().trim().split('\n').map(Number).sort((a,b)=>a-b);
|
||||||
|
console.log([numList.reduce((avg, v) => avg + v, 0) / 5,numList[2]].join('\n'));
|
||||||
Loading…
x
Reference in New Issue
Block a user