VioletaBabel
2178번: 미로 탐색 본문
#include<cstdio>
#include<queue>
using namespace std;
char map[100][101];
bool visit[100][100];
queue<int> _x, _y, _l;
void bfs(int n, int m);
int main()
{
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; ++i)
scanf("%s", &map[i]);
bfs(n, m);
}
void bfs(int n, int m)
{
int x, y, l;
_x.push(0);
_y.push(0);
_l.push(1);
for (;!_x.empty();)
{
x = _x.front();
y = _y.front();
l = _l.front();
_x.pop();
_y.pop();
_l.pop();
if (visit[y][x]++)
continue;
if (y == (n - 1))
if (x == (m - 1))
break;
if (y > 0)
if(!visit[y-1][x])
if (map[y - 1][x] == '1')
{
_x.push(x);
_y.push(y - 1);
_l.push(l + 1);
}
if(y < n)
if(!visit[y+1][x])
if (map[y + 1][x] == '1')
{
_x.push(x);
_y.push(y + 1);
_l.push(l + 1);
}
if(x > 0)
if(!visit[y][x-1])
if (map[y][x - 1] == '1')
{
_x.push(x - 1);
_y.push(y);
_l.push(l + 1);
}
if(x<m)
if(!visit[y][x+1])
if (map[y][x + 1] == '1')
{
_x.push(x + 1);
_y.push(y);
_l.push(l + 1);
}
}
printf("%d", l);
}
'백준 > 백준-C++' 카테고리의 다른 글
| 2965번: 캥거루 세마리 (0) | 2017.06.10 |
|---|---|
| 7576번: 토마토 (0) | 2017.06.09 |
| 2667번: 단지번호붙이기 (0) | 2017.06.09 |
| 1260번: DFS와 BFS (0) | 2017.06.09 |
| 2156번: 포도주 시식 (실패) (0) | 2017.06.07 |