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. Python开发:模块

    在前面的几个章节中我们脚本上是用 python 解释器来编程,如果你从 Python 解释器退出再进入,那么你定义的所有的方法和变量就都消失了. 为此 Python 提供了一个办法,把这些定义存放在文 ...

  2. nw.js学习地址

    http://blog.sina.com.cn/s/blog_600e56a60102vqj2.html https://github.com/nwjs/nw.js/wiki/Manifest-For ...

  3. 2014 ACM/ICPC Asia Regional 北京 Online

    G - Grade Ted is a employee of Always Cook Mushroom (ACM). His boss Matt gives him a pack of mushroo ...

  4. java第五章 子类与继承

    5.1子类与父类 1   java不支持多重继承,即一个子类不可以从多个父类中同时继承,而C++中可以. 人们习惯地称子类与父类的关系式“is—a”的关系 2   再类的声明过程中,通过关键字exte ...

  5. UTF-8转字典

      NSString *textStr = ] forKey:[string substringToIndex:range.location]];     }];     NSLog(@"% ...

  6. 洛谷P3312 - [SDOI2014]数表

    Portal Solution 共\(T(T\leq2\times10^4)\)组测试数据.给出\(n,m(n,m\leq10^5),a(a\leq10^9)\),求\[ \sum_{i=1}^n\s ...

  7. 【扩展kmp+最小循环节】HDU 4333 Revolving Digits

    http://acm.hdu.edu.cn/showproblem.php?pid=4333 [题意] 给定一个数字<=10^100000,每次将该数的第一位放到放到最后一位,求所有组成的不同的 ...

  8. HashMap构造函数有哪些

    hashMap有4个构造函数: public HashMap(int initialCapacity, float loadFactor) public HashMap(int initialCapa ...

  9. C++ assert 的一点说明

    断言(ASSERT)的用法 转载自http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html 我一直以为assert仅仅是个报错函数 ...

  10. RabbitMQ 简介以及使用场景

    目录 一. RabbitMQ 简介 二. RabbitMQ 使用场景 1. 解耦(为面向服务的架构(SOA)提供基本的最终一致性实现) 2. 异步提升效率 3. 流量削峰 三. 引入消息队列的优缺点 ...