Knight Moves

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

Total Submission(s): 11832    Accepted Submission(s): 6969

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.
 
Source
Problem : 1372 ( Knight Moves )     Judge Status : Accepted

RunId : 21142000    Language : G++    Author : hnustwanghe

Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>

using namespace std;

const int N = 8+5;
int visit[N][N];
typedef struct node{
int x,y,step;
}Node;
const int dir[8][2]={{2,1},{-2,1},{-2,-1},{2,-1},{1,2},{1,-2},{-1,-2},{-1,2}};
int print(){
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++){
printf("%d",visit[i][j]);
if(j==7)
printf("\n");
}
}
int DBFS(int x,int y,int goalx,int goaly){
queue<Node> Q1,Q2;
Node t,s;
int step1=0,step2=0,flag =1,cnt,newx,newy,ans;
memset(visit,0,sizeof(visit));
t.x = x,t.y = y,t.step = 0;
s.x = goalx,s.y = goaly,s.step = 0;
visit[t.x][t.y] = 1;
visit[s.x][s.y] = 2;
Q1.push(t);
Q2.push(s);
while(flag){
cnt = Q1.size();
while(cnt--){
t = Q1.front();
if(t.x == goalx && t.y == goaly) return t.step;
for(int d=0;d<8;d++){
newx = t.x + dir[d][0];
newy = t.y + dir[d][1];
step1 = t.step + 1;
if(newx>0 && newx <= 8 && newy>0 && newy<=8){
if(visit[newx][newy]==2){
ans = step1 + step2;
return ans;
}
if(!visit[newx][newy]){
s.x = newx,s.y= newy,s.step = step1;
Q1.push(s);
visit[newx][newy] = 1;
}
}

}
Q1.pop();
}
cnt = Q2.size();
while(cnt--){
t = Q2.front();
if(t.x==x && t.y==y) return t.step;
for(int d=0;d<8;d++){
newx = t.x + dir[d][0];
newy = t.y + dir[d][1];
step2 = t.step + 1;
if(newx>0 && newx<=8 && newy>0 && newy <=8){
if(visit[newx][newy]==1){
ans = step1 + step2;
return ans;
}
if(!visit[newx][newy]){
s.x = newx,s.y = newy,s.step = step2;
Q2.push(s);
visit[newx][newy] = 2;
}
}
}
Q2.pop();
}
}

return -1;
}
void Input_and_solve(){
char ch[10];
int x,y,goalx,goaly;
while(gets(ch)!=NULL){
x = ch[0]-'a'+1;
y = ch[1]-'0';
goalx = ch[3]-'a'+1;
goaly = ch[4]-'0';
printf("To get from %c%c to %c%c takes %d knight moves.\n",ch[0],ch[1],ch[3],ch[4],DBFS(x,y,goalx,goaly));
//print();
}
}
int main(){
Input_and_solve();
}

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>

