結果

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

テストケース

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

ソースコード

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