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. Cocos2d-JS 实现将TiledMap中的每个 tile 变成物理精灵的方法

     How to generate a physics body from a tiledmap  鄙人在网上找了许久都未找到方法,说句实在话,Cocos2d官方给出的与物理引擎相关的内容真的不是很多, ...

  2. 基于深度学习的目标检测技术演进:R-CNN、Fast R-CNN,Faster R-CNN

    基于深度学习的目标检测技术演进:R-CNN.Fast R-CNN,Faster R-CNN object detection我的理解,就是在给定的图片中精确找到物体所在位置,并标注出物体的类别.obj ...

  3. Leetcode 406.根据身高重建队列

    根据身高重建队列 假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列. 注意:总人 ...

  4. 九度oj 题目1030:毕业bg

    题目描述:     每年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上称为“bg”.参加不同团体的bg会有不同的感觉,我们可以用一个非负整数为每个bg定义一个“快乐度”.现给定一个b ...

  5. jQuery中文文档

    http://www.jquery123.com/ http://www.shifone.cc/

  6. 强大!不懂html也来学网抓(xmlhttp/winhttp+fiddler)

    http://club.excelhome.net/thread-1159783-1-1.html 学习html强力推荐此网站:http://www.w3school.com.cn/ ,绝对权威 交流 ...

  7. idea打包SpringBoot项目打包成jar包和war

    - 打包成jar包 1. <groupId>com.squpt.springboot</groupId> <artifactId>springbootdemo< ...

  8. NOJ1203 最多约数问题 [搜索 数论]

    传送门 njczy2010 1203 Accepted 79MS   1400K 2321Byte G++ 2015-01-25 13:14:25.0 最多约数问题 时间限制(普通/Java) : 2 ...

  9. gitweb 搭建教程

    1. 前言 git 是一个版本控制工具,类似svn. 本文内容主要涉及git仓库通过浏览器访问(用web的方式去查看git提交历史记录,tag,branch等信息),即gitweb. 效果图: 在这里 ...

  10. linux内核中预留4M以上大内存的方法

    在内核中, kmalloc能够分配的最大连续内存为2的(MAX_ORDER-1)次方个page(参见alloc_pages函数,     "if (unlikely(order >= ...