Skip to content

Instantly share code, notes, and snippets.

@horace-velmont
Created May 24, 2019 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horace-velmont/edee961b3170d9e0a7964df60107f046 to your computer and use it in GitHub Desktop.
Save horace-velmont/edee961b3170d9e0a7964df60107f046 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
public static boolean[][] box;
public static List<Point> goList;
public static void main(String[] args) {
Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int T = scan.nextInt();
int[] rList = new int[T];
int[] cList = new int[T];
for (int t = 0; t < T; t++) {
rList[t] = scan.nextInt();
cList[t] = scan.nextInt();
}
for (int t = 0; t < T; t++) {
goList = new ArrayList<>();
int R = rList[t];
int C = cList[t];
boolean isSwap = false;
if (R > C) {
isSwap = true;
int temp;
temp = R;
R = C;
C = temp;
}
boolean isPossible = isPossible(R, C) && isPossible(C, R);
if (!isPossible) {
System.out.println("Case #" + (t + 1) + ": IMPOSSIBLE");
continue;
} else {
System.out.println("Case #" + (t + 1) + ": POSSIBLE");
}
box = new boolean[R + 1][C + 1];
int i = 2, j = 3;
while (i <= R) {
while (j <= C) {
checkNextRow(i, j);
j++;
}
i++;
j = 3;
}
goList.add(new Point(R, 1));
goList.add(new Point(1, C));
goList.add(new Point(R, 2));
goList.add(new Point(1, C - 1));
for (Point go : goList) {
if (isSwap) {
System.out.println(go.y + " " + go.x);
} else {
System.out.println(go.x + " " + go.y);
}
}
}
}
public static boolean isPossible(int i, int j) {
if (i < 2 || j < 2) {
return false;
}
if (i == 2) {
if (j < 5) {
return false;
}
}
if (i == 3) {
if (j < 4) {
return false;
}
}
return true;
}
public static void checkNextRow(int i, int j) {
if (i > 2 && j > 3 && !box[i][j] && !box[i - 1][i - 2]) {
box[i][j] = true;
box[i - 1][j - 2] = true;
}
goList.add(new Point(i, j));
goList.add(new Point(i - 1, j - 2));
}
static class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment