题意:有一个棋盘,它的横坐标用a~h表示,纵坐标用1~8表示,现给你一个起点坐标,和一个终点坐标,求步数以及路径,每个格子可以往相邻的8个格子移动
解题思路:其实这题还是比较好做的,暂时假设起点坐标为(x1,y1),终点坐标为(x2,y2),那么步数就是max(|x2-x1|,|y2-y1|)
而对于路径,也好办,只要每步判断一下是否已经达到终点就可以了,代码还是比较好理解的
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 1001;
const int inf = 1000000000;
const int mod = 2009;
int main()
{
int b,d;
char a[5],c[5];
scanf("%s",a);
scanf("%s",c);
b=a[1]-'0';
d=c[1]-'0';
printf("%d\n",max(abs(b-d),abs(a[0]-c[0])));
while(b!=d||a[0]!=c[0])
{
if(c[0]>a[0])
{
printf("R");
a[0]++;
}
if(c[0]<a[0])
{
printf("L");
a[0]--;
}
if(b>d)
{
printf("D");
b--;
}
if(b<d)
{
printf("U");
b++;
}
puts("");
}
return 0;
}
菜鸟成长记