using namespace
std; const int N = 8+5;
int
visit[N][N];
typedef struct
node{
int
x,y,step;
}
Node;
const int
dir[8][2]={{2,1},{-2,1},{-2,-1},{2,-1},{1,2},{1,-2},{-1,-2},{-1,2}};
int
print(){
for(int
i=1;i<=8;i++)
for(int
j=1;j<=8;j++){
printf("%d",visit[i][j]);
if(
j==7)
printf("\n");
}
}
int
DBFS(int x,int y,int goalx,int goaly){
queue<Node> Q1,Q2;
Node t,s;
int
step1=0,step2=0,flag =1,cnt,newx,newy,ans;
memset(visit,0,sizeof(visit));
t.x = x,t.y = y,t.step = 0;
s.x = goalx,s.y = goaly,s.step = 0;
visit[t.x][t.y] = 1;
visit[s.x][s.y] = 2;
Q1.push(t);
Q2.push(s);
while(
flag){
cnt = Q1.size();
while(
cnt--){
t = Q1.front();
if(
t.x == goalx && t.y == goaly) return t.step;
for(int
d=0;d<8;d++){
newx = t.x + dir[d][0];
newy = t.y + dir[d][1];
step1 = t.step + 1;
if(
newx>0 && newx <= 8 && newy>0 && newy<=8){
if(
visit[newx][newy]==2){
ans = step1 + step2;
return
ans;
}
if(!
visit[newx][newy]){
s.x = newx,s.y= newy,s.step = step1;
Q1.push(s);
visit[newx][newy] = 1;
}
} }

Q1.pop();
}

cnt = Q2.size();
while(
cnt--){
t = Q2.front();
if(
t.x==x && t.y==y) return t.step;
for(int
d=0;d<8;d++){
newx = t.x + dir[d][0];
newy = t.y + dir[d][1];
step2 = t.step + 1;
if(
newx>0 && newx<=8 && newy>0 && newy <=8){
if(
visit[newx][newy]==1){
ans = step1 + step2;
return
ans;
}
if(!
visit[newx][newy]){
s.x = newx,s.y = newy,s.step = step2;
Q2.push(s);
visit[newx][newy] = 2;
}
}
}

Q2.pop();
}
} return -
1;
}
void
Input_and_solve(){
char
ch[10];
int
x,y,goalx,goaly;
while(
gets(ch)!=NULL){
x = ch[0]-'a'+1;
y = ch[1]-'0';
goalx = ch[3]-'a'+1;
goaly = ch[4]-'0';
printf("To get from %c%c to %c%c takes %d knight moves.\n",ch[0],ch[1],ch[3],ch[4],DBFS(x,y,goalx,goaly));
//print();
}
}
int main(){

Input_and_solve();
}
Problem : 1372 ( Knight Moves )     Judge Status : Accepted
RunId : 21135178    Language : G++    Author : hnustwanghe
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std; typedef struct node{
int x,y,step;
}Node;
bool visit[10][10];
const int dir[8][2]={{-2,1},{-2,-1},{2,-1},{2,1},{1,2},{1,-2},{-1,2},{-1,-2}};
int BFS(int x,int y,int goalx,int goaly){
queue<Node>Q;
memset(visit,0,sizeof(visit));
int newx,newy;
Node t,s;
t.x = x,t.y = y,t.step = 0;
visit[t.x][t.y] = true;
Q.push(t); while(!Q.empty()){
t = Q.front();
if(t.x==goalx && t.y==goaly) return t.step;
for(int d=0;d<8;d++){
newx = t.x+dir[d][0];
newy = t.y+dir[d][1];
if(newx>=1 && newx<=8 && newy>=1 && newy<=8 && !visit[newx][newy]){
s.x = newx;
s.y = newy;
s.step = t.step+1;
Q.push(s);
}
}
Q.pop();
}
return -1;
}
void Input_data_and_solve(){
char a,c,ch[10];
int b,d,x,y,goalx,goaly;
while(gets(ch)!=NULL){
a = ch[0];
b = ch[1]-'0';
c = ch[3];
d = ch[4]-'0';
x = a-'a'+1;
y = b;
goalx = c-'a'+1;
goaly = d;
BFS(x,y,goalx,goaly);
printf("To get from %c%d to %c%d takes %d knight moves.\n",a,b,c,d,BFS(x,y,goalx,goaly));
}
}
int main(){
Input_data_and_solve();
}
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std; typedef struct node{
int
x,y,step;
}
Node;
bool
visit[10][10];
const int
dir[8][2]={{-2,1},{-2,-1},{2,-1},{2,1},{1,2},{1,-2},{-1,2},{-1,-2}};
int
BFS(int x,int y,int goalx,int goaly){
queue<Node>Q;
memset(visit,0,sizeof(visit));
int
newx,newy;
Node t,s;
t.x = x,t.y = y,t.step = 0;
visit[t.x][t.y] = true;
Q.push(t); while(!Q.empty()){
t = Q.front();
if(
t.x==goalx && t.y==goaly) return t.step;
for(int
d=0;d<8;d++){
newx = t.x+dir[d][0];
newy = t.y+dir[d][1];
if(
newx>=1 && newx<=8 && newy>=1 && newy<=8 && !visit[newx][newy]){
s.x = newx;
s.y = newy;
s.step = t.step+1;
Q.push(s);
}
}

Q.pop();
}
return -
1;
}
void
Input_data_and_solve(){
char
a,c,ch[10];
int
b,d,x,y,goalx,goaly;
while(
gets(ch)!=NULL){
a = ch[0];
b = ch[1]-'0';
c = ch[3];
d = ch[4]-'0';
x = a-'a'+1;
y = b;
goalx = c-'a'+1;
goaly = d;
BFS(x,y,goalx,goaly);
printf("To get from %c%d to %c%d takes %d knight moves.\n",a,b,c,d,BFS(x,y,goalx,goaly));
}
}
int main(){

Input_data_and_solve();
}

