結果

問題 No.124 門松列(3)
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-09-12 16:49:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,350 bytes
コンパイル時間 907 ms
コンパイル使用メモリ 89,104 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-25 07:06:23
合計ジャッジ時間 2,065 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
10,880 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 17 ms
10,752 KB
testcase_04 AC 15 ms
10,880 KB
testcase_05 AC 15 ms
10,752 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 6 ms
6,144 KB
testcase_24 AC 8 ms
7,680 KB
testcase_25 AC 8 ms
7,552 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 16 ms
10,880 KB
testcase_29 AC 16 ms
10,752 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:18:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   18 |   int C, R; scanf("%d%d", &C, &R);
      |             ~~~~~^~~~~~~~~~~~~~~~
main.cpp:22:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   22 |       scanf("%d", &G[r][c]);
      |       ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <queue>
#include <tuple>
using namespace std;

                            // N  E  S  W
constexpr array<int, 4> dr = {-1, 0, 1, 0},
                        dc = { 0, 1, 0,-1};
constexpr int dr_size = dr.size();

bool is_kado(int a, int b, int c) {
  if(a == 0) { return true; }
  return a!=c && ((a>b&&b<c) || (a<b&&b>c));
}

int main(void) {
  int C, R; scanf("%d%d", &C, &R);
  vector<vector<int>> G(R, vector<int>(C, 0));
  for(int r=0; r<R; ++r) {
    for(int c=0; c<C; ++c) {
      scanf("%d", &G[r][c]);
    }
  }
  vector<vector<vector<vector<bool>>>> seen(R, vector<vector<vector<bool>>>(C, vector<vector<bool>>(10, vector<bool>(10, false)))); // seen[R][C][10][10]
  queue<tuple<int, int, int, int, int>> que;
  que.emplace(0, 0, 0, 0, G[0][0]);
  while(!que.empty()) {
    int cnt, r, c, b2, b1; tie(cnt, r, c, b2, b1) = que.front(); que.pop();
    if(seen[r][c][b2][b1]) { continue; }
    seen[r][c][b2][b1] = true;
    if(r == R-1 && c == C-1) {
      printf("%d\n", cnt);
      return 0;
    }
    for(int i=0; i<dr_size; ++i) {
      int nr = r + dr[i],
          nc = c + dc[i];
      if(!(0 <= nr && nr < R && 0 <= nc && nc < C)) { continue; }
      if(!is_kado(b2, b1, G[nr][nc])) { continue; }
      que.emplace(cnt+1, nr, nc, b1, G[nr][nc]);
    }
  }
  puts("-1");
  return 0;
}
0