这道题困扰我的不是算法问题。而是细节问题。不优化一直搜到底 时间是690ms左右

没有优化的dfs

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char map[][];
int flag[][];
int way[][]={ ,,,-,,,-, };
typedef struct
{
int x,y;
} point;
point sta;
int n,m;
int t;
int dfs( int s,int x,int y )
{
int i,xx,yy;
if( map[x][y]=='D' && s==t ) return true; /*c++,true c用1*/
if( map[x][y]=='D' && s!=t ) return false;
if( s>=t ) return false;
for( i=;i<;i++ )
{
xx=x+way[i][] , yy=y+way[i][];
if( xx>= && xx<n && yy>= && yy<m && map[xx][yy]!='X' && !flag[xx][yy] )
{
flag[xx][yy]=;
if( dfs(s+,xx,yy) )
return true;
flag[xx][yy]=;
}
}
return false;
} int main()
{
int i,j;
while(scanf("%d%d%d",&n,&m,&t)&&(n||m||t))
{
memset( flag,,sizeof(flag) );
for( i=;i<n;i++ )
{
scanf("%s",map[i]);//在这里是关键地方,我就是死在这里了,c里面先用getchar()消去回车。然后getchar()一个个接收。但是不幸的是wa了!!这里最好用 %s可以消去回车,用这个可以ac。c++里用cin>>同样不用考虑这个问题
for( j=;j<m;j++ )
{
if( map[i][j]=='S' )
{
sta.x=i ;
sta.y=j ;
}
}
}
flag[sta.x][sta.y]=;
if( dfs( ,sta.x,sta.y ) )
printf( "YES\n" );
else printf( "NO\n" );
}
return ;
}

优化后的算法时间是260ms

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char map[][];
int flag[][];
int way[][]={ ,,,-,,,-, };
int n,m;
int t,d,ex,ey,sx,sy;
int dfs( int s,int x,int y )
{
int i,xx,yy;
if( map[x][y]=='D' && s==t ) return ;
if( map[x][y]=='D' && s!=t ) return ;
if( s>=t ) return ; d = abs(x-ex) + abs(y-ey); /*如果预计正常没有墙壁阻隔,那么最短能走多少步*/
if( d + s > t) /*当步数大于要求的步数就返回*/
return ; if( d % != (t-s) % ) /*因为想要到达目的地,如果中间有阻碍想要跳过再回到最短的线路上就要多走2步 */
return ; /*所以能到达目的地的步数量一定是d+n*2 所以这个Time一定是和d同奇偶的
而判断下个点有没有偏移正确方向就看到这个点开始能否走到目的地 判断是否能走到
就是看(Time-t)规定步数剩下的步数是否可以到达 就是问这个和d是否同奇偶*/ for( i=;i<;i++ )
{
xx=x+way[i][] , yy=y+way[i][];
if( xx>= && xx<n && yy>= && yy<m && map[xx][yy]!='X' && !flag[xx][yy] )
{
flag[xx][yy]=;
if( dfs(s+,xx,yy) )
return ;
flag[xx][yy]=;
}
}
return ;
} int main()
{
int i,j;
while(scanf("%d%d%d",&n,&m,&t)&&(n||m||t))
{
memset( flag,,sizeof(flag) );
for( i=;i<n;i++ )
{
scanf("%s",map[i]);/*最关键的地方*/
for( j=;j<m;j++ )
{
if( map[i][j]=='S' )
{
sx=i ;
sy=j ;
}
else if(map[i][j]=='D')
{
ex=i;
ey=j;
}
}
}
flag[sx][sy]=;
if( dfs( ,sx,sy ) )
printf( "YES\n" );
else printf( "NO\n" );
}
return ;
}

zoj 2110的更多相关文章

  1. DFS Zoj 2110

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2110 //2110 #include<stdio.h> #in ...

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

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

  4. ZOJ 2110 Tempter of the Bone(DFS)

    点我看题目 题意 : 一个N×M的迷宫,D是门的位置,门会在第T秒开启,而开启时间小于1秒,问能否在T秒的时候到达门的位置,如果能输出YES,否则NO. 思路 :DFS一下就可以,不过要注意下一终止条 ...

  5. ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

    题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径 ...

  6. ZOJ 2110 DFS

    狗要出门,且正好在T秒 就是DFS + 剪枝, 联系一下剪枝技巧 #include<iostream> #include<cstdio> #include<cstring ...

  7. ZOJ 2110 C - Tempter of the Bone

    https://vjudge.net/contest/67836#problem/C The doggie found a bone in an ancient maze, which fascina ...

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

  9. zoj 2110 很好的dfs+奇偶剪枝

    //我刚开始竟然用bfs做,不断的wa,bfs是用来求最短路的而这道题是求固定时间的 //剪纸奇偶剪枝加dfs #include<stdio.h> #include<queue> ...

随机推荐

  1. SQL Server 执行计划重编译的两大情况

    1.与正确性相关的重编译 1.为表或视图添加列,删除列. 2.为表添加约束.默认值.规则,删除约束.默认值.规则. 3.为表或视图添加索引. 4.如果计划用不用索引而这个索引被删除. 5.删除表中的统 ...

  2. sim卡中电话本(ADN)的简要格式

    ADN的格式 ADN存放于sim卡下面3f00/7f10/6f3a,记录文件格式,其最小记录格式为14,最长为255(?),记录个数最大为255(?) 其后数14个字节是必有的,其前12个字节是电话号 ...

  3. fragment、ListFragment使用ListView及自定义Listview等初始化操作

    fragment.ListFragment使用ListView及自定义Listview等初始化操作 1.先说一下 从官方api中说fragment碎片中使用Listview有专门的 ListView碎 ...

  4. Windows Azure Web Role 的 IIS 重置

     如果您是一名 Web开发人员,您很可能使用过"简单快捷"的iisreset命令重置运行不正常的 IIS主机.这种方法通常在经典的 Windows Server VM上非常有效 ...

  5. SDL介绍

    SDL(Simple DirectMedia Layer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成.SDL提供了数种控制图像.声音.输出入的函数,让开发者只要用相同或是相似的代码就可以开发 ...

  6. flash跨域策略文件crossdomain.xml

    flash在跨域时唯一的限制策略就是crossdomain.xml文件,该文件限制了flash是否可以跨域读写数据以及允许从什么地方跨域读写数据. 位于www.a.com域中的SWF文件要访问www. ...

  7. PHP 操作redis 详细讲解转的

    http://www.cnblogs.com/jackluo/p/3412670.html phpredis是redis的php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有 ...

  8. python代码的书写要求

    刚刚接触python,python是对缩进要求很严格的语言,对于我这种平时tab,空格乱用的菜鸟来说简直是吃劲苦头阿,经常出现IndentationError.在这里我就结合自己的经历说说书写格式,如 ...

  9. 文档onload处理程序

    //第一种方式是注册onload事件处理代码, //onload事件会在页面或图像加载完成后立即发生.这意味着onload事件的执行时间是在 //document对象和document对象所关联的所有 ...

  10. pl/sql developer 编码格式设置(转)

    一.pl/sql developer 中文字段显示乱码 原因:因为数据库的编号格式和pl /sql developer的编码格式不统一造成的. 二.查看和修改oracle数据库字符集: select ...