#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. Ajax返回的数据存放到js数组

    js定义数组比较简单: var  array = [ ] ; 即可 今天记录一下 js 数组的常用规则: 1. b = [1,'da',"sdaf"]; //定义数组给数组添加默认 ...

  2. netty中的channelPipeline在编程中的作用

    在netty编程中我们绝大多数是要是用nio的,nio相比传统的io更加高效,而nio中核心概念离不开channel,buffer,selector三个重要的对象. 那么在netty中有一个chann ...

  3. imposm模块安装

    imposm安装 pip install imposm 报错,参考:https://imposm.org/docs/imposm/2.5.0/install.html 并安装依赖: sudo ap-g ...

  4. 005 SpringCloud 学习笔记01-----系统架构的演变

    1.系统架构的演变 随着互联网的发展,网站应用的规模不断扩大.需求的激增,带来的是技术上的压力.系统架构也因此不断的演进.升级.迭代.从单一应用,到垂直拆分,到分布式服务,到SOA,以及现在火热的微服 ...

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

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

  6. Django-11-Form组件

    1. 概述 Django的Form组件一般功能有: 验证用户输入 生成html代码 返回错误信息 创建Form类 from django.shortcuts import render, redire ...

  7. Django-07-Model操作

    一.数据库的配置 1. 数据库支持 django默认支持sqlite.mysql.oracle.postgresql数据库  <1> sqlite django默认使用sqlite的数据库 ...

  8. vps建站施工预告

    作为一个小白,最近几天自己用vps搭了个站点,用来发发博客,偶尔还可以去外面看看.后面几章就来记一下过程吧! 结构极为简单,建站用的WordPress,目前也就只有最基础的发文章功能.不过由于习惯了m ...

  9. 第4课,python 条件语句if用法

    主题: 智能对话程序的设计 前言: 在编程中存在三大逻辑结构:顺序结构,分支结构(用条件语句if构成),循环结构.其中循环结构能完成,重复次数多,庞大的工作: 分支结构优势不在完成的多,但占有重要位置 ...

  10. Linux下C++酒店管理系统

    功能要求: ​ 相关源码:码云:传送门,GitHub:传送门 相关图片: 拆分版 make编译 ​ ./hotel运行 ​ 输入2,进入开房模块 ​ 相关源码: class.cpp #include ...