結果

問題 No.340 雪の足跡
ユーザー startcppstartcpp
提出日時 2016-01-30 00:12:54
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,925 bytes
コンパイル時間 817 ms
コンパイル使用メモリ 71,312 KB
実行使用メモリ 50,608 KB
最終ジャッジ日時 2023-10-21 17:37:45
合計ジャッジ時間 6,309 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
22,736 KB
testcase_01 AC 9 ms
22,736 KB
testcase_02 AC 9 ms
20,704 KB
testcase_03 AC 9 ms
22,736 KB
testcase_04 AC 9 ms
22,736 KB
testcase_05 WA -
testcase_06 AC 9 ms
22,736 KB
testcase_07 AC 9 ms
20,752 KB
testcase_08 AC 9 ms
22,736 KB
testcase_09 AC 10 ms
22,736 KB
testcase_10 AC 9 ms
22,740 KB
testcase_11 AC 11 ms
24,784 KB
testcase_12 AC 11 ms
24,784 KB
testcase_13 AC 12 ms
24,784 KB
testcase_14 AC 35 ms
30,932 KB
testcase_15 AC 42 ms
32,980 KB
testcase_16 AC 30 ms
28,884 KB
testcase_17 AC 50 ms
32,980 KB
testcase_18 AC 44 ms
32,984 KB
testcase_19 AC 50 ms
32,984 KB
testcase_20 RE -
testcase_21 WA -
testcase_22 AC 62 ms
50,568 KB
testcase_23 WA -
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 59 ms
30,928 KB
testcase_27 AC 18 ms
22,736 KB
testcase_28 AC 55 ms
30,932 KB
testcase_29 RE -
testcase_30 AC 286 ms
39,132 KB
testcase_31 RE -
testcase_32 RE -
testcase_33 AC 437 ms
50,608 KB
testcase_34 RE -
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

//基本的には、縦横の累積和を使って通行可マス高速列挙と幅優先探索なのだが、実装時にh * wマスを2h * 2wマスに変換したほうが楽。
//2h * 2wマスにすれば、1回以上大人が訪れたマスがそのまま訪れることの出来るマスになる!!(行けない方向には0が埋まっているのだよ)
//m-m-m
//|
//m-m-m
//    |
//m-m-m
//例えば、こういうケースを考える。このケースでは、左下マスから上に行ったり、右上マスから下に行ったりしないようにするのだが、
//2h * 2wマスにしておけば、(元データにおける)隣接マス間において、間のマスの訪れた回数が0⇔通行不可になるので処理が簡単。
//実装が本質だってはっきりわかんだね!(間に合わなかった)
//デバッグ文消し忘れるとか恥ずかしい
//
#include <iostream>
#include <queue>
#include <tuple>
using namespace std;

int w, h, n;
int m;
int by[1000], bx[1000];

int rx[2002][2002];
int ry[2002][2002];
int dp[2002][2002];

const int dy[4] = {1, 0, -1, 0};
const int dx[4] = {0, 1, 0, -1};
bool isRange(int r, int c){
	return (r >= 0 && r < 2 * h - 1 && c >= 0 && c < 2 * w - 1 );
}

void init(){
	for( int i = 0; i < 2002; i++ )
		for( int j = 0; j < 2002; j++ )
			dp[i][j] = 114514194;
}

void make(){
	cin >> w >> h >> n;
	for( int i = 0; i < n; i++ ){
		cin >> m; m++;
		for( int j = 0; j < m; j++ ){
			int t;
			cin >> t;
			by[j] = t / w; by[j] *= 2;
			bx[j] = t % w; bx[j] *= 2;
		}
		//同じ位置にたいして2度足しているが、通行回数を求めるわけではないので問題ない
		for( int j = 1; j < m; j++ ){
			if( by[j-1] == by[j] ){
				//横方向への移動
				rx[by[j-1]][min(bx[j-1], bx[j])]++;
				rx[by[j-1]][max(bx[j-1], bx[j]) + 1]--;
			} else {
				//縦方向への移動
				ry[min(by[j-1], by[j])][bx[j-1]]++;
				ry[max(by[j-1], by[j]) + 1][bx[j-1]]--;
			}
		}
	}
	
	for( int i = 0; i < 2*h; i++ )
		for( int j = 1; j < 2*w; j++ )
			rx[i][j] += rx[i][j-1];
	for( int j = 0; j < 2*w; j++ )
		for( int i = 1; i < 2*h; i++ )
			ry[i][j] += ry[i-1][j];
}

int bfs(){
	typedef tuple<int, int, int> T;	//コスト, y, x
	queue<T> que;
	
	if( ry[0][0] || rx[0][0] )
		que.push(T(0, 0, 0));
	while( !que.empty() ){
		T now = que.front();
		int dist = get<0>(now);
		int y = get<1>(now);
		int x = get<2>(now);
		que.pop();
		
		if( dp[y][x] <= dist )
			continue;
		dp[y][x] = dist;
		
		for( int i = 0; i < 4; i++ ){
			int ny = y + dy[i];
			int nx = x + dx[i];
			if( isRange(ny, nx) && (ry[ny][nx] || rx[ny][nx]) && dp[ny][nx] > dist + 1 ){
				que.push(T(dist + 1, ny, nx));
			}
		}
	}
	if( dp[2*h-2][2*w-2] == 114514194 ) return -1;
	return dp[2*h-2][2*w-2] / 2;
}

int main(){
	init();
	make();
	int res = bfs();
	if( res == -1 ) cout << "Odekakedekinai.." << endl;
	else cout << res << endl;
	return 0;
}
0