結果

問題 No.424 立体迷路
ユーザー tadanoningentadanoningen
提出日時 2020-10-10 17:54:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,543 bytes
コンパイル時間 982 ms
コンパイル使用メモリ 88,044 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 21:34:26
合計ジャッジ時間 2,805 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<queue>

using namespace std;

typedef pair<int,int> P;

int h,w;
int sx,sy,gx,gy;
vector<vector<int>> b(55,vector<int>(55,-1));
vector<vector<bool>> used(55,vector<bool>(55,false));
const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};
const int dx2[] = {2,0,-2,0};
const int dy2[] = {0,2,0,-2};

bool bfs(){
  queue<P> que;
  que.push(P(sx,sy));
  used[sx][sy] = true;
  while(!que.empty()){

    P cur = que.front();que.pop();
    if(cur.first == gx && cur.second == gy){
      return true;
    }

    for(int i=0;i < 4;i++){
      int nx = cur.first + dx[i],ny = cur.second + dy[i];
      int nx2 = cur.first + dx2[i],ny2 = cur.second + dy2[i];

      if(nx > 0 && nx <= h && ny > 0 && ny <= w){
        int height = b[nx][ny] - b[cur.first][cur.second];
        int height2 = b[nx2][ny2] - b[cur.first][cur.second];
        if(height == 0 || height == 1 || height == -1){
          if(!used[nx][ny]) que.push(P(nx,ny));
          used[nx][ny] = true;
        }else if(height <= -1 && height2 == 0){
          if(!used[nx2][ny2]) que.push(P(nx2,ny2));
          used[nx2][ny2] = true;
        }
      }
    }

  }
  return false;
}

int main(){
  cin >> h >> w;
  cin >> sx >> sy >> gx >> gy;
  for(int i=1;i <= h;i++){
    string s;
    cin >> s;
    for(int j=1;j <= w;j++){
      b[i][j] = s[j-1] - '0';
    }
  }
  if(bfs()) cout << "YES" << endl;
  else cout << "NO" << endl;
  return 0;
}
0