HDU 1401 双向BFS !!!
来源:互联网 发布:面板数据分析什么 编辑:程序博客网 时间:2024/05/17 19:20
#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;
}
#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;
}
阅读全文
0 0
- hdu 1401 双向bfs
- HDU 1401 双向BFS !!!
- HDU 1401 Solitaire 双向BFS
- hdu 1401 Solitaire 双向bfs
- HDU 1401 Solitaire 双向BFS
- HDU 1401 Solitaire [双向BFS]
- HDU 1401 Solitaire(双向BFS)
- hdu 1401 Solitaire(双向bfs)
- hdu 1401 双向搜索(bfs)
- hdu 3085 双向BFS
- hdu 3085(双向bfs)
- hdu 1195 双向bfs
- HDU 1560 双向BFS
- HDU 3085 双向BFS
- HDU 3085 Nightmare 双向bfs
- hdu-3085(双向bfs)
- HDU 2612 find the way 双向BFS
- hdu 3085 Nightmare Ⅱ (双向bfs)
- Anniversary Cake
- HTML中href、src区别
- Oracle wait event --- enq: HW
- 解决Hash碰撞冲突方法总结
- 查看当前使用的shell
- HDU 1401 双向BFS !!!
- SiteMesh详解
- 为什么要接口测试?
- Servlet是否是线程安全的
- ZOJ 3981 && 2017CCPC秦皇岛 A:Balloon Robot
- java 小技巧
- TensorFlow学习--TensorBoard神经网络可视化
- 数字推断
- CSS中可以和不可以继承的属性