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> ...
随机推荐
- J2SE知识点摘记(八)
1. 多线程指的是在单个进程中可以同时运行多个不同的线程,执行不用的任务.多线程意味着一个程序的多行语句可以看上去几乎同时进行. 同样作为基本的执行单元,线程是划分得比进程更小的执行单位 ...
- poj 3259 (Bellman_Ford判断负环)
题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...
- hdu 1210_(逻辑训练)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1210 #include<stdio.h> int main() { int n,t,sum ...
- Michael Kors - Wikipedia, the free encyclopedia
Michael Kors - Wikipedia, the free encyclopedia Michael Kors From Wikipedia, the free encyclopedia ...
- Android的UI两大基石
说到Android的UI就不得不从一切的开始View开始说. 让我们从Android Developer上的View的Overview和UI Overview来开始吧. Cla ...
- C与C++不同
常量表示方法不同 C不支持引用,C++支持 注释不同,C89不支持单行注释 (++i)++在C中不合法 (a=3)=4在C中不合法 不能在for循环头部定义变量 C++注重类型,强类型,严格检查类型 ...
- 应用java多线程实现server端与多client之间的通信
应用多线程来实现server与多线程之间的通信的基本步骤 1.server端创建ServerSocket,循环调用accept()等待client链接 2.client创建一个Socket并请求和se ...
- hibernate 及缓存机制
hibernate 是一个持久层的框架,经常访问物理数据库,为了降低应用程序访问物理数据库的频次, 从而提升性能, hibernate缓存机制分为: 一类是session 级缓存,二是sessionF ...
- ROS的文件系统
这篇博客介绍一下ROS的文件系统的基本概念,用户可以直接在官方网站:http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem去查看官方手册. ...
- Java基础之静态变量
public class StaticVariable { public static void main(String[] args) { Person p1 = new Person(); Per ...