简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC。

这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个合适的边界。这题推导可知,任意两点之间马踩6步之内一定能够到达,6步之内还未搜到说明绝对不是最优结果,果断退出。所以这里的res开始时最小设定为6即可,随着设的res的增大,运行时间越来越多,因为深搜可以有很多的分支,不采取较小的边界的话,可能会浪费很多时间在无用的搜索上,所以需要如此剪枝。

反复提交验证发现,res设不同值的运行时间如下:

res = 6    102ms

res = 10  222ms

res = 20  429ms

res = 30  929ms     (过了30后极速增长)

res = 31  1692ms

res = 32  TLE !!!

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 9 int vis[N][N];
int res;
int dx[] = {,,-,-,,,-,-};
int dy[] = {,-,,-,,-,,-}; inline bool OK(int nx,int ny)
{
if(nx >= && nx <= && ny >= && ny <= && !vis[nx][ny])
return true;
return false;
} void dfs(int sx,int sy,int tx,int ty,int cnt)
{
if(cnt >= res)
return;
if(sx == tx && sy == ty && cnt < res)
{
res = cnt;
return;
}
for(int i=;i<;i++)
{
int nx = sx + dx[i];
int ny = sy + dy[i];
if(!OK(nx,ny))
continue;
vis[nx][ny] = ;
dfs(nx,ny,tx,ty,cnt+);
vis[nx][ny] = ;
}
return;
} int main()
{
char s1[],s2[];
int sx,sy,tx,ty;
while(scanf("%s%s",s1,s2)!=EOF)
{
sx = s1[] - 'a' + ;
tx = s2[] - 'a' + ;
sy = s1[] - '';
ty = s2[] - '';
memset(vis,,sizeof(vis));
vis[sx][sy] = ;
res = ; //·dfs边界,能取到正确的最小值最好,这里设为6或以上即可
dfs(sx,sy,tx,ty,);
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,res);
}
return ;
}

UVA 439 Knight Moves --DFS or BFS的更多相关文章

  1. UVA 439 Knight Moves(BFS)

    Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...

  2. UVA 439 Knight Moves

      // 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...

  3. uva 439 Knight Moves 骑士移动

    这道题曾经写过,bfs.用队列,不多说了,上代码: #include<stdio.h> #include<stdlib.h> #include<string.h> ...

  4. 【UVa】439 Knight Moves(dfs)

    题目 题目     分析 没有估价函数的IDA......     代码 #include <cstdio> #include <cstring> #include <a ...

  5. UVa 439骑士的移动(BFS)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. Knight Moves (双向bfs)

    # 10028. 「一本通 1.4 例 3」Knight Moves [题目描述] 编写一个程序,计算一个骑士从棋盘上的一个格子到另一个格子所需的最小步数.骑士一步可以移动到的位置由下图给出. [算法 ...

  7. H - Knight Moves DFS

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...

  8. Knight Moves UVA - 439

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...

  9. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. 基于 ANSIBLE 自动化运维实践

    摘要:运维这个话题很痛苦,你做任何的产品都离不开运维.不管你用什么语言.什么平台.什么技术,真正能够决定你产品成熟度的很有可能就是你运维的能力.取自 云巴 CEO 张虎在 ECUG 大会上的分享. 云 ...

  2. PHP学习笔记:MySQL数据库的操纵

    Update语句 Update 表名 set 字段1=值1, 字段2=值2  where  条件 练习: 把用户名带  ‘小’的人的密码设置为123456@ 语句:UPDATE crm_user SE ...

  3. PowerBuilder反编译

            最近需要了解某个PowerBuilder程序如何工作的,这已经是某个时代的产物了.除了EXE之外,还有一些PBD文件.PBD文件是PowerBuilder动态库,作为本地DLL的一个替 ...

  4. WebForm(ASP开发方式,IIS服务器、WebForm开发基础)

    一.B/S和C/S 1.C/S C/S 架构是一种典型的两层架构,其全程是Client/Server,即客户端服务器端架构,其客户端包含一个或多个在用户的电脑上运行的程序,而服务器端有两种,一种是数据 ...

  5. Css文字特效之text-shadow特效

    今天总结一下文字特效text-shadow,如果用好它可以做出各种不一样的效果,下图是我做出的几种效果. 怎么样,看起来很不错吧,下面贴代码. /* css */ p{ width:300px; ma ...

  6. andriod 读取网络图片

    来自:http://blog.csdn.net/jianghuiquan/article/details/8641283 Android手机上,我们常用ImageView显示图片,我们本章获取网络图片 ...

  7. Oracle之自动收集统计信息

    一.Oracle 11g 在Oracle的11g版本中提供了统计数据自动收集的功能.在部署安装11g Oracle软件过程中,其中有一个步骤便是提示是否启动这个功能(默认是启用这个功能). 在这里介绍 ...

  8. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q40-Q44)

    Question 40You need to send a single value from a consumer Web Part to a provider Web Part.Which int ...

  9. 推荐几个优秀的java爬虫项目

    java爬虫项目   大型的: Nutch apache/nutch · GitHub 适合做搜索引擎,分布式爬虫是其中一个功能. Heritrix internetarchive/heritrix3 ...

  10. iOS开发Facebook POP动效库使用教程

    如果说Origami这款动效原型工具是Facebook Paper的幕后功臣,那么POP便是Origami的地基.感谢Facebook开源了POP动效库,让人人都能制作出华丽的动效.我们只需5步,便能 ...