Skip to content

Instantly share code, notes, and snippets.

@JorgeOlvera
Created December 2, 2013 06:21
Show Gist options
  • Save JorgeOlvera/7745856 to your computer and use it in GitHub Desktop.
Save JorgeOlvera/7745856 to your computer and use it in GitHub Desktop.
TraversingGrid
import java.io.*;
import java.util.Scanner;
public class TraversingGrid {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
int testcases = input.nextInt();
do {for (int i = 0; i < testcases; i++) {
int rows = input.nextInt();
int columns = input.nextInt();
if (rows == columns && rows > 1) {
if (rows % 2== 0) System.out.println("L");
else System.out.println("R");
}
else if (rows > columns && columns > 1) {
if (columns % 2 == 0) System.out.println("U");
else System.out.println("D");
}
else if (columns > rows) {
if (rows % 2 == 0) System.out.println("L");
else System.out.println("R");
}
else if (columns == 1) {
if (rows == 1) System.out.println("R");
else System.out.println("D");
}
}
} while (testcases >= 1 && testcases <= 1000);
}
}
/*1004 - Traversing Grid Description Starting at the top left corner
of an N x M grid and facing towards the right, you keep walking one
square at a time in the direction you are facing. If you reach the
boundary of the grid or if the next square you are about to
visit has already been visited, you turn right. You stop
when all the squares in the grid have been visited.
What direction will you be facing when you
stop? For example: Consider the case
with N = 3, M = 3. The path followed will be
(0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (2,1) -> (2,0) -> (1,0) -> (1,1).
At this point, all squares have been visited, and you are facing right.
The first line contains T the number of test cases.
Each of the next T lines contain two integers N and M, denoting the number of rows and columns respectively.
Output specification Output T lines, one for each test case, containing the required direction you will be
facing at the end.
Output L for left, R for right, U for up, and D for down. 1 <= T <= 5000, 1 <= N,M <= 10^9.*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment