結果
問題 | No.1588 Connection |
ユーザー |
![]() |
提出日時 | 2021-07-08 23:19:26 |
言語 | Java17 (openjdk 17.0.1) |
結果 |
AC
|
実行時間 | 390 ms / 2,000 ms |
コード長 | 12,556 bytes |
コンパイル時間 | 5,110 ms |
使用メモリ | 73,332 KB |
平均クエリ数 | 563.00 |
最終ジャッジ日時 | 2023-02-15 01:10:20 |
合計ジャッジ時間 | 10,504 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
テストケース
テストケース表示入力 | 結果 | 実行時間 使用メモリ |
---|---|---|
testcase_00 | AC | 112 ms
63,716 KB |
testcase_01 | AC | 112 ms
64,816 KB |
testcase_02 | AC | 114 ms
64,576 KB |
testcase_03 | AC | 115 ms
64,700 KB |
testcase_04 | AC | 115 ms
64,852 KB |
testcase_05 | AC | 111 ms
64,092 KB |
testcase_06 | AC | 119 ms
64,540 KB |
testcase_07 | AC | 115 ms
65,212 KB |
testcase_08 | AC | 129 ms
64,768 KB |
testcase_09 | AC | 136 ms
63,652 KB |
testcase_10 | AC | 135 ms
64,932 KB |
testcase_11 | AC | 138 ms
64,716 KB |
testcase_12 | AC | 276 ms
68,232 KB |
testcase_13 | AC | 294 ms
68,396 KB |
testcase_14 | AC | 114 ms
64,632 KB |
testcase_15 | AC | 121 ms
64,772 KB |
testcase_16 | AC | 113 ms
65,172 KB |
testcase_17 | AC | 115 ms
64,972 KB |
testcase_18 | AC | 113 ms
65,076 KB |
testcase_19 | AC | 116 ms
64,692 KB |
testcase_20 | AC | 116 ms
65,996 KB |
testcase_21 | AC | 385 ms
73,332 KB |
testcase_22 | AC | 384 ms
73,096 KB |
testcase_23 | AC | 277 ms
68,232 KB |
testcase_24 | AC | 213 ms
67,908 KB |
testcase_25 | AC | 292 ms
72,132 KB |
testcase_26 | AC | 288 ms
72,304 KB |
testcase_27 | AC | 210 ms
67,660 KB |
testcase_28 | AC | 192 ms
66,040 KB |
testcase_29 | AC | 390 ms
70,304 KB |
testcase_30 | AC | 386 ms
70,228 KB |
testcase_31 | AC | 109 ms
64,120 KB |
ソースコード
import java.util.*; public class Main { public static void main(String[] args) throws Exception{ final ContestScanner sc = new ContestScanner(); final ContestPrinter pr = new ContestPrinter(); final int N = sc.nextInt(); final int M = sc.nextInt(); int[][] board = new int[N][N]; //0: uncertain, 1:black, 2:white board[0][0] = 1; Queue<Pair<Integer,Integer>> queue = new LinkedList<>(); queue.add(new Pair<>(0,0)); while(!queue.isEmpty()){ Pair<Integer,Integer> top = queue.poll(); int h = top.getFirst(), w = top.getSecond(); if(h>0 && board[h-1][w]==0){ System.out.println(String.format("%d %d", h-1 + 1, w + 1)); if(sc.next().equals("Black")){ board[h-1][w] = 1; queue.add(new Pair<>(h-1,w)); } else{ board[h-1][w] = 2; } } if(h<N-1 && board[h+1][w]==0){ System.out.println(String.format("%d %d", h+1 + 1, w + 1)); if(sc.next().equals("Black")){ board[h+1][w] = 1; queue.add(new Pair<>(h+1,w)); } else{ board[h+1][w] = 2; } } if(w>0 && board[h][w-1]==0){ System.out.println(String.format("%d %d", h + 1, w-1 + 1)); if(sc.next().equals("Black")){ board[h][w-1] = 1; queue.add(new Pair<>(h,w-1)); } else{ board[h][w-1] = 2; } } if(w<N-1 && board[h][w+1]==0){ System.out.println(String.format("%d %d", h + 1, w+1 + 1)); if(sc.next().equals("Black")){ board[h][w+1] = 1; queue.add(new Pair<>(h,w+1)); } else{ board[h][w+1] = 2; } } } System.out.println(board[N-1][N-1]==1 ? "Yes" : "No"); } } class Pair<S extends Comparable<S>, T extends Comparable<T>> implements Comparable<Pair<S,T>>{ S first; T second; public Pair(S s, T t){ first = s; second = t; } public S getFirst(){return first;} public T getSecond(){return second;} public boolean equals(Object another){ if(this==another) return true; if(!(another instanceof Pair)) return false; Pair otherPair = (Pair)another; return this.first.equals(otherPair.first) && this.second.equals(otherPair.second); } public int compareTo(Pair<S,T> another){ java.util.Comparator<Pair<S,T>> comp1 = java.util.Comparator.comparing(Pair::getFirst); java.util.Comparator<Pair<S,T>> comp2 = comp1.thenComparing(Pair::getSecond); return comp2.compare(this, another); } public int hashCode(){ return first.hashCode() * 10007 + second.hashCode(); } public String toString(){ return String.format("(%s, %s)", first, second); } } class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } } class ContestPrinter extends java.io.PrintWriter{ public ContestPrinter(java.io.PrintStream stream){ super(stream); } public ContestPrinter(){ super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if(x < 0){ sb.append('-'); x = -x; } x += Math.pow(10, -n)/2; sb.append((long)x); sb.append("."); x -= (long)x; for(int i = 0;i < n;i++){ x *= 10; sb.append((int)x); x -= (int)x; } return sb.toString(); } @Override public void print(float f){ super.print(dtos(f, 20)); } @Override public void println(float f){ super.println(dtos(f, 20)); } @Override public void print(double d){ super.print(dtos(d, 20)); } @Override public void println(double d){ super.println(dtos(d, 20)); } public void printArray(int[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public void printArray(int[] array){ this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n-1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map){ this.printArray(array, " ", map); } public void printArray(long[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public void printArray(long[] array){ this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n-1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map){ this.printArray(array, " ", map); } }