結果
問題 | No.859 路線A、路線B、路線C |
ユーザー | ziita |
提出日時 | 2019-08-09 21:54:22 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,910 bytes |
コンパイル時間 | 2,249 ms |
コンパイル使用メモリ | 81,436 KB |
実行使用メモリ | 39,468 KB |
最終ジャッジ日時 | 2024-07-19 11:46:47 |
合計ジャッジ時間 | 3,570 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
36,732 KB |
testcase_01 | AC | 46 ms
36,376 KB |
testcase_02 | AC | 44 ms
36,700 KB |
testcase_03 | AC | 45 ms
36,852 KB |
testcase_04 | AC | 43 ms
36,640 KB |
testcase_05 | WA | - |
testcase_06 | AC | 44 ms
36,392 KB |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
ソースコード
import java.io.*; import java.util.*; import java.math.*; // import java.awt.Point; public class Main { InputStream is; PrintWriter out; String INPUT = ""; // static int mod = 1_000_000_007; int mod = 998244353; // long inf = Long.MAX_VALUE/2; int inf = Integer.MAX_VALUE/2; void solve(){ int x = ni(); int y = ni(); int z = ni(); String[] s1 = ns().split(" "); String[] s2 = ns().split(" "); int idx1 = set_idx(s1[0], Integer.parseInt(s1[1])-1, x, y, z); int idx2 = set_idx(s2[0], Integer.parseInt(s2[1])-1, x, y, z); ArrayDeque<int[]> q = new ArrayDeque<>(); q.add(new int[]{idx1, 0}); boolean[] seen = new boolean[x+y+z]; while(true){ int[] cur = q.pop(); int id = cur[0]; int dist = cur[1]; seen[id] = true; if(id==idx2){ out.println(dist); return; } if(id==0){ if(!seen[1]) q.add(new int[]{1,dist+1}); if(!seen[x]) q.add(new int[]{x,dist+1}); if(!seen[x+y]) q.add(new int[]{x+y,dist+1}); } if(id==x){ if(!seen[0]) q.add(new int[]{0,dist+1}); if(y!=1&&!seen[x+1]) q.add(new int[]{x+1,dist+1}); if(!seen[x+y]) q.add(new int[]{x+y,dist+1}); } if(id==x+y){ if(!seen[0]) q.add(new int[]{0,dist+1}); if(!seen[x]) q.add(new int[]{x,dist+1}); if(z!=1&&!seen[x+y+1]) q.add(new int[]{x+y+1,dist+1}); } if(id==x-1){ if(x!=1&&!seen[x-2]) q.add(new int[]{x-2,dist+1}); if(!seen[x+y-1]) q.add(new int[]{x+y-1,dist+1}); if(!seen[x+y+z-1]) q.add(new int[]{x+y+z-1,dist+1}); } if(id==x+y-1){ if(!seen[x-1]) q.add(new int[]{x-1,dist+1}); if(y!=1&&!seen[x+y-1]) q.add(new int[]{x+y-2,dist+1}); if(!seen[x+y+z-1]) q.add(new int[]{x+y+z-1,dist+1}); } if(id==x+y+z-1){ if(!seen[x-1]) q.add(new int[]{x-1,dist+1}); if(!seen[x+y-1]) q.add(new int[]{x+y-1,dist+1}); if(z!=1&&!seen[x+y+z-2]) q.add(new int[]{x+y+z-2,dist+1}); } if(id!=0&&id!=x&&id!=x+y&&id!=x-1&&id!=x+y-1&&id!=x+y+z-1){ if(!seen[id-1]) q.add(new int[]{id-1,dist+1}); if(!seen[id+1]) q.add(new int[]{id+1,dist+1}); } } } int set_idx(String s, int idx, int x, int y, int z){ if(s.equals("A")){ return idx; } else if(s.equals("B")){ return x + idx; } else{ return x + y + idx; } } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b) && b != ' ')){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }