https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14vXUqAGMCFAYD&
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.io.*;
import java.util.*;
class Point {
int y, x;
Point(int y, int x) {
this.y = y;
this.x = x;
}
}
class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int T = 10;
for (int test_case = 1; test_case <= 10; test_case++) {
String trash = br.readLine();
int N = 16;
int[][] arr = new int[N][N];
Point start = null;
Point end = null;
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < N; j++) {
arr[i][j] = line.charAt(j) - '0';
if (arr[i][j] == 2)
start = new Point(i, j);
if (arr[i][j] == 3)
end = new Point(i, j);
}
}
////////////////////////////////////
무난하게 입력받고
Queue<Point> queue = new LinkedList<>();
boolean[][] visited = new boolean[N][N];
boolean success = false;
queue.add(start);
visited[start.y][start.x] = true;
while (!queue.isEmpty()) {
Point tmp = queue.remove();
if (tmp.y == end.y && tmp.x == end.x) {
success = true;
break;
}
// 위로
if (tmp.y - 1 >= 0 && !visited[tmp.y - 1][tmp.x] && arr[tmp.y - 1][tmp.x] != 1) {
visited[tmp.y - 1][tmp.x] = true;
queue.add(new Point(tmp.y - 1, tmp.x));
}
// 아래로
if (tmp.y + 1 < N && !visited[tmp.y + 1][tmp.x] && arr[tmp.y + 1][tmp.x] != 1) {
visited[tmp.y + 1][tmp.x] = true;
queue.add(new Point(tmp.y + 1, tmp.x));
}
// 왼쪽
if (tmp.x - 1 >= 0 && !visited[tmp.y][tmp.x - 1] && arr[tmp.y][tmp.x - 1] != 1) {
visited[tmp.y][tmp.x - 1] = true;
queue.add(new Point(tmp.y, tmp.x - 1));
}
// 오른쪽
if (tmp.x + 1 < N && !visited[tmp.y][tmp.x + 1] && arr[tmp.y][tmp.x + 1] != 1) {
visited[tmp.y][tmp.x + 1] = true;
queue.add(new Point(tmp.y, tmp.x + 1));
}
}
if (success)
sb.append("#").append(test_case).append(" ").append(1).append("\n");
else
sb.append("#").append(test_case).append(" ").append(0).append("\n");
}
System.out.println(sb);
}
}
그냥 무난하게 bfs 너무 쉬