入门题:http://poj.org/problem?id=3984
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define N 5
int a[4][2]={{-1,0},{1,0},{0,-1},{0,1}},map[N][N],vist[N][N];
typedef struct
{
int x,y;
}node;
struct save
{
int x,y;
}pre[N][N];
queue< node > que;
node s,e;
void ouput( )
{
node t,tx;
printf("(%d, %d)\n",s.x,s.y);
tx.x=s.x,tx.y=s.y;
while(tx.x != e.x || tx.y != e.y )
{
t.x = pre[tx.x][tx.y].x;
t.y = pre[tx.x][tx.y].y;
printf("(%d, %d)\n",t.x,t.y);
tx.x = t.x;
tx.y = t.y;
}
}
void bfs()
{
int i;
node tmp,cur;
while( !que.empty() ) que.pop();
que.push(e);
while( !que.empty() )
{
tmp = que.front();
que.pop();
for(i=0;i<4;i++)
{
cur.x = tmp.x + a[i][0];
cur.y = tmp.y + a[i][1];
if( !vist[ cur.x ][ cur.y ] && map[cur.x][cur.y] == 0 && cur.x>=0 && cur.x<N && cur.y>=0 && cur.y<N )
{
vist[ cur.x ][ cur.y ] = 1;
pre[cur.x][cur.y].x = tmp.x;
pre[cur.x][cur.y].y = tmp.y;
que.push(cur);
if(cur.x == s.x && cur.y == s.y )
{
ouput();
return ;
}
}
}
}
return ;
}
int main()
{
int i,j;
char xishou;
// freopen("a.txt","r",stdin);
for(i=0;i<N;i++)
for(j=0;j<N;j++)
scanf("%d%c",&map[i][j],&xishou);
memset(vist,0,sizeof(vist));
s.x = s.y = 0 ;
e.x = e.y = N-1;
vist[e.x][e.y]=1;
bfs();
return 0;
}