Submission #544608

#TimeUsernameProblemLanguageResultExecution timeMemory
544608rainboyTracks in the Snow (BOI13_tracks)C11
100 / 100
754 ms105000 KiB
#include <stdio.h>

#define N	4000
#define M	4000

int di[] = { -1, 1, 0, 0 };
int dj[] = { 0, 0, -1, 1 };

int main() {
	static char cc[N][M + 1];
	static int dd[N][M], qu[N * M * 2];
	int n, m, i, j, d, head, cnt;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		scanf("%s", cc[i]);
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			dd[i][j] = n * m + 1;
	head = n * m, cnt = 0;
	dd[0][0] = 1, qu[head + cnt++] = 0 * m + 0;
	while (cnt) {
		int ij, h;

		ij = qu[cnt--, head++], i = ij / m, j = ij % m, d = dd[i][j];
		for (h = 0; h < 4; h++) {
			int i_ = i + di[h], j_ = j + dj[h];

			if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m && cc[i_][j_] != '.') {
				if (cc[i_][j_] == cc[i][j]) {
					if (dd[i_][j_] > d)
						dd[i_][j_] = d, qu[cnt++, --head] = i_ * m + j_;
				} else {
					if (dd[i_][j_] > d + 1)
						dd[i_][j_] = d + 1, qu[head + cnt++] = i_ * m + j_;
				}
			}
		}
	}
	printf("%d\n", d);
	return 0;
}

Compilation message (stderr)

tracks.c: In function 'main':
tracks.c:14:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
tracks.c:16:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |   scanf("%s", cc[i]);
      |   ^~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...