結果

問題 No.124 門松列(3)
ユーザー tkzw_21tkzw_21
提出日時 2015-01-12 01:14:52
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,338 bytes
コンパイル時間 1,264 ms
コンパイル使用メモリ 152,084 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 04:56:41
合計ジャッジ時間 2,453 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 WA -
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 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 2 ms
4,384 KB
testcase_09 WA -
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 WA -
testcase_17 AC 2 ms
4,380 KB
testcase_18 WA -
testcase_19 AC 2 ms
4,376 KB
testcase_20 WA -
testcase_21 AC 2 ms
4,380 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,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,376 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];

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] = true;
		//cout << y << " " << x << endl;
		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])continue;
			used[ny][nx] = 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