백준/백준-C++

2178번: 미로 탐색

Beabletoet 2017. 6. 9. 13:22

#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);

}