简单搜索,我这里用的是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 !!!

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. #define N 9
  8.  
  9. int vis[N][N];
  10. int res;
  11. int dx[] = {,,-,-,,,-,-};
  12. int dy[] = {,-,,-,,-,,-};
  13.  
  14. inline bool OK(int nx,int ny)
  15. {
  16. if(nx >= && nx <= && ny >= && ny <= && !vis[nx][ny])
  17. return true;
  18. return false;
  19. }
  20.  
  21. void dfs(int sx,int sy,int tx,int ty,int cnt)
  22. {
  23. if(cnt >= res)
  24. return;
  25. if(sx == tx && sy == ty && cnt < res)
  26. {
  27. res = cnt;
  28. return;
  29. }
  30. for(int i=;i<;i++)
  31. {
  32. int nx = sx + dx[i];
  33. int ny = sy + dy[i];
  34. if(!OK(nx,ny))
  35. continue;
  36. vis[nx][ny] = ;
  37. dfs(nx,ny,tx,ty,cnt+);
  38. vis[nx][ny] = ;
  39. }
  40. return;
  41. }
  42.  
  43. int main()
  44. {
  45. char s1[],s2[];
  46. int sx,sy,tx,ty;
  47. while(scanf("%s%s",s1,s2)!=EOF)
  48. {
  49. sx = s1[] - 'a' + ;
  50. tx = s2[] - 'a' + ;
  51. sy = s1[] - '';
  52. ty = s2[] - '';
  53. memset(vis,,sizeof(vis));
  54. vis[sx][sy] = ;
  55. res = ; //·dfs边界,能取到正确的最小值最好,这里设为6或以上即可
  56. dfs(sx,sy,tx,ty,);
  57. printf("To get from %s to %s takes %d knight moves.\n",s1,s2,res);
  58. }
  59. return ;
  60. }

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. ASP.NET Web API涉及到的上下文

    1.表示请求的上下文:HttpRequestContext; 2.表示HttpController:HttpControllerContext; 3.表示Action方法:HttpActionCont ...

  2. asp.net 发送邮件

    asp.net 发送邮件 System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();            msg.To. ...

  3. swift学习笔记之-函数

    //函数 import UIKit /*获得系统时间 var date = NSDate() var timeFormatter = NSDateFormatter() timeFormatter.d ...

  4. mvc项目架构分享系列之架构搭建之Infrastructure

    项目架构搭建之Infrastructure的搭建 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构 ...

  5. MySQL数据库中字符集的问题

    今天在做Hibernate案例,往mysql中写记录的时候,出现ERROR: Incorrect string value: '\xE5\x8A\xA0\xE5\x86\x85...' for col ...

  6. 浅谈ClickableSpan , 实现TextView文本某一部分文字的点击响应

    超文本:http://www.baidu.com 这么一个效果:一行文本当中 前面显示黑色颜色的“超文本:”,后面显示红色颜色的“http://www.baidu.com” 并且要求红色字体的部分可以 ...

  7. java多线程系列1--线程实现与调度

    java的重要功能之一就是内部支持多线程,这一系列文章将详细剖析java多线程的基础知识 多线程概述 多线程引入 程序只有一个执行流程,所以这样的程序就是单线程程序. 假如一个程序有多条执行流程,那么 ...

  8. iOS打印Debug日志的方式

    简单介绍以下几个宏: 1) __VA_ARGS__ 是一个可变参数的宏,这个可变参数的宏是新的C99规范中新增的,目前似乎只有gcc支持(VC6.0的编译器不支持).宏前面加上##的作用在于,当可变参 ...

  9. iOS之 Mac下抓包工具使用wireshark

    主要是mac上面网卡的授权 分三个步骤:    1.wireshark安装        wireshark运行需要mac上安装X11,mac 10.8的系统上默认是没有X11的.先去http://x ...

  10. [VMware]设置VM虚拟机随系统自动启动

    设置步骤: 1.找到VM的安装路径,右键vmware发送到桌面快捷方式 2.右键桌面快捷方式的属性,看到目标的属性框 3.找到需要自启动的虚拟机路径,如: D:\QC_VM\Clone of Wind ...