使用?命名空間?std
int?地圖[5][5];
int?視覺[5][5];
struct?節點{
int?x;
int?y;
int?pre
} edge[100];
int?前=0,後= 1;//團隊的頭,團隊的尾
int?dir[4][2]={{0,-1},{1,0},{0,1},{-1,0 } };
作廢?f(int?I)//向後追蹤方法
{
if(edge[i].pre!=-1)
{
f(邊緣[i]。pre);
cout & lt& lt"(" & lt& lt邊緣[i]。x & lt& lt",?"& lt& lt邊緣[i]。y & lt& lt”)" & lt& ltendl
}
}
作廢?BFS(int?x,int?y)
{
邊緣[正面]。x = x
邊緣[正面]。y = y
邊緣[正面]。pre =-1;
while(front & lt;Rear)//隊列為空時終止。
{
int?u;
for(u = 0;u & lt4;u++)
{
int?x =邊緣[正面]。x+dir[u][0];
int?y=edge[front]。y+dir[u][1];
if(x & lt;0 | | x & gt= 5 | | y & lt0 | | y & gt= 5 | | vis[x][y]= = 1 | | map[x][y]= = 1)
繼續;
其他
{
vis[x][y]= 1;
map[x][y]= 1;
邊緣[後部]。x = x//加入團隊
邊緣[後部]。y = y
邊緣[後部]。pre = front
rear++;
}
if(x = = 4 & amp;& ampy==4)
f(正面);
}
front++;//出隊
}
}
int?主()
{
int?I,j;
for(I = 0;我& lt5;i++)
{
for(j = 0;j & lt5;j++)
{
CIN & gt;& gtmap[I][j];
}
}
memset(vis,0,sizeof(vis));
cout & lt& lt"(" & lt& lt"0,?0)" & lt;& ltendl
BFS(0,0);
cout & lt& lt"(4,?4)" & lt;& ltendl
回歸?0;
}