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> ...
随机推荐
- Office 2010 Toolkit and EZ-Activator
“Office 2010 Toolkit 2.0.1”是“迷你KMS”的更新换代版本.虽然是单一可执行程序,但一身承担两大职能:“KMS服务器”和“客户激活端”.“Office 2010 Toolki ...
- 柯南君:看大数据时代下的IT架构(9)消息队列之RabbitMQ--案例(RPC起航)
二.Remote procedure call (RPC)(using the Java client) 三.Client interface(客户端接口) 为了展示一个RPC服务是如何使用的,我们将 ...
- NOI2014 Day2
NOI2014 Day2 动物园 题目描述:给出一个字符串(长度为\(Len\)),设\(num[i]\)为字符串的前\(i\)个字符构成的子串(\(A\))中,满足\(A\)的前\(L\)个字符既是 ...
- 面向对象程序设计-C++_课时30运算符重载——基本规则_课时31运算符重载——原型_课时32运算符重载——赋值_课时33运算符重载——类型转换
区分初始化,赋值 #include <iostream> using namespace std; class Fi { public: Fi() {}//1构造函数 }; class F ...
- 关于json文本数据的一些使用方法
1.对象的存取 如果是对象的存取,可能需要序列化和反序列化对象的属性. NSDictionary params = @{@"hello":@"world"}; ...
- JavaScript 开发经验整理
前言 今年接触了一个B/S的项目,总结了一些JavaScript开发经验,整理些有用的内容与大家分享. 本文会持续更新... 1.实现代码访问的控制 随着项目JavaScript代码库扩大,本应被控制 ...
- 侯老师的话(Application Framework)
摘自http://blog.csdn.net/zlc19876/article/details/5355022 本篇文章主要介绍了"侯老师的话(Application Framework)& ...
- jsp-forward跳转
在Web中可以使用<jsp:forward>指令,将一个用户的请求(request)从一个页面传递到另一个页面,即完成跳转的操作. 1.调整前页:tiaozhuan_a.jsp 代码: & ...
- Unity StrangeIoc框架 (三)signal信号方式
先创建TestRoot using UnityEngine; using System.Collections; using strange.extensions.context.impl; publ ...
- (转)Java通过axis调用WebService
转自:http://blog.csdn.net/wanglha/article/details/49679825 转载地址:http://www.linuxidc.com/Linux/2015-06/ ...