結果

問題 No.340 雪の足跡
ユーザー tsukiotsukio
提出日時 2016-06-10 23:25:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 374 ms / 1,000 ms
コード長 2,018 bytes
コンパイル時間 531 ms
コンパイル使用メモリ 65,932 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2024-04-17 18:35:24
合計ジャッジ時間 7,111 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 21 ms
6,528 KB
testcase_15 AC 26 ms
6,656 KB
testcase_16 AC 19 ms
5,504 KB
testcase_17 AC 33 ms
6,912 KB
testcase_18 AC 26 ms
7,040 KB
testcase_19 AC 32 ms
7,040 KB
testcase_20 AC 301 ms
15,104 KB
testcase_21 AC 193 ms
5,376 KB
testcase_22 AC 13 ms
14,936 KB
testcase_23 AC 361 ms
14,988 KB
testcase_24 AC 289 ms
15,116 KB
testcase_25 AC 317 ms
15,232 KB
testcase_26 AC 52 ms
5,504 KB
testcase_27 AC 11 ms
5,376 KB
testcase_28 AC 45 ms
6,144 KB
testcase_29 AC 319 ms
13,312 KB
testcase_30 AC 223 ms
10,880 KB
testcase_31 AC 324 ms
14,592 KB
testcase_32 AC 374 ms
15,112 KB
testcase_33 AC 295 ms
15,104 KB
testcase_34 AC 370 ms
15,232 KB
testcase_35 AC 340 ms
15,104 KB
testcase_36 AC 338 ms
15,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <climits>
using namespace std;

struct Node {
	int x;
	int y;
	int cost;
};

int x_axis[1000][1000];
int y_axis[1000][1000];
int dp[1000][1000];
int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };

int main() {
	int w, h, n;
	cin >> w >> h >> n;

	int b[1001];

	for (int i = 0; i < n; i++) {
		int m;
		cin >> m;

		for (int k = 0; k < m + 1; k++) {
			cin >> b[k];
		}

		for (int k = 0; k < m; k++) {
			int small_no = b[k];
			int large_no = b[k + 1];

			if (small_no > large_no) {
				swap(small_no, large_no);
			}

			if ((large_no - small_no) % w == 0) {
				int x = small_no % w;
				y_axis[x][small_no / w]++;
				y_axis[x][large_no / w]--;
			}
			else {
				int y = small_no / w;
				x_axis[y][small_no % w]++;
				x_axis[y][large_no % w]--;
			}
		}
	}

	for (int x = 0; x < w; x++) {
		for (int y = 0; y < h - 1; y++) {
			y_axis[x][y + 1] += y_axis[x][y];
		}
	}

	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w - 1; x++) {
			x_axis[y][x + 1] += x_axis[y][x];
		}
	}

	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			dp[y][x] = INT_MAX;
		}
	}
	dp[0][0] = 0;

	queue<Node> nodes;
	nodes.emplace(Node{ 0, 0, 0 });

	while (!nodes.empty()) {
		Node node = nodes.front(); nodes.pop();

		for (int i = 0; i < 4; i++) {
			int nx = node.x + dx[i];
			int ny = node.y + dy[i];
			
			if(ny < 0 || ny >= h || nx < 0 || nx >= w) 
				continue;

			int x, y;
			switch (i) {
			case 0:
			case 1:
				// 横
				x = (nx + node.x) / 2;
				if (x_axis[ny][x] <= 0) continue;
				break;
			case 2:
			case 3:
				// 縦
				y = (ny + node.y) / 2;
				if (y_axis[nx][y] <= 0) continue;
				break;
			default:
				break;
			}
			
			int next_cost = node.cost + 1;
			if (dp[ny][nx] <= next_cost)
				continue;

			dp[ny][nx] = next_cost;
			nodes.emplace(Node{ nx, ny, next_cost });
		}
	}

	int result = dp[h - 1][w - 1];
	if (result == INT_MAX) {
		cout << "Odekakedekinai.." << endl;
	}
	else {
		cout << result << endl;
	}

	return 0;
}
0