[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});
}
}
}
}
思路:保存路径的迷宫问题。用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});
}
}
}
}
相关文章推荐
- [kuangbin带你飞]专题一 简单搜索K - 迷宫问题(POJ 3984)
- [kuangbin带你飞]专题1 简单搜索 K - 迷宫问题 POJ - 3984
- POJ 3984 迷宫问题(kuangbin带你飞 专题一:简单搜索)
- [kuangbin带你飞]专题一 简单搜索 K - 迷宫问题 POJ 3984
- POJ 3984 & [kuangbin带你飞]专题一 简单搜索 K
- POJ - 3984 迷宫问题 [kuangbin带你飞]专题一 简单搜索
- [kuangbin带你飞]专题一 简单搜索 - K - 迷宫问题
- [kuangbin带你飞]专题一 简单搜索C - Catch That Cow(POJ 3278)
- [kuangbin带你飞]专题一 简单搜索 - C - Catch That Cow
- [kuangbin带你飞]专题一 简单搜索I - Fire Game(FZU 2150)
- POJ 3279 Fliptile(kuangbin带你飞 专题一:简单搜索)
- [kuangbin带你飞]专题一 简单搜索 - B - Dungeon Master
- [kuangbin带你飞]专题一 简单搜索G - Shuffle'm Up(POJ 3087)
- [kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple
- [kuangbin带你飞]专题一 简单搜索 A - 棋盘问题(POJ 1321)
- POJ 3126 Prime Path(kuangbin带你飞 专题一:简单搜索)
- [kuangbin带你飞]专题一 简单搜索E - Find The Multiple(POJ 1426)
- [kuangbin带你飞]专题1——简单搜索——母牛翻转问题
- [kuangbin带你飞]专题一 简单搜索J - Fire!(UVA 11624)
- Fire Game [kuangbin带你飞]专题一 简单搜索