結果

問題 No.340 雪の足跡
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-08-09 20:47:11
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,612 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 71,476 KB
実行使用メモリ 823,276 KB
最終ジャッジ日時 2024-04-24 23:17:56
合計ジャッジ時間 8,813 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
30,976 KB
testcase_01 AC 21 ms
30,976 KB
testcase_02 AC 22 ms
30,848 KB
testcase_03 AC 21 ms
30,848 KB
testcase_04 AC 21 ms
30,848 KB
testcase_05 AC 19 ms
26,752 KB
testcase_06 AC 21 ms
30,976 KB
testcase_07 AC 21 ms
30,592 KB
testcase_08 AC 22 ms
30,720 KB
testcase_09 AC 22 ms
30,976 KB
testcase_10 AC 21 ms
30,720 KB
testcase_11 AC 27 ms
32,000 KB
testcase_12 AC 25 ms
31,744 KB
testcase_13 AC 27 ms
32,256 KB
testcase_14 AC 318 ms
82,560 KB
testcase_15 AC 409 ms
104,320 KB
testcase_16 AC 194 ms
70,400 KB
testcase_17 AC 615 ms
131,840 KB
testcase_18 AC 481 ms
110,592 KB
testcase_19 AC 643 ms
131,328 KB
testcase_20 MLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

//横縦横縦
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector<int> b[1001];
vector<int> miti[1000000];
int memo[1001][1001];//(0, 0)からの最短経路を保存しておく
int main(void){
	int w, h, n; cin >> w >> h >> n;
	if(w == 1 && h == 1){
		printf("0\n"); return 0;
	}
	rep(i, n){
		int m; cin >> m;
		rep(j, m + 1){
			int tmp; cin >> tmp;
			b[i].push_back(tmp);
		}
	}

	//通れる道を探す
	for (int i = 0; i < n; ++i){
		for (int j = 0; j < b[i].size() - 1; ++j){
			miti[b[i][j]].push_back(b[i][j + 1]);
			miti[b[i][j + 1]].push_back(b[i][j]);
		}
	}

	//通れる道を探す
	for (int i = 0; i < n; ++i){
		for (int j = 0; j < b[i].size() - 1; ++j){
			int ma, mi;
			if(b[i][j] > b[i][j + 1]){
				ma = b[i][j]; mi = b[i][j + 1];
			}else{
				mi = b[i][j]; ma = b[i][j + 1];
			}
			// printf("ma %d mi %d\n", ma, mi);
			if((ma - mi) % w == 0){
				// 縦方向の動く
				for (int k = mi; k <= ma - w; k += w){
					// printf("%d %d \n", k, k + w);
					miti[k].push_back(k + w);
					miti[k + w].push_back(k);
				}
			}else{
				// 横方向に動く
				for (int k = mi; k <= ma - 1; ++k){
					// printf("%d %d\n", k, k + 1);
					miti[k].push_back(k + 1);
					miti[k + 1].push_back(k);
				}
			}
		}
	}

	/*
	rep(i, 20){
		printf("%d : ", i);
		rep(j, miti[i].size()){
			printf("%d ", miti[i][j]);
		}
		printf("\n");
	}
	*/

	rep(i, 1001)rep(j, 1001) memo[i][j] = -1;//初期化
	memo[0][0] = 0;
	queue<pair<int, int> > q;
	q.push(make_pair(0, 0));
	// int cnt = 0;
	while(!q.empty()){
		// cnt++;
		pair<int, int> now = q.front(); q.pop();
		// printf("first %d  second %d\n", now.first, now.second);
		for (int i = 0; i < 4; ++i){
			int nowy = now.first + dy[i], nowx = now.second + dx[i];//座標
			if(0 <= nowy && nowy < h && 0 <= nowx && nowx < w){
				if(memo[nowy][nowx] == -1){//4方向に動いた点はまだたどり着いていない
					int snum = now.first * w + now.second;//始点の座標を数字に変換
					int dnum = nowy      * w + nowx;//目的地の座標を数字に変換
					for(int u : miti[snum]){
						if(u == dnum){//4方向に動いた点は歩くことができた
							memo[nowy][nowx] = memo[now.first][now.second] + 1;
							if(nowy == h - 1 && nowx == w - 1){
								printf("%d\n", memo[nowy][nowx]); return 0;
							}
							q.push(make_pair(nowy, nowx));
						}
					}
				}
			}
		}
	}
	printf("Odekakedekinai..\n");
	return 0;
}
0