結果

問題 No.20 砂漠のオアシス
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-04-12 13:32:08
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,948 bytes
コンパイル時間 3,759 ms
コンパイル使用メモリ 79,024 KB
実行使用メモリ 51,764 KB
最終ジャッジ日時 2024-10-13 05:05:15
合計ジャッジ時間 5,317 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
36,904 KB
testcase_01 AC 47 ms
36,976 KB
testcase_02 AC 48 ms
36,660 KB
testcase_03 AC 48 ms
37,048 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 79 ms
39,476 KB
testcase_07 WA -
testcase_08 AC 79 ms
39,816 KB
testcase_09 AC 78 ms
39,492 KB
testcase_10 AC 47 ms
36,864 KB
testcase_11 WA -
testcase_12 AC 49 ms
37,004 KB
testcase_13 AC 49 ms
37,068 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 51 ms
36,716 KB
testcase_18 WA -
testcase_19 AC 54 ms
37,028 KB
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

//No.20 砂漠のオアシス

import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;

public class No20 {

    static final Reader in = new Reader();
    static final PrintWriter out = new PrintWriter(System.out,false);

    static void solve(){
        n = in.nextInt();
        int v = in.nextInt();
        ox = in.nextInt()-1;
        oy = in.nextInt()-1;

        l = new int[n][n];
        for (int i=0; i<n; i++) l[i] = in.nextIntArray(n);

        dp = new boolean[n*n][2];

        if (dfs(0,0,-1,-1,v)) {
            out.println("YES");
        }else if (ov == -1) {
            out.println("NO");
        }else {
            for (int i=0; i<n*n; i++) fill(dp[i],false);
            out.println(dfs(oy,ox,-1,-1,ov)?"YES":"NO");
        }
    }

    static int n,ox,oy,ov = -1;
    static int[] dy = {0,0,1,-1};
    static int[] dx = {1,-1,0,0};
    static boolean[][] dp;
    static int[][] l;

    static boolean dfs(int y, int x, int py, int px, int v) {
        if (dp[y*n+x][1]) return dp[y*n+x][0];
        dp[y*n+x][1] = true;

        if (y == n-1 && x == n-1) return dp[y*n+x][0] = true;
        if (y == oy && x == ox) ov = v*2;

        for (int i=0; i<4; i++) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (nx < 0 || n <= nx || ny < 0 || n <= ny) continue;
            if (dp[ny*n+nx][1]) continue;
            
            if (v-l[ny][nx] > 0 && dfs(ny,nx,y,x,v-l[ny][nx])) return dp[y*n+x][0] = true;
        }

        return dp[y*n+x][0] = false;
    }

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();

        solve();
        out.flush();

        long end = System.currentTimeMillis();
        //trace(end-start + "ms");
        in.close();
        out.close();
    }

    static void trace(Object... o) { System.out.println(deepToString(o));}
}

class Reader {
    private final InputStream in;
    private final byte[] buf = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    public Reader() { this(System.in);}
    public Reader(InputStream source) { this.in = source;}

    private boolean hasNextByte() {
        if (ptr < buflen) return true;
        ptr = 0;
        try{
            buflen = in.read(buf);
        }catch (IOException e) {e.printStackTrace();}
        if (buflen <= 0) return false;
        return true;
    }

    private int readByte() { if (hasNextByte()) return buf[ptr++]; else return -1;}

    private boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}

    private void skip() { while(hasNextByte() && !isPrintableChar(buf[ptr])) ptr++;}

    public boolean hasNext() {skip(); 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();
        boolean minus = false;
        long num = readByte();

        if(num == '-'){
            num = 0;
            minus = true;
        }else if (num < '0' || '9' < num){
            throw new NumberFormatException();
        }else{
            num -= '0';
        }
        
        while(true){
            int b = readByte();
            if('0' <= b && b <= '9')
                num = num * 10 + (b - '0');
            else if(b == -1 || !isPrintableChar(b))
                return minus ? -num : num;
            else
                throw new NoSuchElementException();
        }
    }

    public int nextInt() {
        long num = nextLong();
        if (num < Integer.MIN_VALUE || Integer.MAX_VALUE < num)
            throw new NumberFormatException();
        return (int)num;
    }

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

    public char nextChar() {
        if (!hasNext()) throw new NoSuchElementException();
        return (char)readByte();
    }

    public String nextLine() {
        while (hasNextByte() && (buf[ptr] == '\n' || buf[ptr] == '\r')) ptr++;
        if (!hasNextByte()) throw new NoSuchElementException();

        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (b != '\n' && b != '\r') {
            sb.appendCodePoint(b);
            b = readByte();
        }

        return sb.toString();
    }

    public int[] nextIntArray(int n) {
        int[] res = new int[n];
        for (int i=0; i<n; i++) res[i] = nextInt();
        return res;
    }

    public char[] nextCharArray(int n) {
        char[] res = new char[n];
        for (int i=0; i<n; i++) res[i] = nextChar();
        return res;
    }

    public void close() {try{ in.close();}catch(IOException e){ e.printStackTrace();}};
}
0