您的位置:首页 > 其它

[kuangbin带你飞]专题一 简单搜索 K POJ 3984

2016-09-19 21:44 597 查看
题目地址:https://vjudge.net/contest/65959#problem/K

思路:保存路径的迷宫问题。用pre数组保存上一位置。

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;
int map[5][5];
bool vis[5][5];
int xx[4]={-1,0,1,0};
int yy[4]={0,-1,0,1};
struct point{
int x,y;
};
point pre[5][5];
int main()
{
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
{
scanf("%d",&map[i][j]);
}
memset(vis,false,sizeof(vis));
queue<point>q;
q.push(point{0,0});
while(!q.empty())
{
point now=q.front();
q.pop();
int x=now.x;
int y=now.y;
if(x==4 && y==4)
{
int i=4,j=4;
stack<point>k;
while(i!=0 || j!=0)
{
k.push(point{i,j});
int ii=pre[i][j].x;
int jj=pre[i][j].y;
i=ii;
j=jj;
}
k.push(point{0,0});
while(!k.empty())
{
point now=k.top();
k.pop();
printf("(%d, %d)\n",now.x,now.y);
}
break;
}
for(int i=0;i<4;i++)
{
int tempx=x+xx[i];
int tempy=y+yy[i];
if(tempx>0 &&tempx<5 &&tempy>=0 &&tempy<5 && !vis[tempx][tempy] && !map[tempx][tempy])
{
pre[tempx][tempy].x=x;
pre[tempx][tempy].y=y;
vis[tempx][tempy]=true;
q.push(point{tempx,tempy});
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: