Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class ccc99s4 {
- public static void main(String[] args) throws IOException {
- int n = readInt();
- for (int i=0; i<n; i++) {
- int r = readInt(); //rows
- int c = readInt(); //columns
- int pr = readInt(); //row # of pawn (start)
- int pc = readInt(); //col # of pawn (start)
- int kr = readInt(); //row # of knight (start)
- int kc = readInt(); //col # of knight (start)
- // char grid[][] = new char[r+1][c+1];
- // for (int j=0; j<r; j++) {
- // grid[j] = read().toCharArray();
- // }
- int cnt = 0; //store as moves for win.
- int cntstale = 0; //only update it when the knight is one row above the pawn. Output this ONLY IF pawn reaches top row
- Queue<Integer> QR = new LinkedList<Integer>(); Queue<Integer> QC = new LinkedList<Integer>();
- QR.add(kr); QC.add(kc);
- int CR = QR.poll();
- int CC = QC.poll();
- while (!QR.isEmpty() || QC.isEmpty()) {
- pr++; //PAWN MOVES FIRST
- if (pr==r) {
- if (cntstale!=0) System.out.println("Stalemate in " + cntstale + " knight move(s).");
- else System.out.println("Loss in " + cnt + " knight move(s)."); //loss
- break;
- }
- cnt++;
- if (CR+1<=r && CC-2>=1) {
- QR.add(CR+1);
- QC.add(CC-2);
- }
- if (CR+2<=r && CC-1>=1) {
- QR.add(CR+2);
- QC.add(CC-1);
- }
- if (CR+2<=r && CC+1<=c) {
- QR.add(CR+2);
- QC.add(CC+1);
- }
- if (CR+1<=r && CC+2<=c) {
- QR.add(CR+1);
- QC.add(CC+2);
- }
- if (CR-1>=1 && CC+2<=c) {
- QR.add(CR-1);
- QC.add(CC+2);
- }
- if (CR-2>=1 && CC+1<=c) {
- QR.add(CR-2);
- QC.add(CC+1);
- }
- if (CR-2>=1 && CC-1>=1) {
- QR.add(CR-2);
- QC.add(CC-1);
- }
- if (CR-1>=1 && CC-2>=1) {
- QR.add(CR-1);
- QC.add(CC-2);
- }
- CR = QR.poll();
- CC = QC.poll();
- if (isWin(pr, pc, CR, CC)) {
- System.out.println("Win in " + cnt + " knight move(s).");
- break;
- }
- if (isStalemate(pr, pc, CR, CC)) {
- cntstale = cnt;
- }
- }
- }
- exit();
- }
- static boolean isWin(int prr, int pcc, int krr, int kcc) {
- if (prr==krr && pcc==kcc) {
- return true;
- } else {
- return false;
- }
- }
- static boolean isStalemate(int prr, int pcc, int krr, int kcc) {
- if (krr==prr+1 && pcc==kcc) {
- return true;
- } else {
- return false;
- }
- }
- final private static int BUFFER_SIZE = 1 << 16;
- private static DataInputStream din = new DataInputStream(System.in);
- private static byte[] buffer = new byte[BUFFER_SIZE];
- private static int bufferPointer = 0, bytesRead = 0;
- static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
- public static String readLine() throws IOException {
- byte[] buf = new byte[64]; // line length
- int cnt = 0, c;
- while ((c = Read()) != -1) {
- if (c == '\n')
- break;
- buf[cnt++] = (byte) c;
- }
- return new String(buf, 0, cnt);
- }
- public static String read() throws IOException{
- byte[] ret = new byte[1024];
- int idx = 0;
- byte c = Read();
- while (c <= ' ') {
- c = Read();
- }
- do {
- ret[idx++] = c;
- c = Read();
- } while (c != -1 && c != ' ' && c != '\n' && c != '\r');
- return new String(ret, 0, idx);
- }
- public static int readInt() throws IOException {
- int ret = 0;
- byte c = Read();
- while (c <= ' ')
- c = Read();
- boolean neg = (c == '-');
- if (neg)
- c = Read();
- do {
- ret = ret * 10 + c - '0';
- } while ((c = Read()) >= '0' && c <= '9');
- if (neg)
- return -ret;
- return ret;
- }
- public static long readLong() throws IOException {
- long ret = 0;
- byte c = Read();
- while (c <= ' ')
- c = Read();
- boolean neg = (c == '-');
- if (neg)
- c = Read();
- do {
- ret = ret * 10 + c - '0';
- } while ((c = Read()) >= '0' && c <= '9');
- if (neg)
- return -ret;
- return ret;
- }
- public static double readDouble() throws IOException {
- double ret = 0, div = 1;
- byte c = Read();
- while (c <= ' ')
- c = Read();
- boolean neg = (c == '-');
- if (neg)
- c = Read();
- do {
- ret = ret * 10 + c - '0';
- } while ((c = Read()) >= '0' && c <= '9');
- if (c == '.') {
- while ((c = Read()) >= '0' && c <= '9') {
- ret += (c - '0') / (div *= 10);
- }
- }
- if (neg)
- return -ret;
- return ret;
- }
- private static void fillBuffer() throws IOException {
- bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
- if (bytesRead == -1)
- buffer[0] = -1;
- }
- private static byte Read() throws IOException {
- if (bufferPointer == bytesRead)
- fillBuffer();
- return buffer[bufferPointer++];
- }
- public void close() throws IOException {
- if (din == null)
- return;
- din.close();
- }
- static void print(Object o) {
- pr.print(o);
- }
- static void println(Object o) {
- pr.println(o);
- }
- static void flush() {
- pr.flush();
- }
- static void println() {
- pr.println();
- }
- static void exit() throws IOException {
- din.close();
- pr.close();
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement