http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1101

题目描述

小明很喜欢下国际象棋,一天,他拿着国际象棋中的“马”时突然想到一个问题:
给定两个棋盘上的方格a和b,马从a跳到b最少需要多少步?
现请你编程解决这个问题。

提示:国际象棋棋盘为8格*8格,马的走子规则为,每步棋先横走或直走一格,然后再往外斜走一格。

输入格式

输入包含多组测试数据。每组输入由两个方格组成,每个方格包含一个小写字母(a~h),表示棋盘的列号,和一个整数(1~8),表示棋盘的行号。

输出

对于每组输入,输出一行“To get from xx to yy takes n knight moves.”。

样例输入

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

样例输出

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.

思路:

简单的BFS,只要弄清楚马的走法就可以了,一开始字符数组开小了,蛋疼,以后果断要大方点啊

#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std; struct node
{
int x,y,step;
}; int vis[8][8];
int sx,sy,ex,ey,ans;
int to[8][2]={-1,-2,-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2}; int check(int x,int y)
{
if(x<0 || y<0 || x>=8 || y>=8)
return 1;
if(vis[x][y])
return 1;
return 0;
} void bfs()
{
int i;
queue<node> Q;
node a,next;
a.x = sx;
a.y = sy;
a.step = 0;
vis[sx][sy] = 1;
Q.push(a);
while(!Q.empty())
{
a = Q.front();
Q.pop();
if(a.x == ex && a.y == ey)
{
ans = a.step;
return ;
}
for(i = 0;i<8;i++)
{
next = a;
next.x+=to[i][0];
next.y+=to[i][1];
if(check(next.x,next.y))
continue;
next.step = a.step+1;
vis[next.x][next.y] = 1;
Q.push(next);
}
}
return ;
} int main()
{
char ch1[10],ch2[10];
while(~scanf("%s%s",ch1,ch2))
{
sx = ch1[0]-'a';
sy = ch1[1]-'1';
ex = ch2[0]-'a';
ey = ch2[1]-'1';
memset(vis,0,sizeof(vis));
bfs();
printf("To get from %s to %s takes %d knight moves.\n",ch1,ch2,ans);
} return 0;
}

HNCU1101:马的移动(BFS)的更多相关文章

  1. PythonTip--一马当先--bfs

    刚学python,小试牛刀 一马当先 讨论此题 | 解题报告 顶(39) (AC/Submit)Ratio(477|1829)26.08% 踩(1) 描述: 下过象棋的人都知道,马只能走'日'字形(包 ...

  2. 马的移动(BFS) 详细注释 一个具有情怀的题目

    题目描述 小明很喜欢下国际象棋,一天,他拿着国际象棋中的"马"时突然想到一个问题: 给定两个棋盘上的方格a和b,马从a跳到b最少需要多少步? 现请你编程解决这个问题. 提示:国际象 ...

  3. F: Horse Pro 马走棋盘 BFS

    F: Horse Pro 豆豆也已经开始学着玩象棋了,现在豆豆已经搞清楚马的走法了,但是豆豆不能确定能否在 100 步以内从一个点到达另一个点(假设棋盘无限大). Input 第一行输入两个整数 x1 ...

  4. 洛谷 - P1443 - 马的遍历 - bfs

    略有收获的bfs,使用了try_enqueue函数使得加入队列非常方便.性能理论上是一样的因为是inline? 还有就是左对齐是使用%-4d,相对于右对齐的%4d,还有右对齐前导零的%04d,自己试一 ...

  5. Wannafly Camp 2020 Day 6I 你吓到我的马了.jpg - BFS

    暴力BFS即可 #include <bits/stdc++.h> using namespace std; int n,m,f[105][105]; char s[105][105]; s ...

  6. POJ 1915 经典马步 双向bfs

    拿这个经典题目开刀...........可是双向时间优势在这题上的效果不太明显 #include <iostream> #include <algorithm> #includ ...

  7. 洛谷1443 马的遍历【bfs】

    题目链接:https://www.luogu.org/problemnew/show/P1443 题意: 给一个n*m的棋盘,马在上面走(规则就是象棋中的规则,详细见代码dx,dy数组定义) 问棋盘上 ...

  8. 洛谷P1443 马的遍历(bfs,注意输出格式)

    题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 输入输出格式 输入格式: 一行四个数据,棋盘的大小和马的坐标 输出 ...

  9. 洛谷P1443 马的遍历【BFS】

    题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 输入输出格式 输入格式: 一行四个数据,棋盘的大小和马的坐标 输出 ...

随机推荐

  1. html submit 登录

    <!doctype html> <html lang="en"> <head> <meta name="Generator&qu ...

  2. hdu1172猜数字

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1172 题目 猜数字 Time Limit: 20000/10000 MS (Java/Others) ...

  3. TTimerThread和TThreadedTimer(都是通过WaitForSingleObject和CreateEvent来实现的)

    //////////////////////////////////////////////////// // // // ThreadedTimer 1.24 // // // // Copyrig ...

  4. ajax终结篇

    Ajax中post和get的区别 在ajax中有这个方法 xmlreq.open("post","servlet/MyServlet?time="+newDat ...

  5. Atitit.dwr3 不能显示错误具体信息的解决方式,控件显示错误具体信息的解决方式 java .net php

    Atitit.dwr3 不能显示错误具体信息的解决方式,控件显示错误具体信息的解决方式 java .net php 1. Keyword/subtitle 1 2. 使用dwr3的异常convert处 ...

  6. MSSQL - 因为数据库正在使用,所以无法获得对数据库的独占访问权。

    关于“因为数据库正在使用,所以无法获得对数据库的独占访问权”的最终解决方案   今天在使用SQL Server2005对某个数据库进行还原操作的时候,出现了如上问题,“因为数据库正在使用,所以无法获得 ...

  7. net core VS goang web

    asp.net core VS goang web[修正篇] 先前写过一篇文章:http://www.cnblogs.com/gengzhe/p/5557789.html,也是asp.net core ...

  8. delphi中覆盖最大化消息(覆盖WM_GETMINMAXINFO消息)

    unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; ...

  9. Unknown database 'DB_NAME'

    Cannot create PoolableConnectionFactory (Unknown database 'DB_NAME'): com.mysql.jdbc.exceptions.jdbc ...

  10. 弹出框weeboxs 基本属性总结

    使用前需包含以下jquery.js.bgiframe.js.weebox.js文件 boxid: null, //设定了此值只后,以后在打开同样boxid的弹窗时,前一个将被自 动关闭 boxclas ...