結果
| 問題 | 
                            No.20 砂漠のオアシス
                             | 
                    
| コンテスト | |
| ユーザー | 
                             k
                         | 
                    
| 提出日時 | 2014-11-25 16:26:13 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 322 ms / 5,000 ms | 
| コード長 | 1,890 bytes | 
| コンパイル時間 | 729 ms | 
| コンパイル使用メモリ | 76,960 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-13 04:59:34 | 
| 合計ジャッジ時間 | 2,380 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 21 | 
ソースコード
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <string>
#include <cstring>
using namespace std;
int main() {
  int n, v, ox, oy;
  cin >> n >> v >> ox >> oy;
  ox--;
  oy--;
  int field[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> field[j][i];
    }
  }
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, -1, 0, 1};
  int memo[n][n];
  memset(memo, 0, sizeof(memo));
  queue <int> qu;
  qu.push(0);			// x
  qu.push(0);			// y
  qu.push(v);			// v
  while (!qu.empty()) {
    int px = qu.front(); qu.pop();
    int py = qu.front(); qu.pop();
    int pv = qu.front(); qu.pop();
    for (int i = 0; i < 4; i++) {
      int nx = px+dx[i];
      int ny = py+dy[i];
      if (nx >= 0 && ny >= 0 && nx < n && ny < n &&
	  memo[nx][ny] < pv-field[nx][ny]) {
	memo[nx][ny] = pv-field[nx][ny];
	qu.push(nx);
	qu.push(ny);
	qu.push(pv-field[nx][ny]);
      }
    }
  }
  if (memo[n-1][n-1] > 0) {
    cout << "YES" << endl;
    return 0;
  }
  if (ox >= 0 && oy >= 0 && memo[ox][oy] > 0) {
    qu.push(ox);
    qu.push(oy);
    qu.push(memo[ox][oy]*2);
    while (!qu.empty()) {
      int px = qu.front(); qu.pop();
      int py = qu.front(); qu.pop();
      int pv = qu.front(); qu.pop();
      for (int i = 0; i < 4; i++) {
	int nx = px+dx[i];
	int ny = py+dy[i];
	if (nx >= 0 && ny >= 0 && nx < n && ny < n &&
	    memo[nx][ny] < pv-field[nx][ny]) {
	  memo[nx][ny] = pv-field[nx][ny];
	  qu.push(nx);
	  qu.push(ny);
	  qu.push(pv-field[nx][ny]);
	}
      }
    }
    if (memo[n-1][n-1] > 0) {
      cout << "YES" << endl;
      return 0;
    }
  }
  cout << "NO" << endl;
}
            
            
            
        
            
k