DFS Zoj 2110
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2110


//2110
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<cstdlib>
using namespace std;
int n, m, t, di, dj, flag;
char map[10][10];
int dir[4][2]={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void dfs(int si, int sj, int cnt)
{
int i, xx, yy;
if(si<=0||sj<=0||si>n||sj>m) return ;
if(si==di&&sj==dj&&cnt==t)
{
flag=1;
return ;
}
int temp=t-cnt-abs(si-di)-abs(sj-dj);
if(temp<0 || temp%2)
return ;
for(i=0; i<4; i++)
{
xx=si+dir[i][0], yy=sj+dir[i][1];
if(map[xx][yy]!='X')
{
map[xx][yy]='X';
dfs(xx, yy, cnt+1);
if(flag) return;
map[xx][yy]='.';
}
}
return ;
}
int main()
{
int i, j, si, sj;
while(scanf("%d%d%d", &n, &m, &t)!=EOF)
{
if(n==0&&m==0&&t==0) break;
memset(map, 0, sizeof(map));
int wal=0;
char temp;
scanf("%c", &temp);
for(i=1; i<=n; i++)
{
//scanf("%s%*c", map[i]);
for(j=1; j<=m; j++)
{
scanf("%c", &map[i][j]);
if(map[i][j]=='S')
si=i, sj=j;
else if(map[i][j]=='D')
di=i, dj=j;
else if(map[i][j]=='X')
wal++;
}
scanf("%c", &temp);
}
if(n*m-wal<=t)
{
printf("NO\n");
continue;
}
flag=0;
map[si][sj]='X';
dfs(si, sj, 0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
} ****************************************
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<cstdlib>
using namespace std;
int n, m, t, dx, dy, flag;
char map[10][10];
int dir[4][2]={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void dfs(int x, int y, int step)
{
int i, xx, yy;
if(x<=0||y<=0||x>n||y>m) return ;
if(x==dx&&y==dy&&step==t)
{
flag=1;
return ;
}
int temp=t-step-abs(x-dx)-abs(y-dy);
if(temp<0 || temp%2)
return ;
for(i=0; i<4; i++)
{
xx=x+dir[i][0], yy=y+dir[i][1];
if(map[xx][yy]!='X')
{
map[xx][yy]='X';
dfs(xx, yy, step+1);
if(flag) return;
map[xx][yy]='.';
}
}
return ;
}
int main()
{
int i, j, x, y;
while(scanf("%d%d%d", &n, &m, &t)!=EOF)
{
if(n==0&&m==0&&t==0) break;
memset(map, 0, sizeof(map));
int wal=0;
char temp;
scanf("%c", &temp);
for(i=1; i<=n; i++)
{
//scanf("%s%*c", map[i]);
for(j=1; j<=m; j++)
{
scanf("%c", &map[i][j]);
if(map[i][j]=='S')
x=i, y=j;
else if(map[i][j]=='D')
dx=i, dy=j;
else if(map[i][j]=='X')
wal++;
}
scanf("%c", &temp);
}
if(n*m-wal<=t)
{
printf("NO\n");
continue;
}
flag=0;
map[x][y]='X';
dfs(x, y, 0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
// zoj 2110 #include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std; char map[9][9]; //迷宫地图
int n,m,t; //迷宫的大小,及迷宫的门会在第t秒开启
int di,dj; //(di,dj):门的位置
bool escape;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};//控制行走方向 左 右 下 上
void dfs(int si,int sj,int cnt) //已经到达(si, sj)位置 且已经花费cnt秒 {
int i,temp;
if (si>n||sj>m||si<=0||sj<=0) return; //起点不再矩阵之内。返回 //边界
if (cnt==t&&si==di&&sj==dj) escape=1;// 起点在门处,且门在零秒的时候开启,成功逃生
if (escape) return; temp=(t-cnt)-abs(si-di)-abs(sj-dj);//
if (temp<0||temp&1) return; //temp<0是最短距离小于时间的剪枝,temp&1是奇偶剪枝,还有一种写法,temp%2!=0;
for (i=0;i<4;i++)
{
if (map[si+dir[i][0]][sj+dir[i][1]]!='X')//如果该方格是可以通过的,跳到此空格进行操作
{
map[si+dir[i][0]][sj+dir[i][1]]='X';//设置志。变为不可通过,表明已经搜过
dfs(si+dir[i][0],sj+dir[i][1],cnt+1); //从此点开始递归
map[si+dir[i][0]][sj+dir[i][1]]='.'; //递归完,将该点还原
}
}
return;
} int main()
{
int i,j ; //循环变量
int si,sj; //小狗的起始位置
while (cin>>n>>m>>t)
{
if (n==0&&m==0&&t==0) break;//测试数据结束
int wall=0;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
{
cin>>map[i][j];
if (map[i][j]=='S')
{
si=i; //记录起点位置
sj=j;
}
else if (map[i][j]=='D')
{
di=i; //记录门位置
dj=j;
}
else if (map[i][j]=='X') wall++; //wall 为不能穿越的方格的个数
}
if (n*m-wall<=t) //可以走的个数少于等于时间,说明即使全部走,也不能刚好在t时间到达门
{
cout<<"NO"<<endl;
continue;
}
escape=0; //逃跑成功标志位,成功了为1,不成功为0
map[si][sj]='X';
dfs(si,sj,0); //从起点出开始深搜,此时时间为零
if (escape) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
DFS Zoj 2110的更多相关文章
- HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝
传送门: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1010 ZOJ:http://acm.zju.edu.cn/onlinejudge/showPr ...
- ZOJ 2110 Tempter of the Bone(DFS)
点我看题目 题意 : 一个N×M的迷宫,D是门的位置,门会在第T秒开启,而开启时间小于1秒,问能否在T秒的时候到达门的位置,如果能输出YES,否则NO. 思路 :DFS一下就可以,不过要注意下一终止条 ...
- ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
题意 一仅仅狗要逃离迷宫 能够往上下左右4个方向走 每走一步耗时1s 每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次 问狗是否有可能逃离这个迷宫 直接DFS 直道找到满足条件的路径 ...
- ZOJ 2110 DFS
狗要出门,且正好在T秒 就是DFS + 剪枝, 联系一下剪枝技巧 #include<iostream> #include<cstdio> #include<cstring ...
- zoj 2110 Tempter of the Bone (dfs)
Tempter of the Bone Time Limit: 2 Seconds Memory Limit: 65536 KB The doggie found a bone in an ...
- zoj 2110 很好的dfs+奇偶剪枝
//我刚开始竟然用bfs做,不断的wa,bfs是用来求最短路的而这道题是求固定时间的 //剪纸奇偶剪枝加dfs #include<stdio.h> #include<queue> ...
- DFS ZOJ 1002/HDOJ 1045 Fire Net
题目传送门 /* 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1 ...
- stack+DFS ZOJ 1004 Anagrams by Stack
题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ...
- ZOJ 2110 Tempter of the Bone
Tempter of the Bone Time Limit: 2 Seconds Memory Limit: 65536 KB The doggie found a bone in an ...
随机推荐
- SQL server 如何修改登录名和密码
No :1 启动SQL Server Management Studio,用windows登录进入: No :2 在左侧对象资源处理器中找到根节点,也就是你安装sqlserver时注册的服务器名称.然 ...
- 使用EMMET中的小坑
使用EMMET写HTML的时候,是一个非常爽的事情.但是今天我使用时,发现一个小坑.以前倒也没有注意,不过需要非常的小心. form[action="/process" metho ...
- 内联式css样式,直接写在现有的HTML标签中
CSS样式可以写在哪些地方呢?从CSS 样式代码插入的形式来看基本可以分为以下3种:内联式.嵌入式和外部式三种.这一小节先来讲解内联式. 内联式css样式表就是把css代码直接写在现有的HTML标签中 ...
- SpringMVC4+thymeleaf3的一个简单实例(篇五:页面和MySql的数据交互-展示以及存储)
这一篇将介绍怎样把页面数据保存的MySQL数据库,并将数据库内容展示到页面上.首先做一个基础工作,添加以下jar到lib:1: mysql-connector-Java-5.1.40-bin.jar ...
- 关于typedef的用法总结
不管实在C还是C++代码中,typedef这个词都不少见,当然出现频率较高的还是在C代码中.typedef与#define有些相似,但更多的是不同,特别是在一些复杂的用法上,就完全不同了,看了网上一些 ...
- eval函数:分号的应用
eval("echo'hello world';"); ("参数;") eval函数把参数当做php代码来执行,参数后要有分号,最后还要另加一个分号 相当于: ...
- python学习第五天 List和tuple类型介绍及其List切片
List 和tuple: python提供一种类似C语言数组的类型,但是使用起来确是相当的简洁.那就讲讲这神奇的python中list 和tuple吧. List类型: 1.直接贴代码: L = [' ...
- openerp report image
webkit : 再mako 文件中插入以下代码, <% %>标签用于再mako中定义代码或者函数. 然后 ${ embed_image('图片类型', 图片字段 , 宽度,高度) } ...
- cygwin编译ffmpeg移植到android平台问题集锦
编译环境: windows xp Cygwin 1.1.3.1 NDK r9 1.提示各种command not found 比如 ./config.sh: line 6: $'--arch=arm\ ...
- objective-c 错题
//1, NSString *name = [[NSString alloc]initWithString:@"张三"]; NSLog(@"%d",[name ...