#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
struct Point
{
int x_, y_;
int route;
};
int dic[8][2] = {-1,2 ,1,2 ,2,1 ,2,-1 ,1,-2 ,-1,-2 ,-2,-1 ,-2,1};
int vis[10][10]; /*判断是否访问过*/
string fir, sec;
int ans; /*记录最短路*/
int x1, y1, x2, y2;
void dfs()
{
if(x1 == x2 && y1 == y2)
{
ans = 0;
return ;
} Point p;
p.x_ = x1;
p.y_ = y1;
vis[x1][y1] = 1;
p.route = 0;
queue<Point> rr;
rr.push(p);
while(!rr.empty())
{
// if(x1 == x2 && y1 == y2)
// break;
Point q;
for(int i = 0; i < 8; i++)
{
q.x_ = rr.front().x_ + dic[i][0];
q.y_ = rr.front().y_ + dic[i][1];
q.route = rr.front().route + 1;
if(vis[q.x_][q.y_] == 1 || q.x_ < 0 || q.y_ < 0 || q.x_ > 7 || q.y_ > 7)
continue;
else
{
vis[q.x_][q.y_] = 1;
rr.push(q);
if(q.x_ == x2 && q.y_ == y2)
{
ans = q.route;
break; /*找到了*/
}
} } if(q.x_ == x2 && q.y_ == y2)
break;
rr.pop(); }
}
int main()
{
while(cin >> fir >> sec)
{
memset(vis, 0, sizeof(vis));
x1 = (int)fir[0] - 'a';
y1 = (int)fir[1] - '0' - 1;
x2 = (int)sec[0] - 'a';
y2 = (int)sec[1] - '0' - 1;
dfs();
cout << "To get from " << fir << " to " << sec << " takes " << ans << " knight moves." << endl; }
}

UVA-439, Knight Moves(深度优先搜索)的更多相关文章

  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 --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

  3. UVA 439 Knight Moves

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

  4. uva 439 Knight Moves 骑士移动

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

  5. 【UVa】439 Knight Moves(dfs)

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

  6. hdu1372 Knight Moves BFS 搜索

    简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...

  7. Knight Moves UVA - 439

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

  8. POJ---2243 Knight Moves 使用A*算法的广度优先搜索

    题目链接:http://poj.org/problem?id=2243 启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省 ...

  9. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

随机推荐

  1. mysqldump导出完整sql脚本

    #导出某个数据库--结构+数据 shell>mysqldump -h192.168.161.124 -uroot -pxxxxxx --opt db_name |gzip -9 > /db ...

  2. Windows删除文件夹下的指定格式文件(递归删除)

    问题描述: 今天遇到一个需求,需要对文件夹进行文件筛选.目录结构较为复杂(目录较多,层次较深),数据量较大(总共60GB左右). 鉴于上述情况,直接排除了人工处理方式(否则小伙伴们会打死我的). 解决 ...

  3. git 删除本地分支,删除远程分支

    本地分支 git branch -d 分支名 远程分支 git push origin --delete 分支名 查看所有分支 git branch -a

  4. 17 SpringMVC response响应

    1.Model.ModelMap和ModelAndView的使用详解 Spring-MVC在请求处理方法可出现和返回的参数类型中,最重要就是Model和ModelAndView了,对于MVC框架,控制 ...

  5. [转帖]50 亿美元!微软签下毕马威!JEDI 100 亿美元订单之后又一大单!

    50 亿美元!微软签下毕马威!JEDI 100 亿美元订单之后又一大单! https://mp.weixin.qq.com/s/K0SrFNSVK5aOu6TIzhN92Q 前段时间,微软击败亚马逊, ...

  6. window 关机

    schtasks /create /tn "关机" /tr "shutdown /s" /sc once /st 20:30

  7. Scala 安装 Scala for Eclipse安装及运行hello word

    Scala下载安装地址:https://www.scala-lang.org/download/ .windows版本的安装包是scala-2.12.8.msi.直接滑动到网页最下面,下载对应的系统的 ...

  8. 【LEETCODE】60、数组分类,适中级别,题目:75、560、105

    package y2019.Algorithm.array.medium; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.a ...

  9. APIO2019题解

    T1.桥梁(bridges/restriction) Subtask1:暴力,$O(n^2)$. #include<cstdio> #include<algorithm> #d ...

  10. MOOC 数据库笔记(三):关系模型之基本概念

    关系模型的基本概念 关系模型简述 1.最早由E.F.Codd在1970年提出. 2.是从表(Table)及表的处理方式中抽象出来的,是在对传统表及其操作进行数学化严格定义的基础上,引入集合理论与逻辑学 ...