Skip to content

Commit

Permalink
BOJ2178_미로탐색하기 java solution ver2
Browse files Browse the repository at this point in the history
  • Loading branch information
mong3125 committed Mar 10, 2024
1 parent a7215a4 commit 22693d2
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions mong3125/BFS/BOJ2178_미로탐색하기.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class BOJ2178_미로탐색하기 {

static boolean[][] field;
static boolean[][] visited;

static int[] dx = {1, -1, 0, 0};
static int[] dy = {0, 0, 1, -1};
public static void main(String[] args) throws IOException {
// 입력
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Expand Down Expand Up @@ -51,28 +54,15 @@ public static int bfs(int i, int j) {
return now[2];
}

if (isInRange(now[0] - 1, now[1])) {
if (!visited[now[0] - 1][now[1]] && field[now[0] - 1][now[1]]) {
queue.add(new int[]{now[0] - 1, now[1], now[2] + 1});
visited[now[0] - 1][now[1]] = true;
}
}
if (isInRange(now[0], now[1] - 1)) {
if (!visited[now[0]][now[1] - 1] && field[now[0]][now[1] - 1]) {
queue.add(new int[]{now[0], now[1] - 1, now[2] + 1});
visited[now[0]][now[1] - 1] = true;
}
}
if (isInRange(now[0] + 1, now[1])) {
if (!visited[now[0] + 1][now[1]] && field[now[0] + 1][now[1]]) {
queue.add(new int[]{now[0] + 1, now[1], now[2] + 1});
visited[now[0] + 1][now[1]] = true;
}
}
if (isInRange(now[0], now[1] + 1)) {
if (!visited[now[0]][now[1] + 1] && field[now[0]][now[1] + 1]) {
queue.add(new int[]{now[0], now[1] + 1, now[2] + 1});
visited[now[0]][now[1] + 1] = true;
for (int k = 0; k < 4; k++) {
int x = now[0] + dx[k];
int y = now[1] + dy[k];

if (isInRange(x, y)) {
if (!visited[x][y] && field[x][y]) {
queue.add(new int[]{x, y, now[2] + 1});
visited[x][y] = true;
}
}
}
}
Expand Down

0 comments on commit 22693d2

Please sign in to comment.