結果

問題 No.340 雪の足跡
ユーザー noriocnorioc
提出日時 2016-01-30 03:17:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 295 ms / 1,000 ms
コード長 2,433 bytes
コンパイル時間 430 ms
コンパイル使用メモリ 49,432 KB
実行使用メモリ 15,312 KB
最終ジャッジ日時 2023-10-21 17:45:09
合計ジャッジ時間 4,872 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,416 KB
testcase_01 AC 2 ms
5,424 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
5,416 KB
testcase_04 AC 2 ms
5,344 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
5,416 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
5,368 KB
testcase_09 AC 2 ms
5,404 KB
testcase_10 AC 2 ms
5,372 KB
testcase_11 AC 3 ms
5,708 KB
testcase_12 AC 3 ms
5,700 KB
testcase_13 AC 3 ms
5,972 KB
testcase_14 AC 15 ms
11,152 KB
testcase_15 AC 18 ms
11,332 KB
testcase_16 AC 12 ms
8,848 KB
testcase_17 AC 23 ms
11,360 KB
testcase_18 AC 19 ms
11,360 KB
testcase_19 AC 21 ms
11,360 KB
testcase_20 AC 139 ms
15,268 KB
testcase_21 AC 98 ms
5,408 KB
testcase_22 AC 7 ms
12,112 KB
testcase_23 AC 166 ms
15,260 KB
testcase_24 AC 125 ms
15,264 KB
testcase_25 AC 130 ms
13,232 KB
testcase_26 AC 27 ms
11,064 KB
testcase_27 AC 6 ms
5,436 KB
testcase_28 AC 25 ms
11,120 KB
testcase_29 AC 223 ms
15,076 KB
testcase_30 AC 153 ms
12,260 KB
testcase_31 AC 258 ms
15,008 KB
testcase_32 AC 295 ms
15,312 KB
testcase_33 AC 239 ms
15,312 KB
testcase_34 AC 292 ms
15,312 KB
testcase_35 AC 183 ms
15,312 KB
testcase_36 AC 183 ms
15,312 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:67:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   67 |         scanf("%d %d %d", &w, &h, &n);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:72:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   72 |                 scanf("%d", &m);
      |                 ~~~~~^~~~~~~~~~
main.cpp:75:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   75 |                 scanf("%d", &fm);
      |                 ~~~~~^~~~~~~~~~~
main.cpp:78:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   78 |                         scanf("%d", &to);
      |                         ~~~~~^~~~~~~~~~~

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <map>
#include <cassert>
#include <queue>

using namespace std;

const int N = 1010;

struct D {
	int row, col, cost;
	D(int row, int col, int cost) : row(row), col(col), cost(cost) {}
};

void display(int rows, int cols, int g[][N][N]) {
	puts("--- row ---");
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			printf(" %d", g[0][i][j]);
		}
		puts("");
	}
	puts("--- col ---");
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			printf(" %d", g[1][i][j]);
		}
		puts("");
	}
}

int calc(int rows, int cols, int g[][N][N]) {
	queue<D> q;
	q.push(D(0, 0, 0));

	static int used[N][N] = {};
	while (!q.empty()) {
		D d = q.front(); q.pop();

		// printf("r:%d c:%d cost:%d\n", d.row, d.col, d.cost);
		if (d.row == rows-1 && d.col == cols-1) return d.cost;
		if (used[d.row][d.col]++) continue;

		// 上
		if (d.row > 0 && g[0][d.row-1][d.col] > 0)
			q.push(D(d.row-1, d.col, d.cost+1));

		// 下
		if (d.row+1 < rows && g[0][d.row][d.col] > 0)
			q.push(D(d.row+1, d.col, d.cost+1));

		// 右
		if (d.col+1 < cols && g[1][d.row][d.col] > 0)
			q.push(D(d.row, d.col+1, d.cost+1));

		// 左
		if (d.col > 0 && g[1][d.row][d.col-1] > 0)
			q.push(D(d.row, d.col-1, d.cost+1));
	}

	return -1;
}

int main() {
	int w, h, n;
	scanf("%d %d %d", &w, &h, &n);

	static int g[2][N][N] = {}; // [行or列どちら方向か][行][列]
	for (int i = 0; i < n; i++) {
		int m;
		scanf("%d", &m);

		int fm;
		scanf("%d", &fm);
		for (int j = 0; j < m; j++) {
			int to;
			scanf("%d", &to);
			// printf("%d -> %d\n", fm, to);

			int r1 = fm / w;
			int c1 = fm % w;
			int r2 = to / w;
			int c2 = to % w;

			// printf("(%d %d) -> (%d %d)\n", r1, c1, r2, c2);
			if (r1 != r2) {
				assert(c1 == c2);
				int fm = min(r1, r2);
				int to = max(r1, r2);
				g[0][fm][c1]++;
				g[0][to][c1]--;
			}
			else if (c1 != c2) {
				assert(r1 == r2);
				int fm = min(c1, c2);
				int to = max(c1, c2);
				g[1][r1][fm]++;
				g[1][r1][to]--;
			}
			else assert(false);

			fm = to;
		}
	}

	// 行方向、列方向を足し上げる
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			// 行方向
			if (i > 0) g[0][i][j] += g[0][i-1][j];
			// 列方向
			if (j > 0) g[1][i][j] += g[1][i][j-1];
		}
	}

	// display(h, w, g);

	int ans = calc(h, w, g);
	if (ans == -1) {
		puts("Odekakedekinai..");
	}
	else {
		printf("%d\n", ans);
	}
}
0