Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9-mong3125 #34

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class BOJ2178_๋ฏธ๋กœํƒ์ƒ‰ํ•˜๊ธฐ {

static int n;
static int m;

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));
StringTokenizer st = new StringTokenizer(br.readLine());

n = Integer.parseInt(st.nextToken()); // ์„ธ๋กœ ํฌ๊ธฐ
m = Integer.parseInt(st.nextToken()); // ๊ฐ€๋กœ ํฌ๊ธฐ

// ๋ฏธ๋กœ ์„ ์–ธ
field = new boolean[n][m];
visited = new boolean[n][m];

// ๋ฏธ๋กœ ์ž…๋ ฅ
for (int i = 0; i < n; i++) {
String line = br.readLine();
for (int j = 0; j < m; j++) {
field[i][j] = line.charAt(j) == '1';
}
}

// ๋ฌธ์ œ ํ•ด๊ฒฐ
int answer = bfs(0, 0);

// ์ถœ๋ ฅ
System.out.println(answer);
}

public static int bfs(int i, int j) {
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{i, j, 1});
visited[i][j] = true;

while (!queue.isEmpty()) {
int[] now = queue.poll();

if (now[0] == n - 1 && now[1] == m - 1) {
return now[2];
Comment on lines +53 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now[0]๊ณผ now[1]์˜ ์œ„์น˜๊ฐ€ (N,M) ์ผ ๋•Œ ์ด๋™ ํšŸ์ˆ˜ now[2] ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ  ํ•ด์„๋˜๋„ค์š”.

}

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;
}
}
}
}

return 0;
}

public static boolean isInRange(int i, int j) {
return 0 <= i && i < n && 0 <= j && j < m;
}
}