結果

問題 No.340 雪の足跡
ユーザー fiordfiord
提出日時 2016-01-31 10:37:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 445 ms / 1,000 ms
コード長 1,671 bytes
コンパイル時間 1,912 ms
コンパイル使用メモリ 173,640 KB
実行使用メモリ 15,428 KB
最終ジャッジ日時 2023-10-21 18:18:32
合計ジャッジ時間 8,175 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,792 KB
testcase_01 AC 3 ms
7,796 KB
testcase_02 AC 2 ms
5,760 KB
testcase_03 AC 2 ms
7,792 KB
testcase_04 AC 2 ms
7,800 KB
testcase_05 AC 2 ms
5,736 KB
testcase_06 AC 2 ms
7,796 KB
testcase_07 AC 3 ms
5,764 KB
testcase_08 AC 2 ms
7,804 KB
testcase_09 AC 2 ms
7,784 KB
testcase_10 AC 2 ms
7,808 KB
testcase_11 AC 4 ms
7,940 KB
testcase_12 AC 3 ms
7,936 KB
testcase_13 AC 4 ms
8,068 KB
testcase_14 AC 26 ms
12,552 KB
testcase_15 AC 31 ms
14,604 KB
testcase_16 AC 22 ms
10,504 KB
testcase_17 AC 40 ms
14,604 KB
testcase_18 AC 33 ms
14,604 KB
testcase_19 AC 39 ms
14,604 KB
testcase_20 AC 313 ms
15,416 KB
testcase_21 AC 195 ms
7,796 KB
testcase_22 AC 9 ms
15,408 KB
testcase_23 AC 335 ms
15,416 KB
testcase_24 AC 286 ms
15,416 KB
testcase_25 AC 322 ms
15,420 KB
testcase_26 AC 53 ms
12,556 KB
testcase_27 AC 11 ms
7,812 KB
testcase_28 AC 48 ms
12,556 KB
testcase_29 AC 361 ms
15,212 KB
testcase_30 AC 252 ms
14,616 KB
testcase_31 AC 385 ms
15,132 KB
testcase_32 AC 444 ms
15,428 KB
testcase_33 AC 360 ms
15,428 KB
testcase_34 AC 445 ms
15,428 KB
testcase_35 AC 406 ms
15,428 KB
testcase_36 AC 407 ms
15,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fst first
#define sec second
int dist[1000][1000];
int lr[1000][1000],ud[1000][1000];
int main(){
	int w,h,n;	cin>>w>>h>>n;
	for(int i=0;i<n;i++){
		int m;	cin>>m;
		vector<pair<int,int>> b;
		for(int j=0;j<m+1;j++){
			int num;	cin>>num;
			b.push_back(make_pair(num/w,num%w));//y,x
		}
		for(int j=1;j<=m;j++){
			if(b[j].fst==b[j-1].fst){//横方向の移動
				int sml=min(b[j].sec,b[j-1].sec);
				int big=max(b[j].sec,b[j-1].sec);
				lr[b[j].fst][sml]++;	lr[b[j].fst][big]--;
			}
			else{
				int sml=min(b[j].fst,b[j-1].fst);
				int big=max(b[j].fst,b[j-1].fst);
				ud[sml][b[j].sec]++;	ud[big][b[j].sec]--;
			}
		}
	}
	for(int i=0;i<h;i++){
		for(int j=1;j<w;j++)	lr[i][j]+=lr[i][j-1];
	}
	for(int i=1;i<h;i++){
		for(int j=0;j<w;j++)	ud[i][j]+=ud[i-1][j];
	}
	for(int i=0;i<h;i++)	for(int j=0;j<w;j++)	dist[i][j]=INT_MAX;
	dist[0][0]=0;
	priority_queue<tuple<int,int,int>> pq;
	pq.push(make_tuple(-0,0,0));
	while(!pq.empty()){
		auto it=pq.top();	pq.pop();
		int cost=-get<0>(it);
		int y=get<1>(it),x=get<2>(it);
		if(cost>dist[y][x])	continue;
		if(y==h-1&&x==w-1){
			cout<<cost<<endl;
			return 0;
		}
		cost++;
		if(lr[y][x]>0&&dist[y][x+1]>cost){
			dist[y][x+1]=cost;
			pq.push(make_tuple(-cost,y,x+1));
		}
		if(x>0&&lr[y][x-1]>0&&dist[y][x-1]>cost){
			dist[y][x-1]=cost;
			pq.push(make_tuple(-cost,y,x-1));
		}
		if(ud[y][x]>0&&dist[y+1][x]>cost){
			dist[y+1][x]=cost;
			pq.push(make_tuple(-cost,y+1,x));
		}
		if(y>0&&ud[y-1][x]>0&&dist[y-1][x]>cost){
			dist[y-1][x]=cost;
			pq.push(make_tuple(-cost,y-1,x));
		}
	}
	cout<<"Odekakedekinai.."<<endl;
	return 0;
}
0