Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class ccc99s4 {
  4. public static void main(String[] args) throws IOException {
  5.  
  6. int n = readInt();
  7.  
  8. for (int i=0; i<n; i++) {
  9. int r = readInt(); //rows
  10. int c = readInt(); //columns
  11. int pr = readInt(); //row # of pawn (start)
  12. int pc = readInt(); //col # of pawn (start)
  13. int kr = readInt(); //row # of knight (start)
  14. int kc = readInt(); //col # of knight (start)
  15.  
  16. // char grid[][] = new char[r+1][c+1];
  17. // for (int j=0; j<r; j++) {
  18. // grid[j] = read().toCharArray();
  19. // }
  20. int cnt = 0; //store as moves for win.
  21. int cntstale = 0; //only update it when the knight is one row above the pawn. Output this ONLY IF pawn reaches top row
  22.  
  23. Queue<Integer> QR = new LinkedList<Integer>(); Queue<Integer> QC = new LinkedList<Integer>();
  24. QR.add(kr); QC.add(kc);
  25. int CR = QR.poll();
  26. int CC = QC.poll();
  27. while (!QR.isEmpty() || QC.isEmpty()) {
  28. pr++; //PAWN MOVES FIRST
  29. if (pr==r) {
  30. if (cntstale!=0) System.out.println("Stalemate in " + cntstale + " knight move(s).");
  31. else System.out.println("Loss in " + cnt + " knight move(s)."); //loss
  32.  
  33. break;
  34. }
  35. cnt++;
  36.  
  37. if (CR+1<=r && CC-2>=1) {
  38. QR.add(CR+1);
  39. QC.add(CC-2);
  40. }
  41. if (CR+2<=r && CC-1>=1) {
  42. QR.add(CR+2);
  43. QC.add(CC-1);
  44. }
  45. if (CR+2<=r && CC+1<=c) {
  46. QR.add(CR+2);
  47. QC.add(CC+1);
  48. }
  49. if (CR+1<=r && CC+2<=c) {
  50. QR.add(CR+1);
  51. QC.add(CC+2);
  52. }
  53. if (CR-1>=1 && CC+2<=c) {
  54. QR.add(CR-1);
  55. QC.add(CC+2);
  56. }
  57. if (CR-2>=1 && CC+1<=c) {
  58. QR.add(CR-2);
  59. QC.add(CC+1);
  60. }
  61. if (CR-2>=1 && CC-1>=1) {
  62. QR.add(CR-2);
  63. QC.add(CC-1);
  64. }
  65. if (CR-1>=1 && CC-2>=1) {
  66. QR.add(CR-1);
  67. QC.add(CC-2);
  68. }
  69. CR = QR.poll();
  70. CC = QC.poll();
  71.  
  72. if (isWin(pr, pc, CR, CC)) {
  73. System.out.println("Win in " + cnt + " knight move(s).");
  74. break;
  75. }
  76. if (isStalemate(pr, pc, CR, CC)) {
  77. cntstale = cnt;
  78. }
  79.  
  80. }
  81. }
  82.  
  83. exit();
  84. }
  85.  
  86. static boolean isWin(int prr, int pcc, int krr, int kcc) {
  87. if (prr==krr && pcc==kcc) {
  88. return true;
  89. } else {
  90. return false;
  91. }
  92. }
  93.  
  94. static boolean isStalemate(int prr, int pcc, int krr, int kcc) {
  95. if (krr==prr+1 && pcc==kcc) {
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. }
  101.  
  102. final private static int BUFFER_SIZE = 1 << 16;
  103. private static DataInputStream din = new DataInputStream(System.in);
  104. private static byte[] buffer = new byte[BUFFER_SIZE];
  105. private static int bufferPointer = 0, bytesRead = 0;
  106. static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  107.  
  108. public static String readLine() throws IOException {
  109. byte[] buf = new byte[64]; // line length
  110. int cnt = 0, c;
  111. while ((c = Read()) != -1) {
  112. if (c == '\n')
  113. break;
  114. buf[cnt++] = (byte) c;
  115. }
  116. return new String(buf, 0, cnt);
  117. }
  118. public static String read() throws IOException{
  119. byte[] ret = new byte[1024];
  120. int idx = 0;
  121. byte c = Read();
  122. while (c <= ' ') {
  123. c = Read();
  124. }
  125. do {
  126. ret[idx++] = c;
  127. c = Read();
  128. } while (c != -1 && c != ' ' && c != '\n' && c != '\r');
  129. return new String(ret, 0, idx);
  130. }
  131. public static int readInt() throws IOException {
  132. int ret = 0;
  133. byte c = Read();
  134. while (c <= ' ')
  135. c = Read();
  136. boolean neg = (c == '-');
  137. if (neg)
  138. c = Read();
  139. do {
  140. ret = ret * 10 + c - '0';
  141. } while ((c = Read()) >= '0' && c <= '9');
  142.  
  143. if (neg)
  144. return -ret;
  145. return ret;
  146. }
  147.  
  148. public static long readLong() throws IOException {
  149. long ret = 0;
  150. byte c = Read();
  151. while (c <= ' ')
  152. c = Read();
  153. boolean neg = (c == '-');
  154. if (neg)
  155. c = Read();
  156. do {
  157. ret = ret * 10 + c - '0';
  158. } while ((c = Read()) >= '0' && c <= '9');
  159. if (neg)
  160. return -ret;
  161. return ret;
  162. }
  163.  
  164. public static double readDouble() throws IOException {
  165. double ret = 0, div = 1;
  166. byte c = Read();
  167. while (c <= ' ')
  168. c = Read();
  169. boolean neg = (c == '-');
  170. if (neg)
  171. c = Read();
  172.  
  173. do {
  174. ret = ret * 10 + c - '0';
  175. } while ((c = Read()) >= '0' && c <= '9');
  176.  
  177. if (c == '.') {
  178. while ((c = Read()) >= '0' && c <= '9') {
  179. ret += (c - '0') / (div *= 10);
  180. }
  181. }
  182.  
  183. if (neg)
  184. return -ret;
  185. return ret;
  186. }
  187.  
  188. private static void fillBuffer() throws IOException {
  189. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  190. if (bytesRead == -1)
  191. buffer[0] = -1;
  192. }
  193.  
  194. private static byte Read() throws IOException {
  195. if (bufferPointer == bytesRead)
  196. fillBuffer();
  197. return buffer[bufferPointer++];
  198. }
  199.  
  200. public void close() throws IOException {
  201. if (din == null)
  202. return;
  203. din.close();
  204. }
  205.  
  206. static void print(Object o) {
  207. pr.print(o);
  208. }
  209.  
  210. static void println(Object o) {
  211. pr.println(o);
  212. }
  213.  
  214. static void flush() {
  215. pr.flush();
  216. }
  217.  
  218. static void println() {
  219. pr.println();
  220. }
  221.  
  222. static void exit() throws IOException {
  223. din.close();
  224. pr.close();
  225. System.exit(0);
  226. }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement