結果

問題 No.424 立体迷路
ユーザー Yut176Yut176
提出日時 2016-10-04 20:09:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,691 bytes
コンパイル時間 1,368 ms
コンパイル使用メモリ 87,928 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 16:57:20
合計ジャッジ時間 1,963 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<map>
#include<queue>
#include<string>
#include<sstream>
#include<cmath>
using namespace std;


int main(){

  int h, w;
  cin >> h >> w;
  vector< vector<int> > mp(h, vector<int>(w) );
  vector< vector<bool> > f(h, vector<bool>(w, false) );

  vector<int> s(2);
  vector<int> g(2);
  cin >> s[0] >> s[1] >> g[0] >> g[1];

  for(int i=0; i<h; i++){
    string tmp;
    cin >> tmp;
    for(int j=0; j<w; j++){
      mp[i][j] = tmp[j]-'0';
    }
  }
  s[0]--;
  s[1]--;
  g[0]--;
  g[1]--;

  int dx[4] = {1,0,-1,0};
  int dy[4] = {0,1,0,-1};
  queue< pair<int, int> > q;
  q.push(make_pair(s[0], s[1]));
  f[s[0]][s[1]] = true;

  while(true){
    if( q.empty() ) break;

    int x = q.front().first;
    int y = q.front().second;
    // cout << x << " " << y << endl;
    if( x == g[0] && y == g[1] ){
      cout << "YES" << endl;
      return 0;
    }

    q.pop();

    for(int i=0; i<4; i++){
      int tx = x + dx[i];
      int ty = y + dy[i];
      if( tx < 0 || tx >= h || ty < 0 || ty >= w ) continue;
      if( f[tx][ty] ) continue;

      if( mp[tx][ty] == mp[x][y]+1 || mp[tx][ty] == mp[x][y] || mp[tx][ty] == mp[x][y]-1 ){
        q.push(make_pair(tx, ty));
        f[tx][ty] = true;
      }
    }
    for(int i=0; i<4; i++){
      int tx = x + dx[i] + dx[i];
      int ty = y + dy[i] + dy[i];
      if( tx < 0 || tx >= h || ty < 0 || ty >= w ) continue;
      if( f[tx][ty] ) continue;

      if( mp[tx][ty] == mp[x][y] && mp[tx-dx[i]][ty-dy[i]] <= mp[x][y] ){
        q.push(make_pair(tx, ty));
        f[tx][ty] = true;
      }
    }
  }
  cout << "NO" << endl;


  return 0;
}
0