HDU 1175(搜索DFS)
来源:互联网 发布:python格式化毫秒数 编辑:程序博客网 时间:2024/05/09 13:18
#include <stdio.h>#include <string.h>#include <iostream>using namespace std;int ss[1005][1005];int vis[1005][1005];int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};int x1, y1, x2, y2, n, m;int fi;int isin(int x, int y){ return x >= 1 && x <= n && y >= 1 && y <= m;}int dfs(int x, int y, int cnt, int di){ if(fi) return 1; vis[x][y] = 1; if(cnt > 2) return 0; if(cnt == 2 && x - x2 != 0 && y - y2 != 0) //最重要的剪枝 return 0; for(int i = 0; i < 4; i++) { int ans; int tempx = x + dir[i][0]; int tempy = y + dir[i][1]; if( i == di) ans = cnt; else ans = cnt + 1; if(!isin(tempx, tempy) || vis[tempx][tempy]) continue; if(tempx == x2 && tempy == y2 && ans <= 2) { fi = 1; return 1; } if(!ss[tempx][tempy]){ if(di == 5) ans = 0; if(dfs(tempx, tempy, ans, i)) return 1; else vis[tempx][tempy] = 0; } } return 0;}int main(){ while(cin>>n>>m) { if(!n && !m) break; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) scanf("%d", &ss[i][j]); int q; cin>>q; for(int i = 0; i < q; i++) { fi = 0; memset(vis, 0, sizeof vis); cin>>x1>>y1>>x2>>y2; if(ss[x1][y1] == 0 || ss[x2][y2] == 0){ cout<<"NO"<<endl; continue; } else if(ss[x1][y1] != ss[x2][y2]){ cout<<"NO"<<endl; continue; } else { if(dfs(x1, y1, 0, 5)) cout<<"YES"<<endl; else cout<<"NO"<<endl; } } } return 0;}
0 0
- HDU 1175(搜索DFS)
- hdu 1175 [连连看] dfs搜索
- HDU 1175 连连看 (搜索 DFS)
- hdu 1175 连连看 搜索—dfs
- HDU 1175 连连看 (搜索,dfs)
- HDU 1175 连连看 DFS+搜索+方向
- 搜索--HDU简单DFS
- DFS+记忆搜索-HDU-1078
- HDU 1016 搜索问题 dfs
- HDU 1242 Rescue (搜索 DFS)
- 【搜索】 HDU 2821 Pusher DFS
- HDU 1015 Safecracker DFS搜索
- 深度搜索DFS hdu-1010
- 深度搜索DFS hdu-1312
- HDU 1015 Safecracker(DFS搜索)
- hdu 5424(dfs搜索)
- hdu 1518 square (DFS)搜索
- 搜索dfs-hdu Oil Deposits
- NYOJ 675 Sinking Ship
- 从原理上搞定编码--Base64编码
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- Spring——jar包详解
- 线性结构总结(2)-线性结构关系与应用
- HDU 1175(搜索DFS)
- C++学习笔记:C++中的new
- Fedora 24 Alpha带着Gnome 3.20姗姗来迟
- ext2文件系统下rm-rf * 误删数据恢复
- 画布和画笔绘制五子棋
- HDU 1177(水题)
- 大数乘法
- 第十一周项目45-运算符重载之非成员函数重载
- Ubuntu 16.04 LTS 默认壁纸公布,多图预警