HDU 2102 A计划(三维BFS)
2017-01-13 12:23
351 查看
1.略有难度的三维BFS,存储图的数组需要三维
2.需要考虑多种极端情况,比如两层的对应位置为‘#’或者传送门后就是终点的情况
3.下面附上代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
int row,col,time,num,ex,ey,ef;
string s1[15],s2[15];
char Graph[15][15][2];
int vis[15][15][2];
int moves1[4]={ 0, 0, 1,-1};
int moves2[4]={-1, 1, 0, 0};
struct node{
int x,y,floor,step;
};
void init()
{
memset(vis,0,sizeof(vis));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
Graph[i+1][j+1][0]=s1[i].at(j);
if(Graph[i+1][j+1][0]=='P'){
ex=i+1;
ey=j+1;
ef=0;
}
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
Graph[i+1][j+1][1]=s2[i].at(j);
if(Graph[i+1][j+1][1]=='P'){
ex=i+1;
ey=j+1;
ef=1;
}
}
}//初始化 找到终点的位置
for(int i=0;i<=row+1;i++)
{
Graph[i][0][0]='*';
Graph[i][0][1]='*';
Graph[i][col+1][0]='*';
Graph[i][col+1][1]='*';
}
for(int i=0;i<=col+1;i++)
{
Graph[0][i][0]='*';
Graph[0][i][1]='*';
Graph[row+1][i][0]='*';
Graph[row+1][i][1]='*';
}//初始化,将图的四周加上边界 避免出界的判断
}
int bfs(int endx,int endy,int endfloor)
{
queue<node> List;
node start;
start.x=start.y=1;
start.floor=start.step=0;
List.push(start);
int now=start.step;
while(!List.empty())
{
node pos=List.front();
List.pop();
now=pos.step;
if(pos.x==endx&&pos.y==endy&&pos.floor==endfloor){
return now;//在队头若找到终点 返回到达所需的最短时间
}
int xx=pos.x,yy=pos.y,ss=pos.floor;
for(int i=0;i<4;i++)
{
if(Graph[moves1[i]+xx][moves2[i]+yy][ss]=='#'
&&vis[moves1[i]+xx][moves2[i]+yy][ss]==0)
{
if((Graph[moves1[i]+xx][moves2[i]+yy][(ss+1)%2]=='.'
||Graph[moves1[i]+xx][moves2[i]+yy][(ss+1)%2]=='P')
&&vis[moves1[i]+xx][moves2[i]+yy][(ss+1)%2]==0)
{
vis[moves1[i]+xx][moves2[i]+yy][ss]=1;
vis[moves1[i]+xx][moves2[i]+yy][(ss+1)%2]=1;
node newnode;
newnode.x=moves1[i]+xx;
newnode.y=moves2[i]+yy;
newnode.floor=(ss+1)%2;
newnode.step=now+1;
List.push(newnode);
}
}
else if(Graph[moves1[i]+xx][moves2[i]+yy][ss]=='.'
&&vis[moves1[i]+xx][moves2[i]+yy][ss]==0)
{
vis[moves1[i]+xx][moves2[i]+yy][ss]=1;
node newnode;
newnode.x=moves1[i]+xx;
newnode.y=moves2[i]+yy;
newnode.step=now+1;
newnode.floor=ss;
List.push(newnode);
}
else if(Graph[moves1[i]+xx][moves2[i]+yy][ss]=='P'
&&vis[moves1[i]+xx][moves2[i]+yy][ss]==0)
{
vis[moves1[i]+xx][moves2[i]+yy][ss]=1;
node newnode;
newnode.x=moves1[i]+xx;
newnode.y=moves2[i]+yy;
newnode.step=now+1;
newnode.floor=ss;
List.push(newnode);
}
}
}
return -1;//如果没有找到 返回-1
}
int main()
{
scanf("%d",&num);
while(num--)
{
memset(vis,0,sizeof(vis));
scanf("%d%d%d",&row,&col,&time);
for(int i=0;i<row;i++){
cin>>s1[i];
}
for(int i=0;i<row;i++){
cin>>s2[i];
}
init();
int ans=bfs(ex,ey,ef);
if(ans<=time&&ans>0)//如果可以达到终点并且时间条件允许,则输出yes
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
2.需要考虑多种极端情况,比如两层的对应位置为‘#’或者传送门后就是终点的情况
3.下面附上代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
int row,col,time,num,ex,ey,ef;
string s1[15],s2[15];
char Graph[15][15][2];
int vis[15][15][2];
int moves1[4]={ 0, 0, 1,-1};
int moves2[4]={-1, 1, 0, 0};
struct node{
int x,y,floor,step;
};
void init()
{
memset(vis,0,sizeof(vis));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
Graph[i+1][j+1][0]=s1[i].at(j);
if(Graph[i+1][j+1][0]=='P'){
ex=i+1;
ey=j+1;
ef=0;
}
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
Graph[i+1][j+1][1]=s2[i].at(j);
if(Graph[i+1][j+1][1]=='P'){
ex=i+1;
ey=j+1;
ef=1;
}
}
}//初始化 找到终点的位置
for(int i=0;i<=row+1;i++)
{
Graph[i][0][0]='*';
Graph[i][0][1]='*';
Graph[i][col+1][0]='*';
Graph[i][col+1][1]='*';
}
for(int i=0;i<=col+1;i++)
{
Graph[0][i][0]='*';
Graph[0][i][1]='*';
Graph[row+1][i][0]='*';
Graph[row+1][i][1]='*';
}//初始化,将图的四周加上边界 避免出界的判断
}
int bfs(int endx,int endy,int endfloor)
{
queue<node> List;
node start;
start.x=start.y=1;
start.floor=start.step=0;
List.push(start);
int now=start.step;
while(!List.empty())
{
node pos=List.front();
List.pop();
now=pos.step;
if(pos.x==endx&&pos.y==endy&&pos.floor==endfloor){
return now;//在队头若找到终点 返回到达所需的最短时间
}
int xx=pos.x,yy=pos.y,ss=pos.floor;
for(int i=0;i<4;i++)
{
if(Graph[moves1[i]+xx][moves2[i]+yy][ss]=='#'
&&vis[moves1[i]+xx][moves2[i]+yy][ss]==0)
{
if((Graph[moves1[i]+xx][moves2[i]+yy][(ss+1)%2]=='.'
||Graph[moves1[i]+xx][moves2[i]+yy][(ss+1)%2]=='P')
&&vis[moves1[i]+xx][moves2[i]+yy][(ss+1)%2]==0)
{
vis[moves1[i]+xx][moves2[i]+yy][ss]=1;
vis[moves1[i]+xx][moves2[i]+yy][(ss+1)%2]=1;
node newnode;
newnode.x=moves1[i]+xx;
newnode.y=moves2[i]+yy;
newnode.floor=(ss+1)%2;
newnode.step=now+1;
List.push(newnode);
}
}
else if(Graph[moves1[i]+xx][moves2[i]+yy][ss]=='.'
&&vis[moves1[i]+xx][moves2[i]+yy][ss]==0)
{
vis[moves1[i]+xx][moves2[i]+yy][ss]=1;
node newnode;
newnode.x=moves1[i]+xx;
newnode.y=moves2[i]+yy;
newnode.step=now+1;
newnode.floor=ss;
List.push(newnode);
}
else if(Graph[moves1[i]+xx][moves2[i]+yy][ss]=='P'
&&vis[moves1[i]+xx][moves2[i]+yy][ss]==0)
{
vis[moves1[i]+xx][moves2[i]+yy][ss]=1;
node newnode;
newnode.x=moves1[i]+xx;
newnode.y=moves2[i]+yy;
newnode.step=now+1;
newnode.floor=ss;
List.push(newnode);
}
}
}
return -1;//如果没有找到 返回-1
}
int main()
{
scanf("%d",&num);
while(num--)
{
memset(vis,0,sizeof(vis));
scanf("%d%d%d",&row,&col,&time);
for(int i=0;i<row;i++){
cin>>s1[i];
}
for(int i=0;i<row;i++){
cin>>s2[i];
}
init();
int ans=bfs(ex,ey,ef);
if(ans<=time&&ans>0)//如果可以达到终点并且时间条件允许,则输出yes
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
相关文章推荐
- HDU 2102 A计划 (三维的迷宫BFS)
- HDU 2102 A计划 (三维的迷宫BFS)
- HDU 2102 A计划(三维BFS)
- HDU 2102 A计划(三维BFS)
- HDU 2102 A计划(三维BFS)
- HDU 2102--A计划【BFS】
- hdu 2102 A计划_bfs搜索
- HDU 2102 A计划 (BFS)
- hdu 2102 A计划(双层BFS)(详解)
- hdu 2102 A计划-bfs
- Hdu 2102 A计划 [Bfs]
- HDU 2102 A计划 BFS
- HDU 2102 A计划 BFS求解
- hdu 2102 A计划(bfs)
- HDU 2102 A计划(BFS)
- hdu 2102 A计划(BFS,基础)
- HDU 2102 A计划 双层BFS
- hdu 2102 A计划 BFS
- HDU 2102 A计划 BFS
- hdu 2102 A计划_bfs搜索