hdu 1175 bfs+剪枝
来源:互联网 发布:奥维互动地图mac版 编辑:程序博客网 时间:2024/04/27 23:58
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;int a[1005][1005],n,m,flag,cnt[1005][1005];struct node{int sx,sy,hehe,cntxxx;}s,e;int temp[5][2]={0,0,0,1,1,0,0,-1,-1,0};void bfs(){ queue<node> que; que.push(s);cnt[s.sx][s.sy]=0; while(que.size()) { node now=que.front(); que.pop();if(now.cntxxx>2)continue; if(now.sx==e.sx&&now.sy==e.sy) { flag=1; return; } for(int i=1;i<=4;i++) { int xx=now.sx+temp[i][0],yy=now.sy+temp[i][1];if(!(xx>0&&xx<=n&&yy>0&&yy<=m)) continue; if(a[xx][yy]==0||((xx==e.sx)&&(yy==e.sy))) { node tp=now; tp.sx=xx; tp.sy=yy; tp.hehe=i; if(now.hehe!=i) { if(now.hehe!=-1)tp.cntxxx++; if(tp.cntxxx==2&&((xx-e.sx)*temp[i][1]==(yy-e.sy)*temp[i][0])) { if(cnt[xx][yy]==-1||tp.cntxxx<cnt[xx][yy]){que.push(tp);cnt[xx][yy]=tp.cntxxx;} } else if(tp.cntxxx==1) { if(cnt[xx][yy]==-1||tp.cntxxx<cnt[xx][yy]) { que.push(tp); cnt[xx][yy]=tp.cntxxx; }} } if(cnt[xx][yy]==-1||tp.cntxxx<cnt[xx][yy]){que.push(tp);cnt[xx][yy]=tp.cntxxx;} } } }}int main(){ int q; while(scanf("%d%d",&n,&m)!=EOF&&(n||m)) { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]); scanf("%d",&q); while(q--&&scanf("%d%d%d%d",&s.sx,&s.sy,&e.sx,&e.sy)!=EOF) { flag=0; memset(cnt,-1,sizeof(cnt));s.cntxxx=0;s.hehe=-1;e.cntxxx=0;e.hehe=-1; if(a[s.sx][s.sy]==a[e.sx][e.sy]&&a[s.sx][s.sy]) bfs(); if(flag) printf("YES\n"); else printf("NO\n"); } } return 0;}/*5 41 0 2 20 0 0 20 2 1 10 2 2 00 0 0 011 1 3 4*/
0 0
- hdu 1175 bfs+剪枝
- HDU 1175 连连看(bfs + 剪枝)
- HDU-1548(BFS+剪枝)
- hdu 2433 Travel bfs+剪枝
- HDU-1010 bfs+剪枝
- 【搜索之BFS + 剪枝】杭电 hdu 1175 连连看
- HDU 胜利大逃亡 bfs+剪枝
- hdu-1253——三维BFS+剪枝
- HDU 1072Nightmare(BFS+剪枝)
- HDU 1253 胜利大逃亡 (BFS,剪枝)
- BFS + 剪枝 之 hdu 5012 Dice
- hdu 5040 优先队列BFS+剪枝
- HDU 4474(神奇的BFS+强剪枝)
- hdu 1226(bfs+同余剪枝)
- hdu 1401/poj 1198 Solitaire(BFS,剪枝)
- HDU 3533 Escape (bfs + 预处理 + 剪枝)
- HDU 6223 Infinite Fraction Path [bfs+剪枝]
- 【最短路+bfs+剪枝】杭电 hdu 2433 Travel
- 文章标题
- 每一对顶点的最短路径(Floyd算法)
- CentOS使用YUM安装php运行环境,包含php,php-fpm,nginx,mysql
- 给点思路
- [06]项目实战-PC 端固定布局(6)
- hdu 1175 bfs+剪枝
- centos7 mariadb主从复制配置搭建
- css的浮动以及如何清除浮动
- 23.PHP的哈希表实现
- MFC 菜单处理函数
- 1011. World Cup Betting 解析
- 存储基础:DAS/NAS/SAN存储类型及应用
- 解数独_递归
- centos7 mariadb主从复制配置搭建