您的位置:首页 > 其它

CF3A. Shortest path of the king

2014-03-29 11:58 447 查看
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

这个世界大牛真太多了......


题目大意:从起点走到终点,求最短距离,而且写出最短距离的路径是什么样的:


只不过要把y轴翻转一下,就可以了,第一思路是bfs,但是老是不对,看了一下大神的bfs代码:

#include <iostream>
#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
int move[8][2]= {{-1,0},{1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
//             左 L    右  R 下  D  上 U    右下 RD  左下  LD  右上RU    左上LU
char mo[8][3]= {'L','R','D','U','RD','LD','RU','LU'};
int ex,ey,sx,sy;
struct node
{
int x,y;
int vis;
int depth;
int shx,shy;
int m;
} pan[10][10];
int bfs()
{
int i,j,k;
queue<node> q;//队列先进先出
q.push(pan[sx][sy]);//在队尾插入一个元素
if(sx==ex&&sy==ey)
return 0;
pan[sx][sy].vis=1;//标记起点为1
while(!q.empty())
{
node p;
int tx,ty,yx,yy;
p=q.front();//起点
q.pop();//删除起点
for(i=0; i<8; i++)
{
node t;
t=p;
tx=p.x+move[i][0];
ty=p.y+move[i][1];//遍历
//cout<<tx<<"df"<<ty<<endl;
if(tx<0||tx>=8||ty<0||ty>=8)
continue;
if(pan[tx][ty].vis==0)
{
pan[tx][ty].shx=p.x;
pan[tx][ty].shy=p.y;
pan[tx][ty].depth=p.depth+1;
q.push(pan[tx][ty]);//在队尾插入一个元素
pan[tx][ty].vis=1;
pan[tx][ty].m=i;//标记走的路线,是L,R.....
//cout<<pan[tx][ty].shx<<" "<<pan[tx][ty].shy<<" "<<pan[tx][ty].depth<<" "<<pan[tx][ty].m<<endl;
if(tx==ex&&ty==ey)
return pan[tx][ty].depth;
}
}
}
return 0;
}
int main()
{
int i,j,k,l,m,n;
memset(pan,0,sizeof(pan));
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
pan[i][j].x=i;
pan[i][j].y=j;
}
}
char z;
int v;
scanf("%c%d",&z,&v);//先y(字母)后x
sx=z-'a';
sy=8-v;//起点
//cout<<sx<<" "<<sy<<endl;
getchar();
scanf("%c%d",&z,&v);
ex=z-'a';
ey=8-v;//终点
//cout<<ey<<" "<<ex<<endl;
m=bfs();
printf("%d\n",m);
if(m==0)
return 0;
int path[100];
memset(path,0,sizeof(path));
j=0;
path[j++]=pan[ex][ey].m;
int qx,qy;
qx=pan[ex][ey].shx;
qy=pan[ex][ey].shy;
while(qx!=sx||qy!=sy)
{
int tx,ty;
tx=qx;
ty=qy;
qx=pan[tx][ty].shx;
qy=pan[tx][ty].shy;
path[j++]=pan[tx][ty].m;
//printf("%d\n",p->m);
}
for(i=j-1; i>=0; i--)
{
int k=path[i];
if(k==0)
printf("L\n");
else if(k==1)
printf("R\n");
else if(k==2)
printf("D\n");
else if(k==3)
printf("U\n");
else if(k==4)
printf("RD\n");
else if(k==5)
printf("LD\n");
else if(k==6)
printf("RU\n");
else if(k==7)
printf("LU\n");
}
return 0;
}

后来在一个牛人的博客里看到了特吐血的代码:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
string a,b;
while(cin>>a>>b)
{
int m = abs(a[0]-b[0]);
int n = abs(a[1]-b[1]);
cout<<max(m,n)<<endl;
while(a!=b)
{
if(a[0] > b[0]) {cout<<"L";a[0] --;}
if(a[0] < b[0]) {cout<<"R";a[0] ++;}
if(a[1] > b[1]) {cout<<"D";a[1] --;}
if(a[1] < b[1]) {cout<<"U";a[1] ++;}
cout<<endl;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: