結果
問題 | No.367 ナイトの転身 |
ユーザー | めうめう🎒 |
提出日時 | 2016-05-11 17:23:15 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 461 ms / 2,000 ms |
コード長 | 2,994 bytes |
コンパイル時間 | 1,944 ms |
コンパイル使用メモリ | 78,088 KB |
実行使用メモリ | 61,344 KB |
最終ジャッジ日時 | 2024-10-05 13:44:19 |
合計ジャッジ時間 | 8,936 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 168 ms
53,312 KB |
testcase_01 | AC | 164 ms
53,348 KB |
testcase_02 | AC | 174 ms
53,416 KB |
testcase_03 | AC | 187 ms
53,220 KB |
testcase_04 | AC | 159 ms
53,384 KB |
testcase_05 | AC | 175 ms
53,040 KB |
testcase_06 | AC | 193 ms
53,028 KB |
testcase_07 | AC | 169 ms
53,052 KB |
testcase_08 | AC | 164 ms
53,176 KB |
testcase_09 | AC | 155 ms
52,964 KB |
testcase_10 | AC | 379 ms
58,144 KB |
testcase_11 | AC | 461 ms
61,344 KB |
testcase_12 | AC | 275 ms
54,768 KB |
testcase_13 | AC | 258 ms
54,764 KB |
testcase_14 | AC | 326 ms
56,784 KB |
testcase_15 | AC | 198 ms
53,508 KB |
testcase_16 | AC | 340 ms
57,040 KB |
testcase_17 | AC | 229 ms
54,652 KB |
testcase_18 | AC | 228 ms
54,620 KB |
testcase_19 | AC | 222 ms
54,592 KB |
testcase_20 | AC | 196 ms
52,196 KB |
testcase_21 | AC | 236 ms
54,860 KB |
testcase_22 | AC | 161 ms
53,256 KB |
testcase_23 | AC | 190 ms
53,376 KB |
testcase_24 | AC | 180 ms
53,120 KB |
testcase_25 | AC | 176 ms
53,144 KB |
testcase_26 | AC | 186 ms
53,384 KB |
ソースコード
import java.util.ArrayDeque; import java.util.Arrays; import java.util.Queue; import java.util.Scanner; class State{ public int nx; public int ny; public int how; public boolean jo; State(int how,int nx,int ny,boolean jo){ this.how = how; this.nx = nx; this.ny = ny; this.jo = jo; } } public class Main { private int[] DIRX1 = {1,1,2,2,-1,-1,-2,-2}; private int[] DIRY1 = {2,-2,1,-1,2,-2,1,-1}; private int[] DIRX2 = {1,1,-1,-1}; private int[] DIRY2 = {1,-1,1,-1}; private int[][] hyo; private int h; private int w; private int sx; private int sy; private int gx; private int gy; Main(){ hyo = new int[501][501]; for(int i = 0;i < 501;i++){ for(int j = 0;j < 501;j++){ hyo[j][i] = 0; } } } public void setHyo(int i,int j,int num){ this.hyo[j][i] = num; } public void setSx(int sx){ this.sx = sx; } public void setSy(int sy){ this.sy = sy; } public void setGx(int gx){ this.gx = gx; } public void setGy(int gy){ this.gy = gy; } public void setH(int h){ this.h= h; } public void setW(int w){ this.w = w; } public int getSx(){ return this.sx; } public int getSy(){ return this.sy; } public int dfs(int sx,int sy,boolean sjo){ Queue<State> que = new ArrayDeque<State>(); State f = new State(0,sx,sy,false); boolean[][][] used = new boolean[501][501][2]; for(int i = 0;i < 501;i++){ for(int j = 0;j < 501;j++){ for(int k = 0;k < 2;k++){ used[j][i][k] = false; } } } que.add(f); while(que.peek() != null){ State now = que.poll(); int how = now.how; int nx = now.nx; int ny = now.ny; boolean jo = now.jo; if(nx == gx && ny == gy){ return how; } if(used[nx][ny][jo ? 1 : 0] == true) continue; used[nx][ny][jo ? 1 : 0] = true; if(jo){ for(int i = 0;i < 4;i++){ int tx = nx + DIRX2[i],ty = ny + DIRY2[i]; if(tx < 0 || tx >= w || ty < 0 || ty >= h) continue; if(hyo[tx][ty] == 1){ que.add(new State(how + 1,tx,ty,false)); }else{ que.add(new State(how + 1,tx,ty,true)); } } }else{ for(int i = 0;i < 8;i++){ int tx = nx + DIRX1[i],ty = ny + DIRY1[i]; if(tx < 0 || tx >= w || ty < 0 || ty >= h) continue; if(hyo[tx][ty] == 1){ que.add(new State(how + 1,tx,ty,true)); }else{ que.add(new State(how + 1,tx,ty,false)); } } } } return -1; } public void run(){ System.out.println(dfs(this.getSx(),this.getSy(),false)); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); Main m = new Main(); int h = sc.nextInt(); int w = sc.nextInt(); m.setH(h); m.setW(w); String str; for(int i = 0;i < h;i++){ str = sc.next(); char[] s = str.toCharArray(); for(int j = 0;j < w;j++){ if(s[j] == 'R'){ m.setHyo(i, j, 1); }else if(s[j] == 'S'){ m.setSx(j); m.setSy(i); }else if(s[j] == 'G'){ m.setGx(j); m.setGy(i); } } } m.run(); } }