题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=380
样例输入:
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做比较好。
求步数时, 有个小技巧, 开个vis数组,初始化为0, 然后这个用来记录走的步数,而不仅仅是用来标记是否走过。具体见代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char start[3], end[3];
int dir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},
{2,-1},{1,-2},{-1,-2},{-2,-1}};
int vis[10][10];
struct Node{int x, y; };
Node que[1000];
void bfs(){
int front=0, rear=1;
que[0].x=start[0]-'a', que[0].y=start[1]-'0'-1;
vis[que[0].x][que[0].y] = 1;
while(front<rear){
Node t = que[front++];
// 如果遇到满足条件的,直接输出
if(t.x==end[0]-'a' && t.y==end[1]-'0'-1){
printf("To get from %s to %s takes %d knight moves.\n", start,end,vis[t.x][t.y]-1);
return ;
}
for(int i=0; i<8; ++i){
int dx=t.x+dir[i][0], dy=t.y+dir[i][1];
if(dx>=0 && dx<8 && dy>=0 && dy<8 && !vis[dx][dy]){
vis[dx][dy] = vis[t.x][t.y]+1; // 记住步数
Node temp;
temp.x=dx, temp.y=dy;
que[rear++] = temp;
}
}
}
}
int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
while(~scanf("%s %s", start, end) && start[0]){
memset(vis, 0, sizeof(vis));
bfs();
}
return 0;
}
分享到:
相关推荐
北大 acm JudgeOnline poj1915 Knight Moves c++源代码
Your task is to write a program to calculate the minimum number of moves移动次数 needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov. ...
moves = { { 2 , 1 }, { 1 , 2 }, { - 1 , 2 }, { - 2 , 1 }, { - 2 , - 1 }, { - 1 , - 2 }, { 1 , - 2 }, { 2 , - 1 }}; Queue q = new LinkedList<> (); q . add( new int []{ 0 , 0 }); Set< String > ...
因子:因子也叫因数,例如3*5=15,那么3和5是15的因子。同时15*1=15,那么1和15也是15的因子。 1,3,5,15 这四个因子是15的所有因子。 完数:如果一个数等于不含它本身的其他因子之和,则称该数为‘完数’。...
该题求解从一个坐标到达另一个坐标的最短步数,移动规则需要按照题目给的方式来移动,即按照国际象棋马的走法一致。
engine-value_moves.c
资源分类:Python库 所属语言:Python 资源全名:peewee-moves-1.6.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Running-MOVES-2014b-vs-3
knightmoves
$----------------------- RAPID MOVES ---------------------------------------- $20 Rapid Move in XY (MILL/ROUTER/FLAME/LASER) or XZ (LATHE) only G0 X[AX] Y[AY] $IF CHANGE = 0 G0 G43 H[OFS] Z[AZ] $...
微型虚拟机命令: LOAD -- Loads value to R0MOVE_FROM_MEM_TO_REG (Rx),Ry -- Moves the content of (Rx) to RyMOVE_FROM_REG_TO_MEM Rx,(Ry) -- Moves the content of Rx to (Ry)MOVE_FROM_REG_TO_REG Rx,Ry -- ...
TV-MOVES
宝元系统alphacam 后处理 ...$----------------------- RAPID MOVES ---------------------------------------- $20 Rapid Move in XY (MILL/ROUTER/FLAME/LASER) or XZ (LATHE) only G00 X[AX] Y[AY] $IF CHANGE = 0
You have been provided with a third-party library "ChessLib" which calculates the legal moves a knight can make given a position on an 8 by 8 board. The library has been used to create a program which...
# Matlab Wrapper for Fusion Moves (QPBO + alpha-expansion) 作者:Sarun Gulyanon 24.04.2017 --------------------- 描述--------------------- Fusion Moves 是一种基于 QPBO 和 alpha-expansion 的方法,用于...
vbucket-moves.py 是一个分析来自 Couchbase Server 的主事件文件的工具。 主事件文件显示重新平衡事件,可以从。 您需要进行身份验证才能获取文件。 此工具的输出是 vbucket 移动的甘特图,显示移动每个 vbucket 所...
特征Z-Moves Bot具有许多有用而强大的功能。首先,僵尸程序是持久性的,这意味着该僵尸程序具有状态,因为您可以将截止日期和链接添加到数据库中并随时间进行保存。该机器人提供了一些功能:通知事项截止期限链接...
StreetFighterMoves·
Like any good programming book, Eloquent JavaScript begins with fundamentals--variables, control structures, functions, and data structures--then moves on to complex topics like object-oriented ...
移动调制 v0.5 轻量级网络应用程序使用来自 Moves 应用程序 ( ) 的数据来显示自安装 Moves 以来的步行(以及很快跑步和骑自行车)的图表,并为接下来的几天提出建议。 要设置实例,需要移动开发者帐户( ) 重定向 ...