結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  たこし | 
| 提出日時 | 2015-06-12 17:36:21 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 8 ms / 5,000 ms | 
| コード長 | 2,180 bytes | 
| コンパイル時間 | 818 ms | 
| コンパイル使用メモリ | 98,664 KB | 
| 実行使用メモリ | 12,456 KB | 
| 最終ジャッジ日時 | 2024-07-06 15:52:01 | 
| 合計ジャッジ時間 | 1,940 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
ソースコード
#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;
}
            
            
            
        