結果

問題 No.20 砂漠のオアシス
ユーザー togatogatogatoga
提出日時 2015-09-12 14:26:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 20 ms / 5,000 ms
コード長 1,867 bytes
コンパイル時間 1,427 ms
コンパイル使用メモリ 166,892 KB
実行使用メモリ 6,912 KB
最終ジャッジ日時 2024-04-21 06:43:22
合計ジャッジ時間 2,513 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 15 ms
6,656 KB
testcase_06 AC 14 ms
6,912 KB
testcase_07 AC 20 ms
6,912 KB
testcase_08 AC 15 ms
6,784 KB
testcase_09 AC 20 ms
6,912 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 6 ms
5,504 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;


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

struct Edge{
  int node,cost;
  Edge(int node, int cost):node(node),cost(cost){
  }
};

struct State{
  int hp,node;
  bool use;
  State(int node, int hp, bool use):hp(hp),node(node),use(use){}
  bool operator < (const State& right) const {
    return hp < right.hp;

  }
};


int N,V,gx,gy;
int board[220][220];
int cost[44000][2];
vector<Edge> Edges[44000];

int main(){
  cin >> N >> V >> gx >> gy;
  gx--;
  gy--;
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      cin >> board[i][j];
    }
  }
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      int ny,nx;
      int node = i * N + j;
      for (int k = 0; k < 4; k++){
	ny = i + dy[k];
	nx = j + dx[k];
	if (ny < 0 or nx < 0 or ny >= N or nx >= N)continue;
	int next_node = ny * N + nx;
	int cost = board[ny][nx];
	Edges[node].push_back(Edge(next_node, cost));
      }
    }
  }
  priority_queue<State> que;
  que.push(State(0, V, false));
  for (int i = 0; i < 44000; i++){
    for (int j = 0; j < 2; j++){
      cost[i][j] = -100;
    }
  }
  cost[0][0] = V;
  while (!que.empty()){
    State pos = que.top();
    que.pop();

    if (cost[pos.node][pos.use] > pos.hp){
      continue;
    }
    //cout << pos.node << " " << pos.hp << endl;
    if (!pos.use and pos.node == gy * N + gx){
      pos.use = true;
      pos.hp *= 2;
    }
    if (pos.node == N * (N - 1) + (N - 1)){
      cout << "YES" << endl;
      return 0;
    }
    for (int i = 0; i < Edges[pos.node].size(); i++){
      Edge np = Edges[pos.node][i];
      if (pos.hp - np.cost > 0){
	if (cost[np.node][pos.use] < pos.hp - np.cost){
	  cost[np.node][pos.use] = pos.hp - np.cost;
	  que.push(State(np.node, cost[np.node][pos.use], pos.use));
	}
      }
    }
  }
  cout << "NO" << endl;
  return 0;
}
0