题意:从开始位置走到结束位置,恰好走 t 步 YES

否则 NO

搜索题,由于是恰好走到,所以用到了奇偶剪枝

什么是奇偶剪枝,我也是刚知道

所给步数为 t ,起始位置坐标 (begin_x,begin_y), 结束位置坐标 (end_x,end_y)

两位置最短距离为 ju = abs(end_x - begin_x) + abs(end_y - begin_y)

若 t - ju 为奇数,则无论如何不能恰好走到

为偶数才有可能恰好走到

代码中 pos + dis 即 ju

代码如下:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
int m,n,t;
char map[][];
int vis[][];
int flag;
int begin_x,begin_y,end_x,end_y;
int p[][] = {{-,},{,},{,},{,-}}; //偏移量:上,下,左,右
void dfs(int x,int y,int pos) //pos 为当前已走步数
{
if(flag) return ; //剪枝:已经到达目的地
if(pos > t) return ; //剪枝:步数超过 t
if(x == end_x && y == end_y && pos == t) //恰好 t 步走到
{
flag = ;
return ;
}
int dis = abs(end_x - x) + abs(end_y - y); //当前位置到结束位置的最短距离
if((t - dis - pos) % != ) return; //奇偶剪枝,很重要,不剪TLE
for(int i = ; i < ; i ++) //搜索四个方向
{
int tx = x + p[i][];
int ty = y + p[i][];
if(tx >= && tx < m && ty >= && ty < n && !vis[tx][ty] && map[tx][ty] != 'X')
{
vis[tx][ty] = ;
dfs(tx,ty,pos + );
vis[tx][ty] = ;
}
}
}
int main()
{
while(~scanf("%d%d%d",&m,&n,&t) && (m || n || t))
{
memset(vis,,sizeof(vis));
flag = ;
int k = ;
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j++)
{
cin>>map[i][j]; //scanf注意回车
if(map[i][j] == 'S')
{
begin_x = i;
begin_y = j;
vis[i][j] = ;
}
if(map[i][j] == 'D')
{
end_x = i;
end_y = j;
}
if(map[i][j] == 'X')
k ++;
}
}
if((m * n - k) > t) dfs(begin_x,begin_y,);
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}

HDU 1010 Tempter of the Bone的更多相关文章

  1. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  2. 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 ...

  3. hdu 1010 Tempter of the Bone 奇偶剪枝

      如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...

  4. 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 ...

  5. 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 ...

  6. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. hdu 1010 Tempter of the Bone 深搜+剪枝

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. 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 ...

  9. hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  10. 题解报告:hdu 1010 Tempter of the Bone

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Problem Description The doggie found a bone in a ...

随机推荐

  1. 对dijkstra算法的自我理解,c#例子

    dijkstra该算法主要应用在求解最短路径,从最近点开始,广度搜索. 假设有向图中有10个顶点,求其中某个顶点a到其它顶点的最短路径..满足贪心算法的2个标准.时间复杂度为O(N2) 此问题可以进行 ...

  2. sql server 查询和Kill死锁进程

    查询死锁进程语句 select        request_session_id spid,       OBJECT_NAME(resource_associated_entity_id) tab ...

  3. app进入后台之后接收到通知,点进去进入新的页面,再次进入后台,再点击通知进入页面(,两次通过通知进入的页面,创建了两次,会多一个页面,)解决办法监听

    在点击通知进入的页面的 //UIApplicationWillResignActiveNotification是app即将进入后台的方法 //增加监听使它在进入后台之前pop上一个页面 - (void ...

  4. (转)modelsim10.0C编译ISE14.7的xilinx库(xilinx ip核)

    原地址modelsim10.0C编译ISE14.7的xilinx库(xilinx ip核)   1.打开D:\Xilinx\14.7\ISE_DS\ISE\bin\nt64\compxlibgui.e ...

  5. [整理]S-Record数据格式解析

    S-Reord是一种由摩托罗拉公司创建的文件格式(不得不说,摩托罗拉厉害啊,SPI和S-Record都是他们创造的).S-Record的基本字符为ASCII字符,用以表示相应的十六进制数据.该数据格式 ...

  6. Thailand vs Soros

    | exchange rate | | Thailand | Soros | |---------------+---------+----------+---------| | | orgin | ...

  7. deep learning on object detection

    回归工作一周,忙的头晕,看了两三篇文章,主要在写各种文档和走各种办事流程了-- 这次来写写object detection最近看的三篇文章吧.都不是最近的文章,但是是今年的文章,我也想借此让自己赶快熟 ...

  8. 防止多次领取红包进行ID锁

    //controller里面使用锁 ActivityRedPacket ap = customerService.getActivityRedPacket(params);    if (synchr ...

  9. ios 正则表达式之验证手机号、邮箱、身份证、银行卡

    1.手机号 + (BOOL) IsPhoneNumber:(NSString *)number { NSString *phoneRegex1=@"1[34578]([0-9]){9}&qu ...

  10. Fresco支持的URIs

    //远程图片public void httpBtnOnClick(View view) { //网络图片URL String path = "http://p4.so.qhimg.com/t ...