Crawling in process...Crawling failedTime Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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 1 to 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 Input
a8 h1
7
RD
RD
RD
RD
RD
RD
RD
题意:类似poj2488,给出两个点的坐标,八个方向,问最短的路径。
思路:bfs记录路径。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
int sx,sy,ex,ey;
int path[100];
int dir[8][2]={1,1,1,-1,-1,-1,-1,1,0,-1,1,0,-1,0,0,1};//RU,RD,LD,LU,D,R,L,U
struct node
{
int x;
int y;
int px,py;
int step;
int d;
int visit;
}a[10][10];
int bfs(int n)
{
memset(a,0,sizeof(a));
queue<node> q;
node cur;
a[sx][sy].x=sx;
a[sx][sy].y=sy;
a[sx][sy].step=0;
a[sx][sy].visit=1;
q.push(a[sx][sy]);
int aa=1<<29;
while(!q.empty())
{
cur=q.front();//赋值
q.pop();
if(cur.x==ex&&cur.y==ey)
{
//printf("cur=%d\n",cur.step);
aa=cur.step;
break;
}
for(int i=0;i<8;i++)
{
int tx=cur.x+dir[i][0];
int ty=cur.y+dir[i][1];
if(tx>0&&tx<=n&&ty>0&&ty<=n&&a[tx][ty].visit==0)
{
a[tx][ty].visit=1;
a[tx][ty].px=cur.x;
a[tx][ty].py=cur.y;
a[tx][ty].x=tx;
a[tx][ty].y=ty;
a[tx][ty].step=cur.step+1;
a[tx][ty].d=i;
q.push(a[tx][ty]);
}
}
}
return aa;
}
int main()
{
char s,e;
int n=8;
scanf("%c%d",&s,&sy);
getchar();
sx=(s-'a')+1;
scanf("%c%d",&e,&ey);
getchar();
ex=(e-'a')+1;
int w=bfs(n);
printf("%d\n",w);
if(w==0)
return 0;
int tx=a[ex][ey].px,ty=a[ex][ey].py;
int i=0;
path[i++]=a[ex][ey].d;
int mm,nn;
while(tx!=sx||ty!=sy)
{
//cout<<tx<<" "<<ty<<endl;
path[i++]=a[tx][ty].d;
mm=a[tx][ty].px;
nn=a[tx][ty].py;
tx=mm;
ty=nn;
}
for(int j=i-1;j>=0;j--)
{
switch(path[j])
{
case 0://RU,RD,LD,LU,D,R,L,U
printf("RU\n");
break;
case 1:
printf("RD\n");
break;
case 2:
printf("LD\n");
break;
case 3:
printf("LU\n");
break;
case 4:
printf("D\n");
break;
case 5:
printf("R\n");
break;
case 6:
printf("L\n");
break;
case 7:
printf("U\n");
break;
}
}
return 0;
}