結果

問題 No.34 砂漠の行商人
ユーザー GrenacheGrenache
提出日時 2016-03-15 20:12:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 447 ms / 5,000 ms
コード長 3,360 bytes
コンパイル時間 3,991 ms
コンパイル使用メモリ 78,320 KB
実行使用メモリ 60,916 KB
最終ジャッジ日時 2023-09-10 19:23:32
合計ジャッジ時間 8,923 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,596 KB
testcase_01 AC 45 ms
49,484 KB
testcase_02 AC 55 ms
49,248 KB
testcase_03 AC 48 ms
49,704 KB
testcase_04 AC 106 ms
53,960 KB
testcase_05 AC 138 ms
56,208 KB
testcase_06 AC 79 ms
51,440 KB
testcase_07 AC 163 ms
57,644 KB
testcase_08 AC 189 ms
58,048 KB
testcase_09 AC 279 ms
58,776 KB
testcase_10 AC 148 ms
56,596 KB
testcase_11 AC 217 ms
58,188 KB
testcase_12 AC 100 ms
53,788 KB
testcase_13 AC 399 ms
60,716 KB
testcase_14 AC 447 ms
60,916 KB
testcase_15 AC 59 ms
50,852 KB
testcase_16 AC 140 ms
57,408 KB
testcase_17 AC 75 ms
51,420 KB
testcase_18 AC 63 ms
51,228 KB
testcase_19 AC 282 ms
59,080 KB
testcase_20 AC 363 ms
58,540 KB
testcase_21 AC 91 ms
51,444 KB
testcase_22 AC 95 ms
52,892 KB
testcase_23 AC 69 ms
50,756 KB
testcase_24 AC 392 ms
59,044 KB
testcase_25 AC 131 ms
57,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Queue;


public class Main_yukicoder34 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        int[] dx = {1, -1, 0, 0};
        int[] dy = {0, 0, 1, -1};

        int n = sc.nextInt();
        int v = sc.nextInt();
        int sx = sc.nextInt() - 1;
        int sy = sc.nextInt() - 1;
        int gx = sc.nextInt() - 1;
        int gy = sc.nextInt() - 1;

        int[][] l = new int[n][n];

        for (int i = 0; i < n; i++) {
        	for (int j = 0; j < n; j++) {
        		l[i][j] = sc.nextInt();
        	}
        }

        int[][] vv = new int[n][n];
        Queue<Integer> qx = new ArrayDeque<>();
        Queue<Integer> qy = new ArrayDeque<>();
        Queue<Integer> qv = new ArrayDeque<>();
        qx.add(sx);
        qy.add(sy);
        qv.add(v);
        vv[sy][sx] = v;

        for (int cnt = 0; true; cnt++) {
            Queue<Integer> qxtmp = new ArrayDeque<>();
            Queue<Integer> qytmp = new ArrayDeque<>();
            Queue<Integer> qvtmp = new ArrayDeque<>();

        	while (!qx.isEmpty()) {
        		int x = qx.remove();
        		int y = qy.remove();
        		int vtmp = qv.remove();

        		if (x == gx && y == gy && vtmp > 0) {
        			pr.println(cnt);

        			pr.close();
        			sc.close();
        			return;
        		}

        		for (int i = 0; i < dx.length; i++) {
        			int nx = x + dx[i];
        			int ny = y + dy[i];

        			if (nx >= 0 && nx < n && ny >= 0 && ny < n) {
        				int nv = vtmp - l[ny][nx];
        				if (nv > 0 && nv > vv[ny][nx]) {
        					qxtmp.add(nx);
        					qytmp.add(ny);
        					qvtmp.add(nv);
        					vv[ny][nx] = nv;
        				}
        			}
        		}
        	}

        	if (!qxtmp.isEmpty()) {
//        		pr.println(qxtmp.size());
        		qx = qxtmp;
        		qy = qytmp;
        		qv = qvtmp;
        	} else {
        		break;
        	}
        }

        pr.println(-1);

        pr.close();
        sc.close();
    }

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0