【Codeforces】CF3A Shortest path of the king (题解)

CF3A Shortest path of the king


题解:

#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int main() {
	cin>>s1>>s2;
	int x1=s1[0]-'a'+1,y1=s1[1]-'0',x2=s2[0]-'a'+1,y2=s2[1]-'0';
	int dis=max(abs(x1-x2),abs(y1-y2));
	printf("%d\n",dis);
	while(dis--) {
		if(x1>x2 && y1>y2) {
			x1--;
			y1--;
			printf("LD\n");
			continue;
		}
		if(x1>x2 && y1==y2) {
			x1--;
			printf("L\n");
			continue;
		}
		if(x1>x2 && y1<y2) {
			x1--;
			y1++;
			printf("LU\n");
			continue;
		}
		if(x1==x2 && y1>y2) {
			y1--;
			printf("D\n");
			continue;
		}
		if(x1==x2 && y1<y2) {
			y1++;
			printf("U\n");
			continue;
		}
		if(x1<x2 && y1>y2) {
			x1++;
			y1--;
			printf("RD\n");
			continue;
		}
		if(x1<x2 && y1==y2) {
			x1++;
			printf("R\n");
			continue;
		}
		if(x1<x2 && y1<y2) {
			x1++;
			y1++;
			printf("RU\n");
			continue;
		}
	}
}
发布了21 篇原创文章 · 获赞 0 · 访问量 174

猜你喜欢

转载自blog.csdn.net/zhaoweiming2019/article/details/104266149