您的位置:首页 > 其它

HDU 1401 双向BFS !!!

2017-11-01 20:23 573 查看
#include<stdio.h>

#include<string.h>

#include<queue>

#include<iostream>

#include<algorithm>

using namespace std;

struct point 

{
int x,y;
bool check()
{
if(x<8&&x>=0&&y>=0&&y<8)
return true;
return false;
}

};

struct node

{
point p[4];
int step;

}s,e;

int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

int mat[10][10];

bool cmp(point a,point b)//排序

{
if(a.x!=b.x) return a.x<b.x;
return a.y<b.y;

}

void make_map(node a)//标记4个点的位置

{
mat[a.p[0].x][a.p[0].y]=1;
mat[a.p[1].x][a.p[1].y]=1;
mat[a.p[2].x][a.p[2].y]=1;
mat[a.p[3].x][a.p[3].y]=1;

}

int IsOk(point a)

{
if(mat[a.x][a.y])
return 1;
return 0;

}

char vis[8][8][8][8][8][8][8][8];

void make_vis(node a,char w)//标记状态

{
vis[a.p[0].x][a.p[0].y][a.p[1].x][a.p[1].y][a.p[2].x][a.p[2].y][a.p[3].x][a.p[3].y]=w;

}

char get_vis(node a)//获得状态

{
return vis[a.p[0].x][a.p[0].y][a.p[1].x][a.p[1].y][a.p[2].x][a.p[2].y][a.p[3].x][a.p[3].y];

}

bool bfs()

{
node u,v;
memset(vis,0,sizeof(vis));
make_vis(s,'1');
make_vis(e,'2');
s.step=0;
e.step=0;
queue<node>q,t;
q.push(s);
t.push(e);
while(!q.empty()||!t.empty())
{
if(!q.empty())
{
u=q.front();
q.pop();
memset(mat,0,sizeof(mat));
make_map(u);
if(u.step>=4)
continue;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
v=u;
v.step++;
v.p[j].x+=dir[i][0];
v.p[j].y+=dir[i][1];
if(!v.p[j].check())
continue;
if(IsOk(v.p[j]))//跳步
{
v.p[j].x+=dir[i][0];
v.p[j].y+=dir[i][1];
if(!v.p[j].check())
continue;
}
sort(v.p,v.p+4,cmp);
if(get_vis(v)=='1') continue;
else if(get_vis(v)=='2') return true;//可以到达终点
make_vis(v,'1');
q.push(v);
}
}
}
if(!t.empty())
{
u=t.front();
t.pop();
memset(mat,0,sizeof(mat));
make_map(u);
if(u.step>=4)
continue;
if(get_vis(u)=='1')
return true;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
v=u;
v.step++;
v.p[j].x+=dir[i][0];
v.p[j].y+=dir[i][1];
if(!v.p[j].check())
continue;
if(IsOk(v.p[j]))
{
v.p[j].x+=dir[i][0];
v.p[j].y+=dir[i][1];
if(!v.p[j].check())
continue;
}
sort(v.p,v.p+4,cmp);
if(get_vis(v)=='1') return true;
else if(get_vis(v)=='2') continue;
make_vis(v,'2');
t.push(v);
}
}
}
}
return false;

}

int main()

{
while(~scanf("%d%d",&s.p[0].x,&s.p[0].y))
{
for(int i=1;i<4;i++)
scanf("%d%d",&s.p[i].x,&s.p[i].y);
for(int i=0;i<4;i++)
scanf("%d%d",&e.p[i].x,&e.p[i].y);
for(int i=0;i<4;i++)
{
s.p[i].x--;s.p[i].y--;
e.p[i].x--;e.p[i].y--;
}
sort(s.p,s.p+4,cmp);
sort(e.p,e.p+4,cmp);
bfs()?puts("YES"):puts("NO");
}
return 0;

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