zoj 1019 Illusive Chase --- dfs
2014-01-10 14:08
495 查看
永远都看不清楚nx ny
#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f3f3f3f using namespace std; int dx[]={0,0,-1,1}; int dy[]={-1,1,0,0}; struct node { int a,b; char dir; }op[10010]; int t,n,m,cnt,mp[105][105],ans; char s[10]; int dfs(int x,int y,int step) { int nx,ny,i,aa,bb,flag; if(step==cnt) return 1; aa=op[step].a; bb=op[step].b; for(i=aa;i<=bb;i++) { switch(op[step].dir) { case 'L':nx=x;ny=y-i;break; case 'R':nx=x;ny=y+i;break; case 'U':nx=x-i;ny=y;break; case 'D':nx=x+i;ny=y; } if(nx<0||nx>=n||ny<0||ny>=m) continue; // printf("i:%d x;%d y:%d nx:%d ny:%d\n",i,x,y,nx,ny); flag=1; if(op[step].dir=='D') { for(int j=x;j<=nx;j++) { if(mp[j][y]) { flag=0; break; } } } else if(op[step].dir=='R') { for(int j=y;j<=ny;j++) { if(mp[x][j]) { flag=0; break; } } } else if(op[step].dir=='U') { for(int j=x;j>=nx;j--) { if(mp[j][y]) { flag=0; break; } } } else if(op[step].dir=='L') { for(int j=y;j>=ny;j--) { if(mp[x][j]) { flag=0; break; } } } if(flag) { if(dfs(nx,ny,step+1)) return 1; } } return 0; } int main() { int i,j; scanf("%d",&t); while(t--) { memset(mp,0,sizeof mp); scanf("%d%d",&n,&m); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%d",&mp[i][j]); } } cnt=0; while(scanf("%d%d",&op[cnt].a,&op[cnt].b)&&(op[cnt].a||op[cnt].b)) { scanf("%s",s); // printf("op:%c\n",s[0]); op[cnt].dir=s[0]; cnt++; } ans=0; for(i=0;i<n;i++) { for(j=0;j<m;j++) if(!mp[i][j]) if(dfs(i,j,0)){ ans++; // printf("i:%d j:%d\n",i,j); } } printf("%d\n",ans); } return 0; }
相关文章推荐
- POJ 1071 && ZOJ 1019 Illusive Chase (dfs)
- POJ 1071 & HDU 1364 & ZOJ 1019 Illusive Chase(DFS)
- ZOJ&nbsp;1019&nbsp;Illusive&nbsp;Chase
- zoj 1019 Illusive Chase
- zoj 1019 Illusive Chase
- zoj 1019 Illusive Chase
- ZOJ1019 Illusive Chase
- 【DFS】HDU 1364 && POJ 1071 Illusive Chase
- POJ1071 Illusive Chase(DFS)
- hdu 1364 Illusive Chase (dfs)
- hdu Illusive Chase 1364 dfs
- 【DFS】HDU 1364 && POJ 1071 Illusive Chase
- HDU 1010 Tempter of the Bone &&ZOJ 2110【DFS】
- ZOJ 3620 Escape Time II【dfs】
- ZOJ2100题解这是我整整意义上的第一道DFS题能做出来我很开心,和上题一样,方法思路大体相同,不过不需要剪枝。
- 【DFS】ZOJ 1580 Sudoku
- hdu1518 Square | ZOJ1909 | POJ2362 (dfs + 剪枝)
- ZOJ 3322 Strange Country II(DFS)
- ZOJ-1832(DFS)
- ZOJ 1709 Oil Deposits(油田问题DFS)