結果

問題 No.124 門松列(3)
ユーザー たこしたこし
提出日時 2015-06-12 17:36:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 2,180 bytes
コンパイル時間 748 ms
コンパイル使用メモリ 95,424 KB
実行使用メモリ 12,472 KB
最終ジャッジ日時 2023-09-20 21:03:54
合計ジャッジ時間 2,186 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
12,256 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 8 ms
12,396 KB
testcase_04 AC 8 ms
12,444 KB
testcase_05 AC 8 ms
12,312 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 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,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 3 ms
5,848 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 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 5 ms
10,600 KB
testcase_24 AC 6 ms
11,040 KB
testcase_25 AC 6 ms
9,172 KB
testcase_26 AC 4 ms
6,696 KB
testcase_27 AC 3 ms
6,440 KB
testcase_28 AC 8 ms
12,472 KB
testcase_29 AC 8 ms
12,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>
#include <complex>

#define INF_MIN 100000000
#define INF 1145141919
#define INF_MAX 2147483647
#define LL_MAX 9223372036854775807
#define EPS 1e-10
#define PI acos(-1)
#define LL long long

using namespace std;

#define MAX_W 101
#define MAX_H 101

int W, H;
int maze[MAX_H][MAX_W];
int dp[MAX_W][MAX_H][15][15];

void init(int x, int y){

  for(int i = 0; i < 15; i++){
    for(int j = 0; j < 15; j++){
      dp[y][x][i][j] = INF;
    }
  }

}

typedef pair<int, int> P;

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

bool checkKado(int a, int b, int c){

  return max(a, max(b,c)) == b || min(a, min(b,c)) == b;

}

int solve(){

  dp[0][0][10][maze[0][0]] = 0;

  queue<P> PosQue;
  queue<P> KadoQue;

  PosQue.push(P(0, 0));
  KadoQue.push(P(10, maze[0][0]));

  while(PosQue.size()){
    
    P pos = PosQue.front();
    P kado = KadoQue.front();
    PosQue.pop();
    KadoQue.pop();

    int x = pos.first, y = pos.second;
    int a = kado.first, b = kado.second;

    if(x == W-1 && y == H-1)
      return dp[y][x][a][b];

    for(int i = 0; i < 4; i++){
      int nx = x + dx[i];
      int ny = y + dy[i];
      if(0 <= nx && nx < W && 0 <= ny && ny < H){
	int nextKado = maze[ny][nx];
	if(a != b && b != nextKado && nextKado != a &&
	   (dp[y][x][a][b] == 0 || checkKado(a, b, nextKado)) &&
	   dp[ny][nx][b][nextKado] > dp[y][x][a][b] + 1){
	  //printf("(%d, %d) := %d %d %d\n", nx, ny, a, b, nextKado);
	  dp[ny][nx][b][nextKado] = dp[y][x][a][b] + 1;
	  PosQue.push(P(nx, ny));
	  KadoQue.push(P(b, nextKado));

	}
      }
    }

  }

  return -1;
  
}

int main(){

  cin >> W >> H;
  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      cin >> maze[i][j];
      init(j, i);
    }
  }

  cout << solve() << endl;

  return 0;

}
0