結果

問題 No.124 門松列(3)
ユーザー tkzw_21tkzw_21
提出日時 2015-01-12 01:25:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,331 bytes
コンパイル時間 1,280 ms
コンパイル使用メモリ 152,564 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 04:57:17
合計ジャッジ時間 2,582 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 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,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0