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. Leetcode 365.水壶问题

    水壶问题 有两个容量分别为 x升和 y升的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升的水? 如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水. 你允许: 装满 ...

  2. VOC Segmentation GT图像颜色表生成分析

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/52185581 PASCAL VOC图像 ...

  3. 有关git的配置

    git版本控制器总结 关于部分内容请参考:https://www.cnblogs.com/smuxiaolei/p/7484678.htmlgit是一个版本控制器,分布式管理:可以记录每次文件的改动, ...

  4. vamare下centos7.0 动态获取ip报错问题

    CentOS7 Failed to start LSB: Bring up/down解决方法 centos7.0中service network restart重启报错的问题 报错信息: /etc/i ...

  5. oracle用户密码错误导致用户锁定

    解决方法:使用DBA用户将其解锁: SQL> alter user ecology account unlock; 用户已更改. 用户密码限制设置: 查看FAILED_LOGIN_ATTEMPT ...

  6. sublime text2-text3 定义的不同浏览器的预览快捷键

    sublime text3 自己定义的不同浏览器的预览快捷键突然全部失效了,搞到现在一直没闹清楚怎么回事,翻看插件发现SideBarEnhancements这插件刚更新了,快捷键也是依赖这个插件弄得. ...

  7. BZOJ 2440 [中山市选2011]完全平方数 ——莫比乌斯函数

    $\sum_{i=1}^n[i==d^2*p]$ 其中p无平方因子$=\sum_{d^2\mid n,d>=2}\sum_{i=1}^{\lfloor {n/d^2} \rfloor} \lef ...

  8. poj3180 The Cow Prom

    The Cow Prom Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2373   Accepted: 1402 Desc ...

  9. Java线程池快速学习教程

    1. Java线程池 线程池:顾名思义,用一个池子装载多个线程,使用池子去管理多个线程. 问题来源:应用大量通过new Thread()方法创建执行时间短的线程,较大的消耗系统资源并且系统的响应速度变 ...

  10. Mountaineers

    Mountaineers 时间限制: 3 Sec  内存限制: 128 MB 题目描述 The Chilean Andes have become increasingly popular as a ...