結果

問題 No.340 雪の足跡
ユーザー startcppstartcpp
提出日時 2016-01-30 00:54:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 327 ms / 1,000 ms
コード長 3,096 bytes
コンパイル時間 652 ms
コンパイル使用メモリ 71,216 KB
実行使用メモリ 50,824 KB
最終ジャッジ日時 2023-10-21 17:40:08
合計ジャッジ時間 5,925 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
22,952 KB
testcase_01 AC 8 ms
22,952 KB
testcase_02 AC 7 ms
20,920 KB
testcase_03 AC 8 ms
22,952 KB
testcase_04 AC 8 ms
22,952 KB
testcase_05 AC 7 ms
20,904 KB
testcase_06 AC 8 ms
22,952 KB
testcase_07 AC 7 ms
20,968 KB
testcase_08 AC 8 ms
22,952 KB
testcase_09 AC 8 ms
22,952 KB
testcase_10 AC 8 ms
22,956 KB
testcase_11 AC 9 ms
25,000 KB
testcase_12 AC 9 ms
25,000 KB
testcase_13 AC 9 ms
25,000 KB
testcase_14 AC 25 ms
31,148 KB
testcase_15 AC 27 ms
33,196 KB
testcase_16 AC 19 ms
29,100 KB
testcase_17 AC 32 ms
33,196 KB
testcase_18 AC 29 ms
33,196 KB
testcase_19 AC 32 ms
33,196 KB
testcase_20 AC 206 ms
50,804 KB
testcase_21 AC 101 ms
22,952 KB
testcase_22 AC 61 ms
50,784 KB
testcase_23 AC 210 ms
50,804 KB
testcase_24 AC 169 ms
50,804 KB
testcase_25 AC 177 ms
50,804 KB
testcase_26 AC 31 ms
31,144 KB
testcase_27 AC 12 ms
22,952 KB
testcase_28 AC 31 ms
31,148 KB
testcase_29 AC 235 ms
49,952 KB
testcase_30 AC 163 ms
39,344 KB
testcase_31 AC 282 ms
49,636 KB
testcase_32 AC 311 ms
50,824 KB
testcase_33 AC 291 ms
50,824 KB
testcase_34 AC 327 ms
50,824 KB
testcase_35 AC 296 ms
50,824 KB
testcase_36 AC 298 ms
50,824 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void make()’:
main.cpp:43:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   43 |         scanf("%d%d%d", &w, &h, &n);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:48:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   48 |                         scanf("%d", &t);
      |                         ~~~~~^~~~~~~~~~

ソースコード

diff #

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

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

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(){
	scanf("%d%d%d", &w, &h, &n);
	for( int i = 0; i < n; i++ ){
		cin >> m; m++;
		for( int j = 0; j < m; j++ ){
			int t;
			scanf("%d", &t);
			by[j] = t / w; by[j] *= 2;
			bx[j] = t % w; bx[j] *= 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] || (h == 1 && w == 1) ){
		que.push(T(0, 0, 0));
		dp[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();
		
		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));
				dp[ny][nx] = dist + 1;
			}
		}
	}
	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 && !(h == 1 && w == 1) ) cout << "Odekakedekinai.." << endl;
	else cout << res << endl;
	return 0;
}
0