Question about Shortest path of the king

I all, I need help for a problem. This is not homework.
the question is from here: http://codeforces.com/problemset/problem/3/A
I need the result to be like this:
Sample test(s)
Input

a8
h1

Output

7
RD
RD
RD
RD
RD
RD
RD
But I don't know how.
This is what I've done so far
from.txt:
a8
h1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 #include <fstream>
#include <string>
#include<iostream>
using namespace std;

int main()
{
	fstream fin;
	fin.open("from.txt");
	char start[2];
	fin >> start[0];
	fin >> start[1];
	char end[2];
	fin >> end[0];
	fin >> end[1];

	int x = end[0] - start[0];
	int y = end[1] - start[1];
	int numMoves = 0;
	string listOfMoves = "";
	while (x != 0 || y != 0)
	{
		numMoves++;// every time go to loop, add 1 same with +=1, if put in every if it will count 1 move for every if. in here, for 2 moves when diagonal, it count 1 move.
		if ((end[0] - start[0]) > 0)
		{
			listOfMoves += "R";
			x -= 1;
		}
		else if ((end[0] - start[0]) < 0)
		{
			listOfMoves += "L";
			x += 1;
		}
		else
		{
			listOfMoves += "";
		}
		if ((end[1] - start[1]) > 0)
		{
			listOfMoves += "U";
			y -= 1;
		}
		else if ((end[1] - start[1]) < 0)
		{
			listOfMoves += "D";
			y += 1;
		}
		else
		{
			listOfMoves += "";
		}
	}
	cout << numMoves << endl;
	for (int i = 0; i < listOfMoves.size(); i++)
	{
		cout << listOfMoves[i] << endl;
	}

	fin.close();
	system("pause");
	return 0;
}
Last edited on
You should ask a specific question.
This is not homework.

You try to learn by doing yourself. That is homework.

Think about max( abs(x), abs(y) ) and min( abs(x), abs(y) )
The s and t define a rectangle that can be divided into a square and a smaller rectangle.
Traverse the square like a bishop and the rest like a rook.

The a8-h1 example is a square.
Topic archived. No new replies allowed.