poj 3984 迷宫问题

#include<iostream>
#include
<deque>
using namespace std;
struct node
{
int path[25][2];
int t;
}ans[
30];
int arr[5][5],vis[5][5],pos[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int main()
{
for(int i=0;i<5;++i)
for(int j=0;j<5;++j)
cin
>>arr[i][j];
deque
<node> col;
int m=1;
ans[
0].path[0][0]=0;ans[0].path[0][1]=0;ans[0].t=0;
col.push_back(ans[
0]);
vis[
0][0]=1;
while(!col.empty())
{
node temp
=col.front();
col.pop_front();
int x=temp.path[temp.t][0],y=temp.path[temp.t][1],tx,ty;
for(int i=0;i<4;++i)
{
tx
=x+pos[i][0];ty=y+pos[i][1];
if(tx==4&&ty==4)
{
for(int k=0;k<=temp.t;++k)
printf(
"(%d, %d)\n",temp.path[k][0],temp.path[k][1]);
printf(
"(4, 4)\n");
return 0;
}
if(tx>=0&&tx<5&&y+ty>=0&&ty<5&&!arr[tx][ty]&&!vis[tx][ty])
{
vis[tx][ty]
=1;
for(int j=0;j<=temp.t;++j)
ans[m].path[j][
0]=temp.path[j][0],ans[m].path[j][1]=temp.path[j][1];
ans[m].t
=temp.t+1;
ans[m].path[ans[m].t][
0]=tx;ans[m].path[ans[m].t][1]=ty;
col.push_back(ans[m
++]);
}
}
}
}

  

posted on 2011-07-22 20:20  sysu_mjc  阅读(118)  评论(0编辑  收藏  举报

导航