QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#165025#6739. Teleportxaphoenix#ML 8ms111976kbC++143.1kb2023-09-05 15:32:382023-09-05 15:32:39

Judging History

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

  • [2023-09-05 15:32:39]
  • 评测
  • 测评结果:ML
  • 用时:8ms
  • 内存:111976kb
  • [2023-09-05 15:32:38]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define LC k<<1
#define RC k<<1|1
#define IO cin.sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repn(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = (n) - 1; i >= a; i--)
#define pern(i, a, n) for (int i = n; i >= a; i--)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<double, double> PDD;
typedef pair<ull, ull> PUU;
typedef pair<LL, LL> PLL;

const int N = 5100;
const int M = N * N;
const int mod = 1e9+7;
const int inf = (int)1e9;
const LL INF = 1e18;
const double eps = 1e-9;

mt19937_64 Rand((unsigned long long)new char);
#define rand Rand

int n, k, k1, k2, a[N][N], f1[M], f2[M], dis[N][N];
int find(int x, int f[]) {
	return f[x] == x ? x: f[x] = find(f[x], f);
}
int cal(int x, int y) {
	return (x - 1) * n + y;
}
PII cal2(int id) {
	if (id % n == 0) return mp(id / n, n);
	return mp(id / n + 1, id % n);
}
PII que[M];
int head, tail;
const int wayx[4] = {-1, 1, 0, 0};
const int wayy[4] = {0, 0, -1, 1};
int check(int x, int y) {
	if (x < 1 || x > n || y < 1 || y > n) return 0;
	if (dis[x][y] != -1 || a[x][y] == 0) return 0;
	return 1;
}
int main() {
	IO;
	cin >> n >> k;
	k1 = k / 2, k2 = (k + 1) / 2;
	repn(i, 1, n) {
		string s;
		cin >> s;
		repn(j, 1, n) {
			if (s[j - 1] == '.') a[i][j] = 1;
			else a[i][j] = 0;
			int idx = cal(i, j);
			f1[idx] = f2[idx] = idx;
		}
	}
	memset(dis, -1, sizeof(dis));
	que[head = tail = 1] = mp(1, 1);
	dis[1][1] = 0;
	while (head <= tail) {
		auto now = que[head++];
		int x = now.fi, y = now.se, w = dis[x][y];;
		rep(i, 0, 4) {
			int nx = x + wayx[i], ny = y + wayy[i];
			if (check(nx, ny)) {
				dis[nx][ny] = w + 1;
				que[++tail] = mp(nx, ny);
			}
		}
		// f1
		int delta = min(k1, min(n - x, n - y));
		int sx = x + 1, sy = y + 1, ex = x + delta, ey = y + delta;
		int id = cal(sx, sy), idx = cal(ex, ey);
		while (id <= idx) {
			id = find(id, f1);
			if (id > idx) break;
			auto p = cal2(id);
			int nx = p.fi, ny = p.se;
			if (check(nx, ny)) {
				dis[nx][ny] = w + 1;
				que[++tail] = mp(nx, ny);
			}
			if (nx + 1 > n || ny + 1 > n) f1[id] = cal(n + 1, n + 1), id = f1[id];
			else f1[id] = cal(nx + 1, ny + 1), id = f1[id];
		}
		// f2
		delta = min(k2, min(n - y, n - x + 1));
		sx = y + 1, sy = x, ex = y + delta, ey = x + delta - 1;
		id = cal(sx, sy), idx = cal(ex, ey);
		while (id <= idx) {
			id = find(id, f2);
			if (id > idx) break;
			auto p = cal2(id);
			int nx = p.fi, ny = p.se;
			if (check(nx, ny)) {
				dis[nx][ny] = w + 1;
				que[++tail] = mp(nx, ny);
			}
			if (nx + 1 > n || ny + 1 > n) f2[id] = cal(n + 1, n + 1), id = f2[id];
			else f2[id] = cal(nx + 1, ny + 1), id = f2[id];
		}
	}
	cout << dis[n][n] << "\n";
	return 0;
}

詳細信息

Test #1:

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

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 8ms
memory: 111976kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Memory Limit Exceeded

input:

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

output:


result: