티스토리 뷰
[소스 코드]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | import java.beans.Visibility; import java.io.*; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Solution { public static int tCase, N, ans, click, mine, noClick; public static char[][] map; public static boolean[][] visited; public static int[] dirX = new int[] { 0, 0, -1, -1, -1, 1, 1, 1 }; public static int[] dirY = new int[] { -1, 1, -1, 0, 1, -1, 0, 1 }; public static Queue<Node> q = new LinkedList<Node>(); public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); tCase = Integer.parseInt(br.readLine()); for (int t = 1; t <= tCase; t++) { N = Integer.parseInt(br.readLine()); map = new char[N][N]; visited = new boolean[N][N]; ans = click = mine = noClick = 0; for (int i = 0; i < N; i++) { String str = br.readLine(); for (int j = 0; j < N; j++) { map[i][j] = str.charAt(j); if (map[i][j] == '*') mine += 1; } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (map[i][j] != '*' && isPossible(i, j) && !visited[i][j]) { solve(i, j); } } } noClick = countNoCick(); System.out.println("#" + t + " " + (click + noClick)); } } public static int countNoCick() { int count = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (!visited[i][j] && map[i][j] != '*') count += 1; } } return count; } public static boolean isPossible(int row, int col) { for (int i = 0; i < 8; i++) { int nr = row + dirX[i]; int nc = col + dirY[i]; if (!isBoundary(nr, nc)) continue; if (map[nr][nc] != '.') return false; } return true; } public static void solve(int row, int col) { q.clear(); q.offer(new Node(row, col)); click += 1; while (!q.isEmpty()) { Node node = q.poll(); visited[node.row][node.col] = true; for (int i = 0; i < 8; i++) { int nr = node.row + dirX[i]; int nc = node.col + dirY[i]; if (isBoundary(nr, nc) && !visited[nr][nc]) { visited[nr][nc] = true; if (isPossible(nr, nc)) q.offer(new Node(nr, nc)); } } } } public static boolean isBoundary(int row, int col) { return (row >= 0 && row < N) && (col >= 0 && col < N); } } class Node { int row; int col; public Node(int row, int col) { super(); this.row = row; this.col = col; } } | cs |
'알고리즘 문제 > SW Expert Academy' 카테고리의 다른 글
[SWEA 1953번] 탈주범 검거 :: 늦깎이 IT (0) | 2019.03.13 |
---|---|
[SWEA 4012번] 요리사 :: 늦깎이 IT (0) | 2019.03.12 |
[SWEA 4008번] 숫자 만들기 :: 늦깎이 IT (0) | 2019.03.12 |
[SWEA 5658번] 보물상자 비밀번호 :: 늦깎이 IT (0) | 2019.02.18 |
[SWEA 5656번] 벽돌 깨기 :: 늦깎이 IT (0) | 2019.02.18 |
댓글