結果

問題 No.424 立体迷路
ユーザー okayunonaha
提出日時 2016-09-23 00:20:38
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,632 bytes
コンパイル時間 445 ms
コンパイル使用メモリ 57,628 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-05 07:07:19
合計ジャッジ時間 1,283 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
using namespace std;

string b[52];
bool is_search[52][52];
int xvec[4] = {-1, 0, 1, 0}, yvec[4] = {0, -1, 0, 1};
int xvec2[4] = {-2, 0, 2, 0}, yvec2[4] = {0, -2, 0, 2};
int h, w;

void dfs(int y, int x) {
  //cout << "now is " << x << " " << y << endl;
  is_search[y][x] = true;

  char nows = b[y][x];
  int nowtmp = (int)nows - '0';

  for (int i = 0; i < 4; i++) {
    if (x + xvec[i] < 0 || x + xvec[i] > w-1 || y + yvec[i] < 0 || y + yvec[i] > h-1) continue;

    char stmp = b[y + yvec[i]][x + xvec[i]];
    int tmp = (int)stmp - '0';
    //cout << tmp << endl;;

    if(is_search[y + yvec[i]][x + xvec[i]] == false && (nowtmp == tmp + 1 || nowtmp == tmp - 1 || nowtmp == tmp)) {
      dfs(y + yvec[i], x + xvec[i]);
    }

    if (x + xvec2[i] < 0 || x + xvec2[i] > w-1 || y + yvec2[i] < 0 || y + yvec2[i] > h-1) continue;

    
    char sstmp = b[y + yvec2[i]][x + xvec2[i]];
    int ttmp = (int)sstmp - '0';
    //cout << ttmp << endl;;

    if(is_search[y + yvec2[i]][x + xvec2[i]] == false && nowtmp == ttmp) {
      if (ttmp > tmp) dfs(y + yvec2[i], x + xvec2[i]);
    }
  }
}

int main(void) {
  int sx, sy, gx, gy;

  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      is_search[i][j] = false;
    }
  }

  cin >> sx >> sy >> gx >> gy;
  for (int i = 0; i < h; i++) {
      cin >> b[i];
  }

  dfs(sx-1, sy-1);

  /*for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << is_search[i][j] << " ";
    }
    cout << endl;
  }*/

  if(is_search[gx-1][gy-1]) cout << "YES" << endl;
  else cout << "NO" << endl;

  return 0;
}
0