刚才碰到一个搜索题,写错了,好伤心,不过那题下边就是这个,刷个水题
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
struct node
{
int x,y;
int par;
};
int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int G[10][10];
node que[500];
void Print(int index)
{
if(que[index].par == -1)
{
cout << "(" << que[index].x << ", " << que[index].y << ")" << endl;
return;
}
Print(que[index].par);
cout << "(" << que[index].x << ", " << que[index].y << ")" << endl;
}
int main()
{
for(int i = 0; i < 5; ++i)
for(int j = 0; j < 5; ++j)
cin >> G[i][j];
int head = 0,tail = 0;
que[tail].x = que[tail].y = 0;
G[0][0] = 1;
que[tail].par = -1;
++tail;
int tx,ty;
node cur;
while(head < tail)
{
node cur = que[head++];
if(cur.x == 4 && cur.y == 4)
break;
for(int k = 0; k < 4; ++k)
{
tx = cur.x + next[k][0];
ty = cur.y + next[k][1];
if(tx < 0 || ty < 0 || tx > 4 || ty > 4)
continue;
if(!G[tx][ty])
{
G[tx][ty] = 1;
que[tail].x = tx;
que[tail].y = ty;
que[tail].par = head-1;
++tail;
}
}
}
Print(head-1);
return 0;
}