A. Shortest path of the king
time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard output

The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, because he has business of national importance. For example, he has to pay an official visit to square t.
As the king is not in habit of wasting his time, he wants to get from his current position s to square t in
the least number of moves. Help him to do this.

In one move the king can get to the square that has a common side or a common vertex with the square the king is currently in (generally there are 8 different squares he can move to).

Input

The first line contains the chessboard coordinates of square s, the second line — of square t.

Chessboard coordinates consist of two characters, the first one is a lowercase Latin letter (from a to h),
the second one is a digit from 1to 8.

Output

In the first line print n — minimum number of the king's moves. Then in n lines
print the moves themselves. Each move is described with one of the 8: L, R, U, D, LU, LD, RU or RD.

L, R, U, D stand
respectively for moves left, right, up and down (according to the picture), and 2-letter combinations stand for diagonal moves. If the answer is not unique, print any of them.

Sample test(s)
input
a8
h1
output
7
RD
RD
RD
RD
RD
RD
RD
题意:8*8的棋盘 给出起点和终点,求最短路径。

能够走8个方向。
对于打印路径.对一个出队节点x进行搜索的时候将x的每个未訪问子节点的前驱记为x并入队。这样就能通过訪问前驱数组得到逆序的路径.
#include <cstring>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
struct node
{
int x,y,step;
}path[10][10];
char s[5],e[5];
bool vis[10][10];
int dir[8][2]={{0,1},{0,-1},{-1,0},{1,0},{-1,1},{-1,-1},{1,1},{1,-1}};
void bfs()
{
queue <node> Q;
node t,f;
t.x=s[0]-'a'+1;t.y=s[1]-'0';t.step=0;
vis[t.x][t.y]=1;
Q.push(t);
while(!Q.empty())
{
f=Q.front();Q.pop();
if(f.x==(e[0]-'a'+1)&&f.y==(e[1]-'0'))
{
printf("%d\n",f.step);
stack <int> S;
while(path[f.x][f.y].x!=-1&&path[f.x][f.y].y!=-1)
{
S.push(path[f.x][f.y].step);
f.x=path[f.x][f.y].x;
f.y=path[f.x][f.y].y;
}
while(!S.empty())
{
int tem=S.top();
S.pop();
switch (tem)
{
case 0: puts("U");break;
case 1: puts("D");break;
case 2: puts("L");break;
case 3: puts("R");break;
case 4: puts("LU");break;
case 5: puts("LD");break;
case 6: puts("RU");break;
case 7: puts("RD");break;
}
}
return ;
}
for(int i=0;i<8;i++)
{
t.x=f.x+dir[i][0];
t.y=f.y+dir[i][1];
if(t.x>=1&&t.x<=8&&t.y>=1&&t.y<=8&&!vis[t.x][t.y])
{
path[t.x][t.y].x=f.x;
path[t.x][t.y].y=f.y;
path[t.x][t.y].step=i;
t.step=f.step+1;
vis[t.x][t.y]=1;
Q.push(t);
}
}
}
}
int main()
{
while(scanf("%s %s",s,e)!=EOF)
{
memset(path,-1,sizeof(path));
memset(vis,0,sizeof(vis));
bfs();
}
return 0;
}

Codeforces 3A-Shortest path of the king(BFS打印路径)的更多相关文章

  1. node搜索codeforces 3A - Shortest path of the king

    发一下牢骚和主题无关: 搜索,最短路都可以     每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...

  2. 3A. Shortest path of the king

    给你一个的棋盘, 问:从一个坐标到达另一个坐标需要多少步? 每次移动可以是八个方向.   #include <iostream> #include <cmath> #inclu ...

  3. Codeforces Beta Round #3 A. Shortest path of the king 水题

    A. Shortest path of the king 题目连接: http://www.codeforces.com/contest/3/problem/A Description The kin ...

  4. Codeforces-A. Shortest path of the king(简单bfs记录路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  5. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  6. BFS+打印路径

    题目是给你起点sx,和终点gx:牛在起点可以进行下面两个操作: 步行:John花一分钟由任意点X移动到点X-1或点X+1. 瞬移:John花一分钟由任意点X移动到点2*X. 你要输出最短步数及打印路径 ...

  7. Codeforces Beta Round #3 A. Shortest path of the king

    标题效果: 鉴于国际棋盘两点,寻求同意的操作,是什么操作的最小数量,在操作过程中输出. 解题思路: 水题一个,见代码. 以下是代码: #include <set> #include < ...

  8. A - Shortest path of the king (棋盘)

    The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, becaus ...

  9. Shortest path of the king

    必须要抄袭一下这个代码 The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose h ...

随机推荐

  1. 获取class

    使用原生JavaScript,获取类操作符时:即使使用getElementByClassName,在Firefox和IE9以下是不兼容的.Firefox下是可以用它获取的到元素而IE不行,一般框架都会 ...

  2. [工具使用] visualvm 通过jmx不能连接

    远程服务器,通常配置下jmx,然后用visualvm连接然后监控. 但昨天自己的一台测试服务器上,正确配置了jmx还是不能连接上去. 后来参考了 https://bjddd192.github.io/ ...

  3. POJ 3469 Dual Core CPU ——网络流

    [题目分析] 构造一个最小割的模型. S向每一个点连Ai,每一个点向T连Bi. 对于每一个限制条件,在i和j之间连一条Cij的双向边即可. 然后求出最小割就是最少的花费. 验证最小割的合理性很容易. ...

  4. BZOJ2561 最小生成树 【最小割】

    题目 给定一个边带正权的连通无向图G=(V,E),其中N=|V|,M=|E|,N个点从1到N依次编号,给定三个正整数u,v,和L (u≠v),假设现在加入一条边权为L的边(u,v),那么需要删掉最少多 ...

  5. leetcode 349 map

    只需要用map来标记1,今儿通过map的值来得到重叠的部分 class Solution { public: vector<int> intersection(vector<int& ...

  6. Linux 下测试磁盘读写 I/O 速度的方法汇总

    在分布式异构存储系统中,我们经常会需要测量获取不同节点中硬盘/磁盘的读写 I/O 速度,下面是 Linux 系统下一些常用测试方法(之后不定期更新): 1.使用 hdparm 命令这是一个是用来获取A ...

  7. bat常用命令,转【http://www.cnblogs.com/yplong/archive/2013/04/02/2996550.html】

    1.@它的作用是隐藏它后面这一行的命令本身(只能影响当前行).2.echo中文为“反馈”.“回显”的意思.它其实是一个开关命令,就是说它只有两种状态:打开和关闭.于是就有了echo on和echo o ...

  8. mongodb的安装及环境配置

    一 下载 官网:https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.6.3-signed.msi 官网可能打不开, ...

  9. GridView选中,编辑,取消,删除代码

    原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 2.GridView选中,编辑,取消,删除: 效果图: 后台代码:你可以使用sqlhelper,本文没用。代码如下 ...

  10. C# 判断上传图片是否被PS修改过的方法

    今天在网上发现一个判断图片是否被Photoshop修改过的方法.发现还不错,呵呵.摘录下来. 讲下基本的原理:其实每张被photoshop修改过的图片都会有Adobe Photoshop这样的字样在图 ...