Codeforces Beta Round #3

http://codeforces.com/contest/3/problem/A
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.

Codeforces Beta Round 3 - 风未定 - NGUNAUJ

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: LRUDLULDRU or RD.

LRUD 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
题目大意:给定起点 终点 求出从起点到终点的最短距离(最少步数),并输出走法
解题思路BFS 记录方向
/* ***********************************************
Author :
Created Time :2015/5/31 0:26:26
File Name :3.cpp
************************************************ */

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
struct node{
int x,y;
int dis;
};
bool cmp(int a,int b){
return a>b;
}
int dir[8][2]={-1,0,1,0,0,1,0,-1,-1,1,-1,-1,1,1,1,-1};
int vis[9][9];
char ss[8][5]={"L","R","U","D","LU","LD","RU","RD"};
int pre[11][11];
node s,e;
void print(int x,int y){
if(x==s.x&&y==s.y)return ;
int t=pre[x][y];
print(x-dir[t][0],y-dir[t][1]);
printf("%s\n",ss[t]);
}
void bfs(node s,node e){
node u;
u.x=s.x;
u.y=s.y;
queue<node>q;
q.push(u);
cle(vis);
while(!q.empty()){
node u=q.front();q.pop();
if(u.x==e.x&&u.y==e.y){
cout<<u.dis<<endl;
print(e.x,e.y);
break;
}
for(int i=0;i<8;i++){
node v;
v.x=u.x+dir[i][0];
v.y=u.y+dir[i][1];
if(!vis[v.x][v.y]&&v.x<=8&&v.y<=8&&v.x>=1&&v.y>=1){
v.dis=u.dis+1;
q.push(v);
vis[v.x][v.y]=1;
pre[v.x][v.y]=i;//方向
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
string s1,s2;
while(cin>>s1>>s2){
int x1=(int)(s1[0]-'a')+1;
int y1=(int)(s1[1]-'0');
int x2=(int)(s2[0]-'a')+1;
int y2=(int)(s2[1]-'0');
//cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;
s.x=x1,s.y=y1;
e.x=x2,e.y=y2;
bfs(s,e);
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值