-
Notifications
You must be signed in to change notification settings - Fork 0
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
12-wnsmir #51
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฏธ๋ก ์ฐพ๊ธฐ ์ค๋๋ง์ ํด๋ด์ ์ฌ๋ฐ๋ค์! ์ ๋ queue๋ก ๊ตฌํํ์ต๋๋ค.
๊ฝค ๋นจ๋ฆฌ ์์ฑํ์๋๋ฐ... ์คํ์ด์ค๋ก ๊ตฌ๋ถ๋์ง ์๋ ์
๋ ฅ์ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ๋ฅผํด์...
cin
์ด ์ธ์์ ๋ชปํด์ ์ถ๋ ฅ์ด ์๋๋ ๊ฒ๋๋ค ใ
ใ
ใ
ใ
ใ
์ ๋ ์ ๋ก์ง์ด ์๋ชป๋์ ๋ฌดํ๋ฃจํ์ธ ์ค ์๊ณ ํ์ฐธ ๊ณ ๋ฏผํ์์ต๋๋ค.
(๊ทธ๋ฆฌ๊ณ ๋ฏธ๋ก๋ฅผ ํ์ถํ ์ ์๋ ๊ฒฝ์ฐ ๋ฑ์ ๋ฌด์ํ์ต๋๋ค!)
CPP CODE
#include <iostream>
#include <queue>
#include <vector>
#define WALL 0
#define PATH 1
#define VISITED 2
using namespace std;
int N, M;
int offset[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
bool isMoveable(vector<vector<int>>& maze, int x, int y) {
return 0 <= x && x < M && 0 <= y && y < N && maze[y][x] == PATH;
}
int main() {
cin >> N >> M;
vector<vector<int>> maze(N, vector<int>(M));
for (int y = 0; y < N; y++) {
for (int x = 0; x < M; x++) {
cin >> maze[y][x];
}
}
queue<pair<int, int>> q;
q.push(make_pair(0, 0));
maze[0][0] = VISITED;
int minDistance = 1;
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
int y = q.front().first;
int x = q.front().second;
q.pop();
if (x == M - 1 && y == N - 1) {
cout << minDistance;
return 0;
}
for (int dir = 0; dir < 4; dir++) {
int y_ = y + offset[dir][0];
int x_ = x + offset[dir][1];
if (isMoveable(maze, x_, y_)) {
q.push(make_pair(y_, x_));
maze[y_][x_] = VISITED;
}
}
}
minDistance++;
}
cout << -1;
return 0;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ค๋๋ง์ ๋ฏธ๋ก์ฐพ๊ธฐ ๋ฌธ์ ์ ํ์์ต๋๋ค. ๋ฏธ๋ก์ฐพ๊ธฐ๋งํผ ์๋ฃ๊ตฌ์กฐ์ ์๊ณ ๋ฆฌ์ฆ ์คํฌ์ ์ ์ ํ ์ฌ์ฉํ ์ ์๋ ๋ฌธ์ ์ ํ์ ๋ ์๋๊ฒ ๊ฐ๋ค์.
๋จ๋ค์ด bfs๋ก ํ๊ฑฐ ๊ฐ์ dfs๋ก ๊ตฌํํด๋ณด์์ต๋๋ค. ์ต๋จ ๊ธธ์ด๋ฅผ ๊ตฌํ ๋์ dfs๊ฐ ํจ์จ์ ์ด์ง ์์ผ๋ ๋ญ ์ง๊ธ์ ๊ทธ๋ฐ๊ฑด ์๋ฏธ๊ฐ ์๋ ๊ฒ ๊ฐ์์ dfs๋ฅผ ์ ํํ์ต๋๋ค.
๊ทผ๋ฐ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ์ ๋ต์ด ๋์ค์ง ์์์ ํ์ฐธ์ ๊ณ ๋ฏผํ๋ค ๊ฐ๋๋์ฒ๋ผ ์ ๋ ฅ ๋ฌธ์ ์์์ ํ์ธํ์ต๋๋ค..
bool canMove(int x, int y) {
return 0 <= x && x < m && 0 <= y && y < n
&& map[y][x] == 1;
}
void dfs(int x, int y, int cnt) {
if (x == m - 1 && y == n - 1) {
minimum = min(minimum, cnt);
return;
}
for (auto &dir: offset) {
int nx = x + dir[0];
int ny = y + dir[1];
if (canMove(nx, ny)) {
map[ny][nx] = 2;
dfs(nx, ny, cnt + 1);
map[ny][nx] = 1;
}
}
}
์ ๋ BFS๋ก ํ์ด๋ณด์์ต๋๋ค. java codeimport java.util.*;
public class Main {
static int n, m;
static int[][] map;
static int[][] distance;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
map = new int[n][m];
distance = new int[n][m];
sc.nextLine();
for (int i = 0; i < n; i++) {
String line = sc.nextLine();
for (int j = 0; j < m; j++) {
map[i][j] = line.charAt(j) - '0';
distance[i][j] = -1;
}
}
bfs(0, 0);
System.out.println(distance[n-1][m-1]);
}
public static void bfs(int sx, int sy) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{sx, sy});
distance[sx][sy] = 1;
while (!queue.isEmpty()) {
int[] current = queue.poll();
int x = current[0];
int y = current[1];
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] == 1 && distance[nx][ny] == -1) {
distance[nx][ny] = distance[x][y] + 1;
queue.offer(new int[]{nx, ny});
}
}
}
}
} |
๐ ๋ฌธ์ ๋งํฌ
์ด๊ฒ์ด ์ฝ๋ฉํ ์คํธ๋ค p152์์ ๋ฐ์ทํ์์ต๋๋ค.
๋ฌธ์ : ๋ฏธ๋กํ์ถ
N*M์ map์ด ์๋์ ์์์ฒ๋ผ ์ฃผ์ด์ง๊ณ , 1์ ์ง๋๋ค๋ ์ ์๋๊ธธ, 0์ ๋ฒฝ์ ๋๋ค. ์ค๋ฅธ์ชฝ ํ๋จ ๋์ผ๋ก ๊ฐ๊ธฐ๊น์ง ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ์์ค.
์ ๋ ฅ์์
5 6
101010
111111
000001
111111
111111
์ถ๋ ฅ์์
10
โ๏ธ ์์๋ ์๊ฐ
40min
โจ ์๋ ์ฝ๋
N M์ split์ผ๋ก ๋ฐ์์ค๋๋ค. (๋์์ฐ๊ธฐ๋ก ๊ตฌ๋ถ)
์ดํ ๊ทธ๋ํ๋ ๋์์ฐ๊ธฐ๋ก ๊ตฌ๋ถ๋์ด์์ง ์๊ธฐ ๋ผ๋ฌธ์ ๊ทธ๋ฅ input์ผ๋ก ๋ฐ์์ค๋๋ค.
dx dy๋ฅผ ๋ง๋ค์ด์ฃผ๋๋ฐ, for๋ฌธ์ผ๋ก i๋ฅผ 4๋ฒ ๋๋ ค๊ฐ๋ฉฐ ์ํ์ข์ฐ๋ฅผ ์คํํ๊ธฐ์ํจ ์ ๋๋ค.
์ ๋ ์ฃผ๋ก ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํฉ๋๋ค.
์ดํ BFS๋ฅผ ํ์ฉํ์ฌ ๋ฒฝ์ด๊ฑฐ๋ ์งํ์ ๋์ด๊ฐ๋ ์ขํ๋ผ๋ฉด pass
์ํ์ข์ฐ๋ก ์ด๋ํ ๊ธธ์ด 1์ด๋ผ๋ฉด ์ง๋๊ฐ ์ ์๋ ๊ธธ์ด๋ผ ํ๋จํ๊ณ ์ด์ ๊ธธ์ ๊ฐ์ 1์ ๋ํ ๊ฐ์ผ๋ก ์ ๋ฐ์ดํธ ํฉ๋๋ค.
bfs๊ฐ ํ๋จ๊ณ์ฉ ๊ธธ์ ๋ํ๊ฐ๊ธฐ ๋๋ฌธ์ ์ต๋จ๊ฑฐ๋ฆฌ์ ์ ์ฉํ๋ค ํ๋จ๋์ด bfs๋ฅผ ์ฌ์ฉํ์์ต๋๋ค. (๊ฐ์ค์น๋ 0)