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. webservice 测试地址

    腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx Disco: http:// ...

  2. Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring

    D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  3. .NET重构(一):抽象工厂模式实现登录

    导读:一路艰辛,我也走到了重构.在重构之前,师傅让用经典三层(UI.BLL.DAL)敲了登录.用户的增删改查,共五条线.从开始对三层的朦胧,到五条线结束,终于对三层有了逻辑上清晰的理解.然后就画了几天 ...

  4. Android Email check 正则表达式

    Android Email check 正则表达式 (?:[-!#-\\'*+\\x2f-9=?A-Z^-~]+(?:\\.[-!#-\\'*+\\x2f-9=?A-Z^-~]+)*|\"( ...

  5. 使用ANT将Android打包成Jar包

    本文主要实现使用ANT,将Android项目打包成jar,为方便其他项目使用. ANT可以去官网下载(http://ant.apache.org/) 先介绍打包的步骤,打包脚本下方贴出 步骤: 1,将 ...

  6. BZOJ 3196 二逼平衡树 ——树套树

    [题目分析] 全靠运气,卡空间. xjb试几次就过了. [代码] #include <cmath> #include <cstdio> #include <cstring ...

  7. 算法复习——cdq分治

    题目: Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要 ...

  8. 算法复习——splay+启发式合并(bzoj2733-永无乡)

    题目: Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通 ...

  9. Lucas 卢卡斯定理

    Lucas: 卢卡斯定理说白了只有一条性质 $$ C^n_m \equiv C^{n/p}_{m/p} \times C^{n \bmod p}_{m \bmod p} \ (mod \ \ p) $ ...

  10. SpringBoot项目整合Druid进行统计监控

    0.druid介绍,参考官网 1.在项目的POM文件中添加alibaba的druid依赖 <dependency> <groupId>com.alibaba</group ...