結果

問題 No.20 砂漠のオアシス
ユーザー uafr_csuafr_cs
提出日時 2015-09-14 03:04:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 400 ms / 5,000 ms
コード長 2,899 bytes
コンパイル時間 2,522 ms
コンパイル使用メモリ 79,760 KB
実行使用メモリ 58,732 KB
最終ジャッジ日時 2024-04-21 06:44:28
合計ジャッジ時間 6,682 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
50,548 KB
testcase_01 AC 57 ms
50,668 KB
testcase_02 AC 57 ms
50,468 KB
testcase_03 AC 99 ms
51,584 KB
testcase_04 AC 98 ms
52,676 KB
testcase_05 AC 161 ms
56,136 KB
testcase_06 AC 158 ms
56,496 KB
testcase_07 AC 400 ms
58,732 KB
testcase_08 AC 191 ms
56,884 KB
testcase_09 AC 345 ms
58,564 KB
testcase_10 AC 54 ms
50,300 KB
testcase_11 AC 54 ms
50,448 KB
testcase_12 AC 97 ms
51,272 KB
testcase_13 AC 96 ms
51,576 KB
testcase_14 AC 117 ms
55,632 KB
testcase_15 AC 95 ms
52,368 KB
testcase_16 AC 146 ms
55,684 KB
testcase_17 AC 118 ms
54,820 KB
testcase_18 AC 136 ms
55,352 KB
testcase_19 AC 142 ms
56,168 KB
testcase_20 AC 74 ms
50,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;

public class Main {
	
	public static final int[][] move_dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int V = sc.nextInt();
		final int Ox = sc.nextInt() - 1;
		final int Oy = sc.nextInt() - 1;
		
		int[][] Lyx = new int[N][N];
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				Lyx[i][j] = sc.nextInt();
			}
		}
		
		LinkedList<Integer> x_queue = new LinkedList<Integer>();
		LinkedList<Integer> y_queue = new LinkedList<Integer>();
		LinkedList<Integer> oasis_queue = new LinkedList<Integer>();
		int[][][] max_hp = new int[N][N][2];
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				for(int k = 0; k < 2; k++){
					max_hp[i][j][k] = -1;
				}
			}
		}
		max_hp[0][0][0] = V;

		x_queue.add(0);
		y_queue.add(0);
		oasis_queue.add(0);
		
		boolean ok = false;
		while(!x_queue.isEmpty()){
			final int x = x_queue.poll();
			final int y = y_queue.poll();
			final int oasis = oasis_queue.poll();
			final int hp = max_hp[y][x][oasis];
			
			if(x == N - 1 && y == N - 1){
				ok = true;
				break;
			}
			
			for(int[] move : move_dir){
				final int nx = x + move[0];
				final int ny = y + move[1];
				
				if(nx < 0 || nx >= N || ny < 0 || ny >= N){
					continue;
				}
				
				final int n_hp = hp - Lyx[ny][nx];
				if(n_hp <= 0){ continue; }
				
				if(max_hp[ny][nx][oasis] < n_hp){
					max_hp[ny][nx][oasis] = n_hp;
					x_queue.add(nx);
					y_queue.add(ny);
					oasis_queue.add(oasis);
				}
				
				if(oasis == 0 && Ox == nx && Oy == ny && max_hp[ny][nx][1] < n_hp * 2){
					max_hp[ny][nx][1] = n_hp * 2;
					x_queue.add(nx);
					y_queue.add(ny);
					oasis_queue.add(1);
				}
			}
		}
		
		System.out.println(ok ? "YES" : "NO");
	}
	
	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;

		public Scanner(InputStream is) throws IOException {
			br = new BufferedReader(new InputStreamReader(is));
		}

		private void getLine() throws IOException {
			while (!hasNext()) {
				tok = new StringTokenizer(br.readLine());
			}
		}

		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}

		public String next() throws IOException {
			getLine();
			return tok.nextToken();
		}

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

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

		public void close() throws IOException {
			br.close();
		}
	}

}
0