hdu 1175 bfs+剪枝
2017-01-18 23:51
155 查看
#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 4 1 0 2 2 0 0 0 2 0 2 1 1 0 2 2 0 0 0 0 0 1 1 1 3 4 */
相关文章推荐
- 23.PHP的哈希表实现
- Linux&C语言文件学习笔记(三):文件I/O与系统API
- [06]项目实战-PC 端固定布局(6)
- Batch Normalization 学习笔记
- 系统调用,将一个文件复=中的内容复制到另一个文件中去
- 给点思路
- CentOS使用YUM安装php运行环境,包含php,php-fpm,nginx,mysql
- 文章标题
- 文章标题
- Codeforces 509E(思维)
- Android动画之帧动画实现方式
- MFC 实现保存数据为excel文件格式用那种方法好?
- 排序算法0——引言
- 了解jsonp跨域并实现自己的jsonp
- UVa 12325 宝箱
- MFC 内存泄漏问题
- HP-UX修改MP卡的SNMP团体字
- Spark 2.1.0的运行模式
- 不要过度依赖JQuery
- Arch Linux 记录