結果
問題 |
No.124 門松列(3)
|
ユーザー |
![]() |
提出日時 | 2015-01-15 20:53:41 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 254 ms / 5,000 ms |
コード長 | 2,032 bytes |
コンパイル時間 | 2,960 ms |
コンパイル使用メモリ | 77,976 KB |
実行使用メモリ | 58,084 KB |
最終ジャッジ日時 | 2024-06-22 11:47:06 |
合計ジャッジ時間 | 9,036 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int column = sc.nextInt(); int row = sc.nextInt(); int[][] maze = new int[row][column]; int[] vx = {0,1,0,-1}; int[] vy={-1,0,1,0}; int[][] fromx = new int[row][column]; int[][] fromy = new int[row][column]; int[][] kai = new int[row][column]; for(int i=0;i<row;i++){ for(int j=0;j<column;j++){ maze[i][j]=sc.nextInt(); } } Queue<Integer> quex = new LinkedList<Integer>(); Queue<Integer> quey = new LinkedList<Integer>(); Queue<Integer> moto = new LinkedList<Integer>(); Queue<Integer> hoo = new LinkedList<Integer>(); quex.offer(1); quey.offer(0); quex.offer(0); quey.offer(1); hoo.offer(1); hoo.offer(1); moto.offer(maze[0][0]); moto.offer(maze[0][0]); while(!quex.isEmpty()){ int nx = quex.poll(); int ny = quey.poll(); int nowho = hoo.poll(); int moto1 = moto.poll(); for(int i=0;i<4;i++){ int nex = nx+vx[i]; int ney = ny+vy[i]; if(nex<0 || nex > column-1 || ney<0 || ney > row-1) continue; if( ((moto1 > maze[ny][nx] && maze[ney][nex]>maze[ny][nx]) || (moto1 < maze[ny][nx] && maze[ney][nex]<maze[ny][nx]))&&kai[ney][nex]<4 && (fromx[ney][nex]!=nx||fromy[ney][nex]!=ny)&&moto1!=maze[ny][nx]&&moto1 != maze[ney][nex] && maze[ny][nx]!=maze[ney][nex]){ quex.offer(nex); quey.offer(ney); moto.offer(maze[ny][nx]); hoo.offer(nowho+1); fromx[ney][nex]=nx; fromy[ney][nex]=ny; kai[ney][nex]++; if(nex == column-1 && ney == row-1){ System.out.println(nowho+1); return; } } } } System.out.println(-1); } }