水水
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int N = 5;
struct Point
{
int x, y;
int last;
Point(int x=0, int y=0,int last=-1)
{
this->x = x;
this->y = y;
this->last = last;
}
};
const int ROW[] = {1, -1, 0, 0};
const int COL[] = {0, 0, 1, -1};
bool in_maze(const Point next)
{
return (next.x < N && next.x >= 0 && next.y < N && next.y >= 0 );
}
void _print(const Point q[], const Point cur)
{
if (cur.last != -1)_print(q, q[cur.last]);
printf("(%d, %d)\n", cur.x, cur.y);
}
int main()
{
bool maze[N][N];
// read maze
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
scanf("%d", &maze[i][j]);
// bfs
bool visit[N][N];
memset(visit, 0, sizeof(visit));
Point q[N*N+2];
int head = 1;
int tail = 2;
while (head < tail)
{
Point cur = q[head];
for (int i = 0; i < 4; i++)
{
Point next(cur.x+ROW[i], cur.y+COL[i], head);
if (next.x == 4 && next.y == 4)
{
_print(q, next);
return 0;
}
else if (in_maze(next) && !maze[next.x][next.y] && !visit[next.x][next.y])
{
visit[next.x][next.y] = true;
q[tail++] = next;
}
}
head++;
}
}