杭电 1010 Tempter of the Bone
来源:互联网 发布:全球网络连接七彩图 编辑:程序博客网 时间:2024/05/22 06:06
杭电 1010
这个题目的意思是在n*m的矩阵迷宫里,你必须在 t 秒内达到出口。(1s 可以走过一个房间)
S 和 D 分别代表 初始位置 和 出口 ,而 。 和 X 则分别代表 可行房间 和 阻拦的墙。
#include <stdio.h>#include <string.h>#include <stdlib.h>int MAP[9][9]={0};int dx[4]={0,1,0,-1};int dy[4]={1,0,-1,0};int n,m,t,sum;int Sx,Sy,Dx,Dy;int DFS(int x,int y,int time){ int i,px,py; if(x==Dx&&y==Dy&&time==t) return 1; if(abs(Dx-x)+abs(Dy-y)>t-time) return 0; if(Sx<0||Sy<0||Sx>=n||Sy>=m) return 0; for(i=0;i<4;i++) { px=x+dx[i]; py=y+dy[i]; if(px>=0&&px<n&&py>=0&&py<m&&MAP[px][py]!='X'&&time+1<=t) { MAP[px][py]='X'; if(DFS(px,py,time+1)) return 1; MAP[px][py]='.'; } } return 0;}int main(){ int i,j,k,time=0,sum=0; scanf("%d%d%d",&n,&m,&t); while(n||m||t) { sum=0; getchar(); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%c",&MAP[i][j]); if(MAP[i][j]=='S') {Sx=i;Sy=j;} if(MAP[i][j]=='D') {Dx=i;Dy=j;} if(MAP[i][j]=='.') sum++; } getchar(); } if(sum+1<t||(t+Sx+Sy+Dx+Dy)%2==1) printf("NO\n"); else { MAP[Sx][Sy]='X'; k=DFS(Sx,Sy,time); if(k==1) printf("YES\n"); else printf("NO\n"); } scanf("%d%d%d",&n,&m,&t); } return 0;}
因为题目时限是 1000ms ,所以必须适当 剪枝 .
剪枝1:可行房间数小于时间t,和 (t+Sx+Sy+Dx+Dy)%2==1 奇偶剪枝 .
剪枝2: S 点和 D 点距离大于 t 的 .
剪枝3:走到的下一个房间到出口大于剩下的步骤的 .
DFS ▪深度优先搜索
通过向每个方向的摸索前进(按照自己设计的方向摸索),找出到达终点的方法,把摸索的下一点标记为墙,然后把下一点到为下次起点,如果这个方向不行,就重新标记为可行房间,当到达终点且满足时间t,就输出yes 否则no
补上一点,在输入MAP时,如果是一个一个字符输入时,需要加入几个getchar().
0 0
- 杭电1010-Tempter of the Bone
- 杭电 Tempter of the Bone 1010
- 杭电 1010 Tempter of the Bone
- 杭电1010---Tempter of the Bone
- 杭电 1010 Tempter of the Bone
- 【杭电】[1010]Tempter of the Bone
- 杭电1010 Tempter of the Bone
- 杭电acm1010 Tempter of the Bone
- 杭电 HDU 1010 Tempter of the Bone
- 杭电1010Tempter of the Bone(搜索)
- 杭电OJ:1010 Tempter of the Bone
- 杭电1010 Tempter of the Bone(奇偶性剪枝)
- 杭电(hdu)ACM 1010 Tempter of the Bone
- 杭电1010-Tempter of the Bone(BFS)
- C - Tempter of the Bone 杭电1010
- 杭电-1010 Tempter of the Bone(深搜)
- 杭电ACM 1010 Tempter of the Bone
- 杭电OJ——Tempter of the Bone
- csv文件导入mysql乱码解决
- 剑指offer面试题42
- 旧识新知
- Ubuntu 安装OpenGL
- xib的关联问题
- 杭电 1010 Tempter of the Bone
- [dfs] zoj 3736 Pocket Cube
- POJ 2492 A Bug's Life
- s3c2440 uart串口实验
- 群“模”乱舞之策略模式
- 【SQL入门】内连接:显式连接、隐式连接 外连接:左外连接、右外连接
- 4个mysql客户端工具的比较
- VS环境中进行内存泄漏的检测
- Python入门经典 以解决计算问题为导向的Python编程 待完善