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
#include <fstream>
#include <string>
#include<iostream>
usingnamespace 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;
}
elseif ((end[0] - start[0]) < 0)
{
listOfMoves += "L";
x += 1;
}
else
{
listOfMoves += "";
}
if ((end[1] - start[1]) > 0)
{
listOfMoves += "U";
y -= 1;
}
elseif ((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;
}
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.