简单搜索,我这里用的是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. Delphi又要换东家了

    前几天听到这个消息,搞个FMX出来,64位还没搞清楚,又开始折腾了!http://www.deltics.co.nz/blog/posts/2371 No Seriously – Let’s Buy ...

  2. 数据库的有关知识==>>我们的血泪史之经典练习(1-2)

    今天给大家说说数据库的有关知识 抒情一下,想在好困,真的,虽然我在这温暖的教室,身边有知心的盆友, ,很高兴还能是学生的一员,我们还年轻,我们也不会想的太多,高高兴兴上学,快快乐乐回家,每天吃的饱饱, ...

  3. uml入门之14图与图之间的关系

    1.先奉上整理的14图. 2.其次奉上整理的图之间的6种关系

  4. 领域对象模型(domain object model)

    在Play程序中,模型(model)占据了核心地位.它是程序操作的信息的特定领域的表现方式. Martin Fowler这样定义模型: 负责表达业务概念,业务状态信息以及业务规则.尽管保存业务状态的技 ...

  5. sqlserver 存储过程 try catch TRANSACTION (转)

    CREATE PROCEDURE YourProcedure    ASBEGIN    SET NOCOUNT ON; BEGIN TRY---------------------开始捕捉异常    ...

  6. The URL "filename" is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web

    Sharepoint Error : The URL "filename" is invalid. It may refer to a nonexistent file or fo ...

  7. RGui的http代理设置

    办公电脑环境需要http代理访问大网,使用R语言安装包时老是无法连接网络,后来从网上发现解决方法很简单,只需在启动RGui.exe的命令行上加上启动参数就可以了. "C:\Program F ...

  8. sublime: useful commands

    CMD+R go to function in current file CMD+Option+Down find function definition in another file (from ...

  9. JAVA基础学习day21--IO流三-File、Properties、PrintWriter与合并、分割流

    一.File 1.1.File概述 文件和目录路径名的抽象表示形式. 用户界面和操作系统使用与系统相关的路径名字符串 来命名文件和目录.此类呈现分层路径名的一个抽象的.与系统无关的视图.抽象路径名 有 ...

  10. Swift 中的函数

    学习来自<极客学院:Swift中的函数> 工具:Xcode6.4 直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会 练习一: import Foundation //函数 ...