-
Notifications
You must be signed in to change notification settings - Fork 0
/
2178_미로탐색.cpp
60 lines (55 loc) · 1.2 KB
/
2178_미로탐색.cpp
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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
using namespace std;
int N, M;
int board[101][101];
int cntBoard[101][101];
int visited[101][101];
int dirX[4] = { 1, 0, -1, 0 };
int dirY[4] = { 0, 1, 0, -1 };
vector<pair<int, int>> queue;
int main(void)
{
cin >> N >> M;
for (int i = 1; i < N + 1; i++)
{
string row;
cin >> row;
for (int j = 1; j < M + 1; j++)
board[i][j] = row[j - 1] - '0';
}
for (int i = 0; i < 101; i++)
{
for (int j = 0; j < 101; j++)
{
cntBoard[i][j] = 0;
visited[i][j] = 0;
}
}
visited[1][1] = 1;
queue.push_back({ 1, 1 });
cntBoard[1][1] = 1;
while (!queue.empty())
{
pair<int, int> root;
root = queue.front();
queue.erase(queue.begin());
int row = root.first;
int col = root.second;
for (int i = 0; i < 4; i++)
{
int newX = row + dirX[i];
int newY = col + dirY[i];
if ((newX >= 1 && newX <= N + 1) && (newY >= 1 && newY <= M + 1) && board[newX][newY] == 1 && visited[newX][newY] == 0)
{
visited[newX][newY] = 1;
queue.push_back({ newX, newY });
cntBoard[newX][newY] = cntBoard[row][col] + 1; //new location's count = previous count + 1
}
}
}
cout << cntBoard[N][M];
}