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 squaret. As the king is not in habit of wasting his time, he wants to get from his current positions to squaret 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 (froma toh), the second one is a digit from1 to8.
Output
In the first line print n — minimum number of the king's moves. Then inn lines print the moves themselves. Each move is described with one of the 8:L,R,U,D,LU,LD,RU orRD.
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
题意:给出起点和终点,问从起点到终点的最短路径需要走多少步 并输出其中的一组路径 方向(上下左右左上左下右上右下)八个方向~
解题:这道题开始用bfs做 但是由于路径掌握不熟练出现了n多错误 后来左俊鑫想到了直接拿坐标进行求解 同时最短步数就是横纵坐标相减的最大值
( int ans=max(abs(x),abs(y));)然后从起点坐标到终点坐标相减, 先走斜的方向 然后再走斜方向的左移(或者右移后或者上下移动)一步到达终点然后这道题就很容易了 但是我做这道题的时候由于前段时间把大部分精力放在了bfs上 所以出现了方向上的判断失误 所以一直wa
心得: 以后遇到题应先想想有没有简单方便的方法 不能产生思维定式 同时自己的搜索还是不熟练 尤其是bfs存路径的问题一直没有解决 应该加强学习~
代码:
第一个:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
char ch1[4],ch2[4];
cin>>ch1;
cin>>ch2;
int x=(ch2[0]-'a'+1) -(ch1[0]-'a'+1);
int y=ch2[1]-'0'-(ch1[1]-'0');
int ans=max(abs(x),abs(y));// 找到最大的步数
cout<<ans<<endl;
while (x!=0)
{
if (x>0)
{
if (y<0)
{
cout<<"RD"<<endl;
x--;
y++;
}
else if (y>0)
{
cout<<"RU"<<endl;
x--;
y--;
}
else
{
cout<<"R"<<endl;
x--;
}
}
else
{
if (y>0)
{
cout<<"LU"<<endl;
x++;
y--;
}
else if (y<0)
{
cout<<"LD"<<endl;
x++;
y++;
}
else
{
cout<<"L"<<endl;
x++;
}
}
}
while (y!=0)
{
if (y>0)
{
cout<<"U"<<endl;
y--;
}
else
{
cout<<"D"<<endl;
y++;
}
}
return 0;
}
这个是徐坤坤的 拿来学习一下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000;
char st[4],ed[4];
int ans,sx,sy,ex,ey;
int dx[]={1,-1,0,0,1,-1,1,-1};
int dy[]={0,0,-1,1,1,1,-1,-1};
//char st[10][3]={"D","U","L","R","RU","LU","RD","LD"};
char p[maxn][3];
int main()
{
scanf("%s",&st);
scanf("%s",&ed);
ans=0;
int i,j;
sx=st[0]-'a'+1,sy=st[1]-'0';
ex=ed[0]-'a'+1,ey=ed[1]-'0';
while(sx!=sy&&ex!=ey)
{
if(sx<ex&&sy<ey)
{
strcmp(p[ans++],"RU");
}
if(sx<ex&&sy>ey)
{
strcmp(p[ans++],"RD");
}
if(sx<ex&&sy==ey)
{
strcmp(p[ans++],"U");
}
if(sx==ex&&sy<ey)
{
strcmp(p[ans++],"R");
}
if(sx==ex&&sy>ey)
{
strcmp(p[ans++],"L");
}
if(sx==ex&&sy==ey)
{
break;
}
if(sx>ex&&sy==ey)
{
strcmp(p[ans++],"D");
}
if(sx>ex&&sy<ey)
{
strcmp(p[ans++],"LU");
}
if(sx>ex&&sy>ey)
{
strcmp(p[ans++],"LD");
}
}
for(int i=0;i<ans;i++)
{
puts(p[i]);
}
return 0;
}