搜索专题: HDU1372Knight Moves的更多相关文章

  1. HDU(搜索专题) 1000 N皇后问题(深度优先搜索DFS)解题报告

    前几天一直在忙一些事情,所以一直没来得及开始这个搜索专题的训练,今天做了下这个专题的第一题,皇后问题在我没有开始接受Axie的算法低强度训练前,就早有耳闻了,但一直不知道是什么类型的题目,今天一看,原 ...

  2. NOIP2018提高组金牌训练营——搜索专题

    NOIP2018提高组金牌训练营——搜索专题 1416 两点 福克斯在玩一款手机解迷游戏,这个游戏叫做”两点”.基础级别的时候是在一个n×m单元上玩的.像这样: 每一个单元有包含一个有色点.我们将用不 ...

  3. 搜索专题:Balloons

    搜索专题:Balloons 这道题一看与时间有关,第一想到的就是BFS,定义一个状态,包含每一个状态的剩余气球数,已经进行的时间和每一个志愿者上一次吹气球的时间: 每一次状态转换时,检查是否有没有使用 ...

  4. 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜

    Eight Puzzle Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...

  5. 2014 UESTC暑前集训搜索专题解题报告

    A.解救小Q BFS.每次到达一个状态时看是否是在传送阵的一点上,是则传送到另一点即可. 代码: #include <iostream> #include <cstdio> # ...

  6. 【PHP高效搜索专题(2)】sphinx&coreseek在PHP程序中的应用实例

    PHP可以通过三种途径来调用sphinx 通过Sphinx官方提供的API接口(接口有Python,Java,Php三种版本) 通过安装SphinxSE,然后创建一个中介sphinxSE类型的表,再通 ...

  7. 【PHP高效搜索专题(1)】sphinx&Coreseek的介绍与安装

    我们已经知道mysql中带有"%keyword%"条件的sql是不走索引的,而不走索引的sql在大数据量+大并发量的时候,不仅效率极慢还很有可能让数据库崩溃.那我们如何通过某些关键 ...

  8. 2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索

    邱老师降临小行星 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...

  9. 蓝桥杯dfs搜索专题

    2018激光样式 #include<bits/stdc++.h> using namespace std; /* dfs(i) 第i个激光机器 有两种选择:vis[i-1] == 0 时 ...

随机推荐

  1. js-弹框倒计时三秒后,自动关闭???

    效果: js: //弹出窗,三秒倒计时 countdown(){ //点击发布按钮后,三秒倒计时开始 $(".btn-pub").click(function(){ var cou ...

  2. 51Nod 1433 0和5 (数论 && 被9整除数的特点)

    题意 : 小K手中有n(1~1000)张牌, 每张牌上有一个一位数的数, 这个字数不是0就是5.小K从这些牌在抽出任意张(不能抽0张), 排成一行这样就组成了一个数.使得这个数尽可能大, 而且可以被9 ...

  3. BZOJ 1901 洛谷 P2617 ZOJ 2112 Dynamic Rankings

    以下时空限制来自zoj Time limit 10000 ms Memory limit 32768 kB OS Linux Source Online Contest of Christopher' ...

  4. 【gym102394B】Binary Numbers(DP)

    题意:From https://blog.csdn.net/m0_37809890/article/details/102886956 思路: 可以发现转移就是右上角的一个区间前缀和 std只要开1倍 ...

  5. xwiki安装部署

    环境介绍 http://aiushtha-mybook.stor.sinaapp.com/xwiki/xwiki%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E8%BF%9 ...

  6. Katalon Studio用迅雷快速下载历史版本方法

    一.下载说明 官网正版--历史版本下载地址: https://github.com/katalon-studio/katalon-studio/releases 说明1:这里需要注册账户才可以下载,但 ...

  7. JAVA单例模式的实现伪代码

    什么是单例?:其定义是单例对象的类只能允许一个实例存在 单例的实现基本原理:1.将该类的构造方法定义为私有访问,不对外暴露从而使其他类不能实例化该类对象,只能通过该类的静态方法得到该类的唯一实例 2. ...

  8. 五大 JAVA Web 框架的优缺点对比,Spring MVC 领先

    毫无疑问,Java 是当今世界上最重要的编程语言之一.js 框架给程序员提供了 一个可以构建程序的坚实基础.它包括定义的类和功能,用于硬件设备管理,与系统软件交互并处理输入,让开发人员变得更轻松.Ja ...

  9. 分布式工作流任务调度系统Easy Scheduler正式开源

    分布式工作流任务调度系统Easy Scheduler正式开源 1.背景 在多位技术小伙伴的努力下,经过近2年的研发迭代.内部业务剥离及重构,也经历一批种子用户试用一段时间后,EasyScheduler ...

  10. js fuction函数内return一个内部函数详解

    今天在网上,看到一篇关于js函数难点的文章,js函数的一些难点.在那上面提了一下,关于js函数返回另一个函数的问题,并附上了一道面试题: var add = function(x){ var sum  ...