hdu 1175 连连看(dfs+剪枝)
2012-10-28 22:25
316 查看
一些细节没处理好,wa了很多次。
剪枝不强,跑了6000+ms。
View Code
剪枝不强,跑了6000+ms。
View Code
/* Author:Zhaofa Fang Lang:C++ */ #include <cstdio> #include <cstdlib> #include <sstream> #include <iostream> #include <cstring> #include <algorithm> #include <string> #include <utility> #include <vector> #include <queue> #include <stack> #include <map> #include <set> using namespace std; typedef long long ll; #define DEBUG(x) cout<< #x << ':' << x << endl #define PII pair<int,int> #define PB push_back #define MP make_pair #define FI first #define SE second #define lowbit(x) (x&(-x)) #define INF (1<<30) const double eps = 1e-6; int maz[1005][1005]; int dx[]={1,0,0,-1}; int dy[]={0,1,-1,0}; bool OK; int n,m; int x1,y1,x2,y2; bool check(int x,int y) { if(x<1 || x>n || y<1 || y>m)return false; return true; } void dfs(int x,int y,int turn,int dire) { if(turn > 2 || !check(x,y))return; if(OK)return ; if(x == x2 && y == y2) { OK = 1; return; } for(int i=0;i<4;i++) { if(dire + i == 3)continue; int xx = x + dx[i]; int yy = y + dy[i]; if((xx != x2 || yy != y2) && maz[xx][yy] != 0)continue; if(dire != i && dire != -1)dfs(xx,yy,turn + 1,i); else dfs(xx,yy,turn,i); } } int main() { #ifndef ONLINE_JUDGE freopen("in","r",stdin); #endif while(~scanf("%d%d",&n,&m),n,m) { for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) scanf("%d",&maz[i][j]); } int q; scanf("%d",&q); while(q--) { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(maz[x1][y1] != maz[x2][y2] || maz[x1][y1] == 0 || maz[x2][y2] == 0) { puts("NO"); continue; } OK=0; dfs(x1,y1,0,-1); if(OK)puts("YES"); else puts("NO"); } } return 0; }
相关文章推荐
- HDU 1175 连连看(dfs+剪枝)
- hdu 1175 连连看 DFS+剪枝
- HDU-#1175 连连看(DFS+剪枝)
- hdu 1175 连连看
- HDU-OJ-1175 连连看
- hdu 1175 连连看 (bfs)
- 连连看 HDU-1175
- HDU 1175 连连看
- hdu 1175 [连连看] dfs搜索
- hdu 1175 连连看问题
- HDU 1175 连连看(DFS)
- HDU - 1175 连连看(dfs)
- HDU 1175 连连看 (BFS处理转折点)
- HDU 1175 连连看
- HDU 1175 连连看
- HDU 1175 ( 连连看 )
- HDU-1175 连连看 DFS
- HDU 1175 连连看(搜索)
- 【DFS】hdu 1175 连连看
- hdu 1175 连连看