結果

問題 No.124 門松列(3)
ユーザー purple_jwlpurple_jwl
提出日時 2015-04-12 23:53:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 1,683 bytes
コンパイル時間 1,339 ms
コンパイル使用メモリ 150,728 KB
実行使用メモリ 7,376 KB
最終ジャッジ日時 2023-09-17 19:49:47
合計ジャッジ時間 2,561 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define REP(i, x, n) for(int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

typedef pair<P, int> State; // x, y, b2, b1

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

int W, H;
int M[100][100];
int minCost[100][100][10][10];

bool isKSeq(int b2, int b1, int b) {
  if(b2 == b1 || b1 == b || b == b2) return false;
  if(b2 < b1 && b1 < b) return false;
  if(b2 > b1 && b1 > b) return false;
  return true;
}

int solve() {
  rep(i, H) rep(j, W) rep(k, 10) rep(l, 10) {
    minCost[i][j][k][l] = 100000;
  }

  queue<State> Q;
  Q.push(mp(mp(0, 0), 0));
  minCost[0][0][0][M[0][0]] = 0;

  while(!Q.empty()) {
    State stt = Q.front();
    Q.pop();

    rep(i, 4) {
      int y = stt.F.S;
      int x = stt.F.F;
      int ny = y + dy[i];
      int nx = x + dx[i];
      int b = stt.S;
      if(!(0 <= ny && ny < H)) continue;
      if(!(0 <= nx && nx < W)) continue;
      if(b != 0 && !isKSeq(b, M[y][x], M[ny][nx])) continue;
      if(minCost[ny][nx][M[y][x]][M[ny][nx]] <= minCost[y][x][b][M[y][x]] + 1) continue;
      minCost[ny][nx][M[y][x]][M[ny][nx]] = minCost[y][x][b][M[y][x]] + 1;
      Q.push(mp(mp(nx, ny), M[y][x]));

      if(ny == H - 1 && nx == W - 1) return minCost[ny][nx][M[y][x]][M[ny][nx]];
    }
  }  
  
  return -1;
}

int main() {
  // ios_base::sync_with_stdio(false);
  cin >> W >> H;
  rep(i, H) rep(j, W) cin >> M[i][j];
  cout << solve() << endl;
  return 0;
}
0