結果

問題 No.424 立体迷路
ユーザー koba-e964koba-e964
提出日時 2016-09-22 23:20:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 2,970 bytes
コンパイル時間 834 ms
コンパイル使用メモリ 100,512 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 16:50:30
合計ジャッジ時間 1,963 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;

/**
 * Dijkstra's algorithm.
 * First, call add_edge() to add edges.
 * Second, call solve() to calculate the length of the shortest path from source to each vertex.
 * Header requirement: algorithm, queue, vector
 * Verified by AtCoder ARC026-C (http://arc026.contest.atcoder.jp/submissions/604231)
 */

const ll inf = 1e16;
template<class Len = int>
class Dijkstra {
private:
  int n;
  std::vector<std::vector<std::pair<int, Len> > > edges;
public:
  /**
   * n: the number of vertices
   */
  Dijkstra(int n) : n(n), edges(n) {}
  /*
   * from: the source of edge to add
   * to: the target of edge to add
   * cost: the cost of edge to add
   */
  void add_edge(int from, int to, Len cost) {
    edges[from].push_back(std::pair<int, Len>(to, cost));
  }
  /*
   * This function returns an array consisting of the distances from vertex source.
   */
  std::vector<Len> solve(int source) {
    typedef std::pair<Len, int> pi;
    std::vector<Len> d(n, inf);
    std::priority_queue<pi, std::vector<pi>, std::greater<pi> > que;
    que.push(pi(0, source));
    while (!que.empty()) {
      pi p = que.top(); que.pop();
      int idx = p.second;
      if (d[idx] <= p.first) {
	continue;
      }
      d[idx] = p.first;
      for(int j = 0; j < edges[idx].size(); ++j) {
	que.push(pi(p.first + edges[idx][j].second, edges[idx][j].first));
      }
    }
    return d;
  }
};

const int H = 51;
string s[H];

int main(void){
  int h, w;
  cin >> h >> w;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  REP(i, 0, h) {
    cin >> s[i];
  }
  Dijkstra<ll> dijk(h * w);
  REP(i, 0, h) {
    REP(j, 0, w) {
      int dxy[5] = {1, 0, -1, 0, 1};
      REP(d, 0, 4) {
	int nx = i + dxy[d];
	int ny = j + dxy[d + 1];
	if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
	  continue;
	}
	int diff = s[i][j] - s[nx][ny];
	if (diff >= -1 && diff <= 1) {
	  dijk.add_edge(i * w + j, nx * w + ny, 1);
	}
      }
      REP(d, 0, 4) {
	int nx = i + 2 * dxy[d];
	int ny = j + 2 * dxy[d + 1];
	if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
	  continue;
	}
	char midheight = s[(i + nx) / 2][(j + ny) / 2];
	int diff = s[i][j] - s[nx][ny];
	if (diff == 0 && midheight < s[i][j]) {
	  dijk.add_edge(i * w + j, nx * w + ny, 1);
	}
      }
    }
  }
  ll res = dijk.solve(sx * w + sy)[gx * w + gy];
  cout << (res == inf ? "NO" : "YES") << endl;
}
0