hdu1010 Tempter of the Bone
来源:互联网 发布:matlab怎么输入矩阵 编辑:程序博客网 时间:2024/05/15 01:03
题目:hdu1010 tzc1221
方法:dfs
思想:由于本题中规定只有在固定时刻到达目的点才能逃脱,
不能提早,更不能延迟,故不能用bfs。可以用dfs找出所有情况,
若有可行解,直接返回即可。本题比较郁闷的是,在tzc上将方向
数组int dir[4][2]稍微变动两下,就会超时。这也是今后出现TLE时
该考虑的重要的一点吧,可以作为经验教训。
代码:
#include <iostream>#include <stdlib.h>using namespace std;char map[10][10];int n,m,t;int dx,dy;bool flag;//int dir[4][2]={-1,0,0,-1,1,0,0,1}; //本题用上面的数组超时,下面的数组AC,在今后//遇到相似问题,应该考虑改动遍历方向数组。 int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; void dfs(int i,int j,int step){ if(i==dx&&j==dy&&step==t)flag=true; if(step>t)return; if(flag)return;//如果找到符合题意的就返回 if((t-step)%2!=(abs(dx-i)+abs(dy-j))%2)return;//如果奇偶性不同就返回 for(int u=0;u<4;u++) { int x=i+dir[u][0]; int y=j+dir[u][1]; if(x>=0&&x<n&&y>=0&&y<m) { if(map[x][y]!='X') { map[x][y]='X'; dfs(x,y,step+1); map[x][y]='.'; } } }}int main(int argc, char *argv[]){ //freopen("hdu1010in.txt","r",stdin); while(cin>>n>>m>>t) { if(n==0&&m==0&&t==0)break; int i,j,k=0; int x,y; for(i=0;i<n;i++) for(j=0;j<m;j++) { cin>>map[i][j]; if(map[i][j]=='S'){x=i,y=j;map[i][j]='X';} else if(map[i][j]=='D'){dx=i,dy=j;k++;} else if(map[i][j]=='.')k++; } if(k<t){printf("NO\n");continue;} flag=false; dfs(x,y,0); if(flag)cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}
- HDU1010 Tempter of the Bone
- Tempter of the Bone hdu1010
- (hdu1010)Tempter of the Bone
- hdu1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- HDU1010:Tempter of the Bone
- HDU1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- hdu1010(Tempter of the Bone)
- hdu1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- hdu1010 Tempter of the Bone
- hdu1010 Tempter of the Bone
- hdu1010 Tempter of the Bone
- hdu1010---Tempter of the Bone
- HDU1010---Tempter of the Bone
- hdu1010 Tempter of the Bone
- 如何写asp.net抓取网页内容的小偷程序
- hookwmi
- .net MVC 项目发布 (MVC 1.0 2.0 均可)
- cocos2d学习-Touch事件
- python集成现有的数据库
- hdu1010 Tempter of the Bone
- 二叉排序树与二叉堆
- swf文件格式解析(一)
- Oracle 字符集的查看和修改
- 二分图的Study
- 批处理set命令变量与等号之间不要加空格
- syslog.conf介绍(转)
- 删除带点的文件(夹)
- JNI调用(原创+转载)