結果

問題 No.340 雪の足跡
ユーザー 37zigen37zigen
提出日時 2020-04-13 23:16:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 568 ms / 1,000 ms
コード長 4,314 bytes
コンパイル時間 2,477 ms
コンパイル使用メモリ 79,196 KB
実行使用メモリ 92,192 KB
最終ジャッジ日時 2024-04-08 11:13:19
合計ジャッジ時間 14,634 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
53,968 KB
testcase_01 AC 54 ms
53,876 KB
testcase_02 AC 54 ms
53,964 KB
testcase_03 AC 54 ms
53,988 KB
testcase_04 AC 53 ms
53,868 KB
testcase_05 AC 57 ms
53,872 KB
testcase_06 AC 56 ms
53,976 KB
testcase_07 AC 53 ms
53,980 KB
testcase_08 AC 53 ms
53,976 KB
testcase_09 AC 55 ms
53,988 KB
testcase_10 AC 55 ms
53,864 KB
testcase_11 AC 66 ms
53,996 KB
testcase_12 AC 61 ms
53,964 KB
testcase_13 AC 68 ms
54,268 KB
testcase_14 AC 141 ms
59,644 KB
testcase_15 AC 148 ms
59,224 KB
testcase_16 AC 121 ms
59,196 KB
testcase_17 AC 161 ms
62,328 KB
testcase_18 AC 150 ms
60,900 KB
testcase_19 AC 165 ms
62,384 KB
testcase_20 AC 508 ms
89,956 KB
testcase_21 AC 195 ms
60,812 KB
testcase_22 AC 171 ms
79,916 KB
testcase_23 AC 483 ms
90,036 KB
testcase_24 AC 315 ms
81,476 KB
testcase_25 AC 437 ms
85,528 KB
testcase_26 AC 130 ms
58,876 KB
testcase_27 AC 94 ms
56,300 KB
testcase_28 AC 152 ms
59,744 KB
testcase_29 AC 520 ms
77,016 KB
testcase_30 AC 489 ms
73,932 KB
testcase_31 AC 541 ms
84,676 KB
testcase_32 AC 508 ms
92,008 KB
testcase_33 AC 532 ms
92,100 KB
testcase_34 AC 556 ms
92,036 KB
testcase_35 AC 568 ms
92,172 KB
testcase_36 AC 506 ms
92,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Queue;

public class Main {
	
	void run() {
		FastScanner sc=new FastScanner();
		PrintWriter pw=new PrintWriter(System.out);
		int W=sc.nextInt();
		int H=sc.nextInt();
		int N=sc.nextInt();
		int[][][] horizontal=new int[2][H+1][W+1];
		int[][][] vertical=new int[2][H+1][W+1];
		for (int i=0;i<N;++i) {
			int M=sc.nextInt();
			int[] B=new int[M+1];
			for (int j=0;j<M+1;++j) {
				B[j]=sc.nextInt(); // w+hW
			}
			for (int j=0;j<M;++j) {
				int src=B[j];
				int dst=B[j+1];
				int hs=src/W;
				int ws=src%W;
				int hd=dst/W;
				int wd=dst%W;
				if (hs==hd) {
					if (ws<wd) { 
						++horizontal[1][hs][ws];
						--horizontal[1][hd][wd];
					} else {
						--horizontal[0][hs][ws+1];
						++horizontal[0][hd][wd+1];
					}
				} else {
					if (hs<hd) {
						++vertical[1][hs][ws];
						--vertical[1][hd][wd];
					} else {
						--vertical[0][hs+1][ws];
						++vertical[0][hd+1][wd];
					}
				}
			}
		}
		for (int k=0;k<2;++k) {
			for (int i=0;i<=H;++i) {
				for (int j=0;j<=W;++j) {
					if (j+1<=W) horizontal[k][i][j+1]+=horizontal[k][i][j];
					if (i+1<=H) vertical[k][i+1][j]+=vertical[k][i][j];
				}
			}
		}
		int[][] dist=new int[H][W];
		int INF=Integer.MAX_VALUE/3;
		for (int i=0;i<H;++i) for (int j=0;j<W;++j) dist[i][j]=INF;
		dist[0][0]=0;
		Queue<Integer> que=new ArrayDeque<Integer>();
		que.add(0);
		while (!que.isEmpty()) {
			int p=que.poll();
			int h=p/W;
			int w=p%W;
			if (dist[h][w]==INF) continue;
			int[] dh= {-1,1,0,0};
			int[] dw= {0,0,-1,1};
			for (int k=0;k<4;++k) {
				int nh=h+dh[k];
				int nw=w+dw[k];
				if (nh<0||nw<0||nh>=H||nw>=W) continue;
				if (dist[nh][nw]<=dist[h][w]+1) continue;
				if ((dw[k]!=0&&(horizontal[(dw[k]+1)/2][h][w]>0||horizontal[((dw[k]+1)/2)^1][h][w+dw[k]]>0))||(dh[k]!=0&&(vertical[(dh[k]+1)/2][h][w]>0||vertical[((dh[k]+1)/2)^1][h+dh[k]][w]>0))) {
					dist[nh][nw]=dist[h][w]+1;
					que.add(nw+nh*W);
				}
			}
		}
		pw.println(dist[H-1][W-1]==INF?"Odekakedekinai..":dist[H-1][W-1]);
		pw.close();
	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
	
    public static void main(String[] args) {
    	new Main().run();
    }
}




class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (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;}
    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new 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 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') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
    	return (int)nextLong();
    }
}
0