QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#165014#6739. Teleportmendicillin2#AC ✓420ms127708kbC++172.7kb2023-09-05 15:27:452023-09-05 15:27:45

Judging History

你现在查看的是最新测评结果

  • [2023-09-05 15:27:45]
  • 评测
  • 测评结果:AC
  • 用时:420ms
  • 内存:127708kb
  • [2023-09-05 15:27:45]
  • 提交

answer

//#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;

template <class T> int sz(T&& a) { return int(size(forward<T>(a))); }

template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;

using ll = int64_t;
using vi = vc<int>;

mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

template <class F>
struct ycr {
	F f;
	template <class T> explicit ycr(T&& f_) : f(forward<T>(f_)) {}

	template <class... Args> decltype(auto) operator()(Args&&... args) {
		return f(ref(*this), forward<Args>(args)...);
	}
};
template <class F> decltype(auto) yc(F&& f) {
	return ycr<decay_t<F>>(forward<F>(f));
}

const int MAXN = 5010;
const int MAXV = MAXN * MAXN;
int par[MAXV];

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);

	const bool debug = false;
	int N, K;
	if (!debug) {
		cin >> N >> K;
	} else {
		N = K = 5000;
	}
	vector<string> grid(N);
	for (int x = 0; x < N; x++) {
		if (!debug) {
			cin >> grid[x];
		} else {
			for (int y = 0; y < N; y++) {
				grid[x] += '.';
			}
		}
	}

	auto getpar = yc([&](auto self, int i) -> int {
		if (par[i] == i) return i;
		return par[i] = self(par[i]);
	});
	auto merge = [&](int x, int y) -> void {
		int i = x * N + y;
		if (par[i] != i) return;
		int j = (x+1) * N + (y+1);
		j = getpar(j);
		par[i] = j;
	};

	for (int v = 0; v < N*N; v++) par[v] = v;

	vector<array<int, 2>> bfs; bfs.reserve(N*N);
	vector<vector<int>> dist(N, vector<int>(N, -1));
	auto update = [&](int x, int y, int d) -> void {
		if (x < 0 || x >= N) return;
		if (y < 0 || y >= N) return;
		if (x+1 < N && y+1 < N) {
			merge(x, y);
		}
		if (grid[x][y] == '*') return;
		if (dist[x][y] != -1) return;
		dist[x][y] = d;
		bfs.push_back({x, y});
	};
	update(0, 0, 0);

	auto update_diag = [&](int x_start, int y_start, int s, int d) -> void {
		int x = x_start;
		int y = y_start;
		if (x >= N || y >= N) return;
		while (true) {
			int j = getpar(x*N+y);
			x = j / N, y = j % N;
			if (x-x_start >= s) break;
			update(x, y, d);
			if (x+1 >= N || y+1 >= N) break;
			//merge(x, y);
			x++, y++;
		}
	};

	const vector<int> dx = {0, 1, 0, -1};
	const vector<int> dy = {1, 0, -1, 0};
	for (int z = 0; z < int(bfs.size()); z++) {
		auto [x, y] = bfs[z];
		int nd = dist[x][y]+1;
		if (x == N-1 && y == N-1) {
			cout << dist[x][y] << '\n';
			exit(0);
		}
		for (int dir = 0; dir < 4; dir++) {
			int nx = x + dx[dir];
			int ny = y + dy[dir];
			update(nx, ny, nd);
		}
		update_diag(x+1, y+1, K/2, nd);
		update_diag(y+1, x, (K+1)/2, nd);
	}
	//cout << dist[N-1][N-1] << '\n';
	cout << -1 << '\n';
	//cerr << double(clock()) / CLOCKS_PER_SEC << endl;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3672kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 27ms
memory: 16744kb

input:

961 4
...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 6ms
memory: 12764kb

input:

975 434
.*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 6ms
memory: 14684kb

input:

966 19
..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 5ms
memory: 13480kb

input:

973 199
..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 5ms
memory: 13284kb

input:

984 95
.....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 420ms
memory: 127708kb

input:

2996 2
..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 12ms
memory: 79800kb

input:

2905 1023
.........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 34ms
memory: 87536kb

input:

2978 104
.*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...

output:

58

result:

ok 1 number(s): "58"