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 ...
随机推荐
- List转xml
1. List<Model> list = new List<Model>(); Model zj = new Model(); zj.id = ; zj.name = &qu ...
- C#中邮件的发送基本操作
本地配置的邮箱:http://localhost:6080/index.php //邮件的收发需要用到两个类 //1.用来创建一封邮件对象 //1.MailMessage 添加对 usin ...
- [转]mysql导入导出数据中文乱码解决方法小结
本文章总结了mysql导入导出数据中文乱码解决方法,出现中文乱码一般情况是导入导入时编码的设置问题,我们只要把编码调整一致即可解决此方法,下面是搜索到的一些方法总结,方便需要的朋友. linux系统中 ...
- Tomcat 8熵池阻塞变慢详解(转)
Tomcat 8熵池阻塞变慢详解 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Tomcat 8启动很慢,且日志上无任何错误,在日志中查看到如下信息: ...
- mem 族函数的实现
1.void * memcpy ( void * dest, const void * src, size_t num ); 头文件:#include <string.h>memcpy() ...
- SGU 168.Matrix
时间限制:0.5s 空间限制:15M 题意: 给出一个N*M的矩阵A,计算矩阵B,满足B[i][j]=min{ A[x][y]:(y>=j) and ( x>=i+j-y )} Solut ...
- 【POJ2887】【块状链表】Big String
Description You are given a string and supposed to do some string manipulations. Input The first lin ...
- 读书笔记之 - javascript 设计模式 - 组合模式
组合模式是一种专为创建Web上的动态用户界面而量身定制的模式,使用这种模式,可以用一条命令在对各对象上激发复杂的或递归的行为. 在组合对象的层次体系中有俩种类型对象:叶对象和组合对象.这是一个递归定义 ...
- Extjs4 关于设置form中所有子控件为readOnly属性的解决方案
之前在网上找了一堆,但那些确实没法用,后来考虑了一下,发现主要是网上提供的假设form中只有一层控件,没有考虑到布局稍微复杂的form情形,此处采用递归的形式实现对form中所有控件(grid及but ...
- ios开发之代理&&协议
Object-C是不支持多继承的,所以很多时候都是用Protocol(协议)来代替.Protocol(协议)只能定义公用的一套接口,但不能提供具体的实现方法.也就是说,它只告诉你要做什么,但具体怎么做 ...