Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 9320    Accepted Submission(s): 5495

Problem Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of
the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.

Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part. 



Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b. 
 
Input
The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard. 
 
Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.". 
 
Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
 
Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves. To get from f6 to f6 takes 0 knight moves. 题意:象棋中悍马从起点到终点所需要走的最少步数
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue> using namespace std; char c1, c2;
int n1, n2, n3, n4;
int map[10][10];
int b[8][2] = {2,1,1,2,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};
int ans = 0;
struct node {
int x;
int y;
int step;
}; int judge(int x, int y) {
if ((x<1 || x>8) || (y<1 || y>8)) return 0;
if (map[x][y]) return 0;
return 1;
} int bfs() {
node cur, next;
cur.x = n1;
cur.y = n3;
cur.step = 0;
map[n1][n3] = 1;
queue <node> q;
q.push(cur);
while (!q.empty()) {
cur = q.front();
q.pop();
if (cur.x == n2 && cur.y == n4) return cur.step;
for (int i = 0; i<8; i++) {
next.x = cur.x + b[i][0];
next.y = cur.y + b[i][1];
if (next.x == n2 && next.y == n4) return cur.step+1;
if (judge(next.x, next.y)) { next.step = cur.step + 1;
map[next.x][next.y] = 1;
q.push(next);
}
//cout << i<< endl; } }
return -1;
} int main() {
while (cin >> c1>> n1 >> c2>> n2) {
n3 = (int)c1-96;
n4 = (int)c2-96;
//cout << n3 << endl << n4 << endl; memset(map, 0, sizeof(map));
ans = bfs();
cout << "To get from "<<c1<< n1<<" to "<<c2<< n2<<" takes " << ans << " knight moves." << endl;
}
return 0;
}

HDU1372搜索的更多相关文章

  1. hdu1372 dfs搜索之国际象棋的马

    原题地址 题意 一个8x8的国际象棋棋盘,你有一个棋子"马".算出棋子"马"从某一格到还有一格子的最少步数. 与普通dfs不同的是,你能走的路线不是上下左右,四 ...

  2. hdu1372 Knight Moves BFS 搜索

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

  3. SQLSERVER走起微信公众帐号已经开通搜狗微信搜索

    SQLSERVER走起微信公众帐号已经开通搜狗微信搜索 请打开下面链接 http://weixin.sogou.com/gzh?openid=oIWsFt-hiIb_oYqQHaBMoNwRB2wM ...

  4. solr_架构案例【京东站内搜索】(附程序源代码)

    注意事项:首先要保证部署solr服务的Tomcat容器和检索solr服务中数据的Tomcat容器,它们的端口号不能发生冲突,否则web程序是不可能运行起来的. 一:solr服务的端口号.我这里的sol ...

  5. SQLServer地址搜索性能优化例子

    这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...

  6. HTML5轻松实现搜索框提示文字点击消失---及placeholder颜色的设置

    在做搜索框的时候无意间发现html5的input里有个placeholder属性能轻松实现提示文字点击消失功能,之前还傻傻的在用js来实现类似功能... 示例 <form action=&quo ...

  7. bzoj1079--记忆化搜索

    题目大意:有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得 ...

  8. bzoj3208--记忆化搜索

    题目大意: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目.    我们可以把风景区看作一个n*n的地图,每个点有它的初始高度,滑雪只能从高处往低处滑[严格大于] ...

  9. Android中通过ActionBar为标题栏添加搜索以及分享视窗

    在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...

随机推荐

  1. MySQL创建用户与授权方法

    最近在弄个mysql兼职项目,记录一下: 一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username ...

  2. 推荐:让你快速搞定各服务端(api,pc,mobile,wechat)代码

    如果你在写服务端 (PHP) ,会因为项目须求(做app.pc.mobiel.微信) 而写几套代码的,你不觉得很累吗? 现在的很多开源框架商用版本在做程序方面都是这么一套一套的,维护起来,二开起来特别 ...

  3. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  4. windows 下共享内存使用方法示例

    windows下共享内存使用方法较 linux 而言微微复杂 示例实现的功能 有一个视频文件,一块内存区域 : 程序 A,将该视频写入该内存区域 : 程序 B,从该内存区域读取该视频 : 代码模块实现 ...

  5. Redis在APP中的应用

    前言 redis 是内存型数据库,读取data速度远快于mysql和sqlserver,如果将APP中列表信息或者一些常被访问的信息转存至内存上,然后APP通过redis读取内存上的数据,那么APP的 ...

  6. Notepad++使用教程

    Notepad++ 快捷键 大全 Ctrl+C 复制Ctrl+X 剪切Ctrl+V 粘贴Ctrl+Z 撤消Ctrl+Y 恢复Ctrl+A 全选Ctrl+F 键查找对话框启动Ctrl+H 查找/替换对话 ...

  7. Xamarin.android 重写axml控件

    https://www.cnblogs.com/lonelyxmas/p/5632694.html <Laco: 用来用引指定的控件            android:layout_widt ...

  8. Swift语言中与C/C++和Java不同的语法(四)

    这一节,我们将会讨论一下Swift中的函数相关的基本内容 首先是函数的创建: func sayHello (name:String) -> String { return "Hello ...

  9. TCP 建立连接:三次握手

    转自:http://www.cnblogs.com/winner-0715/p/5032661.html 感谢! TCP 建立连接过程 TCP是因特网中的传输层协议,使用三次握手协议建立连接,下面是T ...

  10. 什么是ObjCTypes?

    先看一下消息转发流程: 在forwardInvocation这一步,你必须要实现一个方法: - (NSMethodSignature *)methodSignatureForSelector:(SEL ...