zoj 2110 很好的dfs+奇偶剪枝
//我刚开始竟然用bfs做,不断的wa,bfs是用来求最短路的而这道题是求固定时间的
//剪纸奇偶剪枝加dfs
#include<stdio.h>
#include<queue>
#include<math.h>
#include<string.h>
using namespace std;
#define N 10
char ma[N][N];
struct node {
int x,y,step;
}ss,tt;
int dis[4][2]={1,0,-1,0,0,1,0,-1},n,m,t;
int vis[N][N];
int judge(int x,int y) {
if(ma[x][y]!='X'&&x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y])//刚开始这里少些!vis[x][y]导致wa
return 1;
return 0;
}
int dfs(int x,int y,int step) {
int xx,yy,st,i;
if(ma[x][y]=='D'&&step==t)return 1;
if(step>=t) return 0;
for(i=0;i<4;i++) {
xx=x+dis[i][0];
yy=y+dis[i][1];
st=step+1;
if(judge(xx,yy)) {
vis[xx][yy]=1;
if(dfs(xx,yy,st))return 1;
vis[xx][yy]=0;
}
}
return 0;
}
int main() {
int i,j,k;
while(scanf("%d%d%d",&n,&m,&t),n||m||t) {
for(i=1;i<=n;i++)
scanf("%s",ma[i]+1);
k=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++) {
if(ma[i][j]=='S') {
ss.x=i;
ss.y=j;
}
if(ma[i][j]=='D') {
tt.x=i;
tt.y=j;
}
if(ma[i][j]=='X')
k++;
}
if(n*m-k<t||(((int)fabs(tt.x-ss.x)+(int)fabs(tt.y-ss.y))%2!=t%2)) {
printf("NO\n");
continue;
}
memset(vis,0,sizeof(vis));
vis[ss.x][ss.y]=1;
if(dfs(ss.x,ss.y,0))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
zoj 2110 很好的dfs+奇偶剪枝的更多相关文章
- hdoj--1010<dfs+奇偶剪枝>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 杭电1010(dfs + 奇偶剪枝)
题目: The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Tempter of the Bone(dfs+奇偶剪枝)题解
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1010:Tempter of the Bone(DFS+奇偶剪枝+回溯)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu1010Tempter of the Bone(dfs+奇偶剪枝)
题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...
随机推荐
- self , static 都是何方神圣?
前言: php中 this 用于代指 对象, 而代指类的却有3个:self , static , parent self , static , parrent 既然都能代指类,那么他们之间又有哪些区 ...
- 自己编写的str操作函数
1.strcat() 此函数原型为 char *strcat(char *dest, const char *src). 功能为连接两个字符串,把src连接到dest后面:返回dest地址 实现如下 ...
- asp.net ajax get post 中文乱码解决办法
前台: var username = $("#UserName").val(); var tel = $("#tel").val(); var yzm = $( ...
- 19 C#循环语句的跳过和中断 continue和break
在C#的循环语句中,有的时候我们希望跳过其中某个循环,有时我们希望当某个条件满足时,直接终止整个循环.C#为我们提供了 continue;和break;语句. continue和break的用法一样, ...
- 1102 A-B数对
题目描述 出题是一件痛苦的事情! 题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A+B Problem,改用A-B了哈哈! 好吧,题目是这样的:给出一串数以及一个数字C,要求计算出所有A-B=C的数 ...
- WordPress百度熊掌号页面改造(纯代码实现)
一.粉丝关注改造 1.添加熊掌号ID声明 ID声明 <script src="//msite.baidu.com/sdk/c.js?appid=你的熊掌ID">< ...
- win7 中使用NFS共享
转自和修改自:http://blog.sina.com.cn/s/blog_553761ef0100oevm.html 一 安装 在卸载或更改程序->打开或关闭windows的功能-> 安 ...
- Jmeter的属性和变量
jmeter的属性和变量可以简单理解为编程里面的全局变量和局部变量.属性是全局可见,可以跨线程组传递调用,而变量基本上只能存在于一个线程组中(在测试计划定义的变量也是可以跨线程组传递的).同线程组内的 ...
- 以POST方式推送JSON数据,并接收返回的服务器消息
private static string GetResult(string jsonString, string type) { string url = GetUrl(type); string ...
- 100 道 Linux 笔试题,能拿 80 分就算大神!
本套笔试题共100题,每题1分,共100分.(参考答案在文章末尾) 1. cron 后台常驻程序 (daemon) 用于: A. 负责文件在网络中的共享 B. 管理打印子系统C. 跟踪管理系统信息和错 ...