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> ...
随机推荐
- [方法] ubuntu12.04开启root账户
ubuntu 12.04使用LightDM显示管理器,默认禁止root账户登录. 通过修改/etc/lightdm/lightdm.con文件可以打开root登录权限. 方法很简单,只要在lightd ...
- 微软 Office 2010 SP2 正式版下载大全(含简中)
7月24日消息,微软正式为 Office 2010 和 SharePoint 2010 系列产品发布 SP 2服务包,带来重要更新和修复.除了提供产品补丁,SP2服务包还将提升产品的稳定性.性能以及安 ...
- Debug程序无法运行解决
说明:文章内容部分参考网络上的解决思路. 在没有安装Microsoft Visual Studio的系统上,Debug版本无法正常运行.这是由于缺少vs运行时库引起的. 以vs2005为例.开发机用v ...
- C语言的复合文字
假设需要向一个带有int型参量的函数传递一个值,这时可以传递一个int型常量,也可以传递一个int型的变量.在C99标准之前,数组参数情况于现在不一样,没有所谓的数组常量可供传递,而在C99中增加了复 ...
- 简单的Cookie登录
登录页前台代码 <form id="form1" action ="" method="post"> <input typ ...
- 权威指南学习心得-浏览器中的js
window对象:表示web了浏览器的一个窗口或窗体(winow属性引用自身) 含有以下属性:location包含Location对象,指定当前显示在窗口中URL,允许脚本往窗口里载入新的URL 含有 ...
- JavaScript之面向对象学习一
1.通过Object构造函数和对象字面量来创建对象缺点:使用同一个接口创建很多的对象,会产生大量的重复代码.比如我需要创建人的对象,并且需要三类人,医生.工程师.老师,他们可以抽象出很多属性,比如姓名 ...
- SQLite 终端相关命令
SQLite ALL Last login: Fri Dec 5 09:52:08 on ttys002 BeSilent:~ qianfeng$ sqlite3 data.db SQLite ve ...
- C++ this 指针
类的(非静态)成员函数具有一个附加的隐含形参,即指向该类对象的一个指针.这个隐含形参命名为this,与调用成员函数的对象绑定在一起.成员函数不能定义this形参,而是由编译器隐含地定义.成员函数的函数 ...
- B - 确定比赛名次
B - 确定比赛名次 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...