• 문제 링크
11448번: Ga
The Dutch version of the game “Go” is called “Ga”. It is played on an N × N board; the N2 squares are initially empty. Two players, White and Black, take turns; White begins. During a move, the player places one or more stones of his own colour (o
www.acmicpc.net
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.Queue;
public class Main {
static int n, cnt;
static char[][] mat;
static int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(br.readLine().trim());
while (t-- > 0) {
n = Integer.parseInt(br.readLine().trim());
cnt = 0;
mat = new char[n][n];
for (int i = 0; i < n; i++) mat[i] = br.readLine().trim().toCharArray();
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (mat[i][j] == 'w') bfs(new Pair(i, j));
sb.append(cnt).append("\n");
}
bw.write(sb.toString());
bw.flush();
}
private static void bfs(Pair src) {
Queue<Pair> q = new ArrayDeque<>();
q.offer(src);
while (!q.isEmpty()) {
Pair cur = q.poll();
for (int[] d : dir) {
int ny = cur.y + d[0], nx = cur.x + d[1];
if (ny < 0 || ny >= n || nx < 0 || nx >= n || mat[ny][nx] != '-') continue;
q.offer(new Pair(ny, nx));
mat[ny][nx] = 'w';
cnt++;
}
}
}
private static class Pair {
int y, x;
Pair(int y, int x) {
this.y = y;
this.x = x;
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 6125 Treasure Cave - Graph Theory / Java (0) | 2023.11.17 |
---|---|
[백준] 11322 Numbers are Easy - Graph Theory / Java (0) | 2023.11.16 |
[백준] 6004 The Chivalrous Cow - Graph Theory / Java (0) | 2023.11.14 |
[백준] 26876 New Time - Graph Theory / Java (0) | 2023.11.13 |
[백준] 6798 Knight Hop - Graph Theory / Java (0) | 2023.11.12 |
댓글