結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  tkzw_21 | 
| 提出日時 | 2015-01-12 01:25:01 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 1,331 bytes | 
| コンパイル時間 | 1,380 ms | 
| コンパイル使用メモリ | 166,808 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-06-22 04:26:11 | 
| 合計ジャッジ時間 | 2,337 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
ソースコード
#include <bits/stdc++.h>
#define INF INT_MAX / 2
#define MOD 1000000007
using namespace std;
typedef pair<int,int> PI;
typedef pair<int,pair<int,int>> PIPII;
typedef long long ll;
bool isKadomatsuSequence(int a2,int a1,int a0){
	if(a2 == -1)return true;
	if((a1 < a0 && a1 < a2 || a1 > a2 && a1 > a0) && a2 != a0)return true;
	return false;
}
int dy[] = {0,1,0,-1};
int dx[] = {1,0,-1,0};
int m[101][101];
bool used[101][101][10];
int w,h;
int bfs(){
	priority_queue<pair<int,pair<PI,int>>,vector<pair<int,pair<PI,int>>>,greater< pair<int,pair<PI,int>> >> pq;
	pq.push(make_pair(0,make_pair(make_pair(0,0),-1)));
	while(!pq.empty()){
		pair<int,pair<PI,int>> p = pq.top();pq.pop();
		int dist = p.first;
		int y = p.second.first.first;
		int x = p.second.first.second;
		int prev = p.second.second;
		if(y == h-1 && x == w-1)return dist;
		used[y][x][prev] = true;
		for(int i=0;i<4;i++){
			int ny = y + dy[i],nx = x + dx[i];
			if(ny<0||ny>=h||nx<0||nx>=w|| !isKadomatsuSequence(prev,m[y][x],m[ny][nx]) )continue;
			if(used[ny][nx][m[y][x]])continue;
			used[ny][nx][m[y][x]] = true;
			pq.push(make_pair(dist+1,make_pair(make_pair(ny,nx),m[y][x])));
		}
	}
	return -1;
}
int main(void) {
	cin >> w >> h;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin >> m[i][j];
		}
	}
	int ans = bfs();
	cout << ans << endl;
}
            
            
            
        