zoj 2110
这道题困扰我的不是算法问题。而是细节问题。不优化一直搜到底 时间是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的更多相关文章
- DFS Zoj 2110
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2110 //2110 #include<stdio.h> #in ...
- 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
Tempter of the Bone Time Limit: 2 Seconds Memory Limit: 65536 KB The doggie found a bone in an ...
- 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 C - Tempter of the Bone
https://vjudge.net/contest/67836#problem/C The doggie found a bone in an ancient maze, which fascina ...
- 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> ...
随机推荐
- 分析java中clone()方法 (转载+修改)
Java中的clone() 方法 java所有的类都是从java.lang.Object类继承而来的,而Object类提供下面的方法对对象进行复制. protected native Object c ...
- poj2405---体积几何
#include <stdio.h> #include <stdlib.h> #include<math.h> #define pi acos(-1) int ma ...
- 前端开发工具—fiddle
- 【分享】事实上,你VS界面也可以如此,VS界面美化
阿土.它直接在地图上. 第一节目:Transformers(变形金刚) 第二方案:Assassin's Creed (刺客信条) watermark/2/text/aHR0cDovL2Jsb2cuY3 ...
- ZeroClipBoard 复制粘贴插件
ZeroClipboard 1. 引用js 1 <script type="text/javascript" src="/ZeroClipboard.js&q ...
- MATLAB中imshow()和image()
MATLAB中imshow()和image(): IMSHOW Display image in Handle Graphics figure. IMSHOW(I) displays the gray ...
- poj2407---欧拉函数应用
欧拉函数介绍: 在数论中,对正整数n,欧拉函数是少于或等于n你的数中与n互质的数的数目. 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),其中 ...
- VS2010断点无效
可能的原因如下: 1. 菜单tools->Options->Debugging->General,有个Require source files to exactly match t ...
- BZOJ 1406: [AHOI2007]密码箱( 数论 )
(x+1)(x-1) mod N = 0, 枚举N的>N^0.5的约数当作x+1或者x-1... ------------------------------------------------ ...
- ROS使用rqt_console
打开一个新的终端在里面输入: sudo apt-get install ros-hydro-rqt ros-hydro-rqt-common-plugins ros-hydro-turtlesim 安 ...