QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#335239#6739. Teleportucup-team1198#AC ✓579ms313320kbC++173.3kb2024-02-23 00:07:312024-02-23 00:07:31

Judging History

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

  • [2024-02-23 00:07:31]
  • 评测
  • 测评结果:AC
  • 用时:579ms
  • 内存:313320kb
  • [2024-02-23 00:07:31]
  • 提交

answer

#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;

const int MAXN = 5e3 + 100;
string s[MAXN];
int d[MAXN][MAXN][5];

vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	auto start = clock();
	
	int n, k;
	cin >> n >> k;
	for (int i = 0; i < n; ++i) {
		cin >> s[i];
		/**for (int j = 0; j < n; ++j) {
			if (rand() % 5 == 0) {
				s[i].push_back('*');
			} else {
				s[i].push_back('.');
			}
		}*/
	}

	auto check = [&](int i, int j) {
		if (i < 0 || i >= n || j < 0 || j >= n || s[i][j] == '*') return false;
		return true;
	};

	deque<array<int, 3>> q;
	q.push_back({0, 0, 0});

	auto get_i = [&](int a) {
		if (a == 0) return 2;
		int pos = abs(a) - (k + 1) / 2;
		if (a > 0) return pos + 3;
		return pos;
	};

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			for (int t = 0; t < 5; ++t) {
				d[i][j][t] = n * n;
			}
		}
	}
	d[0][0][2] = 0;

	int num = 0;

	while (!q.empty()) {
		if ((++num) % 1000 == 0) {
			if ((clock() - start) * 1.0 / CLOCKS_PER_SEC > 0.95) {
				cout << "-1\n";
				return 0;
			}
		} 
		int i = q.front()[0], j = q.front()[1], l = q.front()[2];
		if (i == n - 1 && j == n - 1 && l == 0) {
			cout << d[i][j][2] << "\n";
			return 0;
		}
		/// cerr << i << " " << j << " " << l << endl;
		q.pop_front();
		if (l == 0) {
			for (int t = 0; t < 4; ++t) {
				int i1 = i + dx[t], j1 = j + dy[t];
				/// cerr << i1 << " " << " " << j1 << endl;
				if (check(i1, j1) && d[i1][j1][2] > d[i][j][2] + 1) {
					d[i1][j1][2] = d[i][j][2] + 1;
					q.push_back({i1, j1, 0});
				}
			}

			auto add = [&](int i, int j, int l, int w) {
				if (d[i][j][get_i(l)] > w) {
					d[i][j][get_i(l)] = w;
					q.push_front({i, j, l});
				}
				int i1 = i + l - 1;
				int j1 = j + l - 1;
				if (i1 >= n) {
					j1 -= (i1 - n + 1);
					i1 -= (i1 - n + 1);
				}
				if (j1 >= n) {
					i1 -= (j1 - n + 1);
					j1 -= (j1 - n + 1);
				}
				if ((i + j + 2 * l - 2) / (2 * l) == (i1 + j1 + 2 * l - 2) / (2 * l)) return;
				if (d[i1][j1][get_i(-l)] > w) {
					d[i1][j1][get_i(-l)] = w;
					q.push_front({i1, j1, -l});
				}
			};

			int l = (k + 2) / 2;
			int w = d[i][j][2];
			add(i, j, l, w);
			if (j + 1 < n) {
				l = (k + 1) / 2;
				add(j + 1, i, l, w);
			}
		} else {
			if (check(i, j) && d[i][j][2] > d[i][j][get_i(l)] + 1) {
				d[i][j][2] = d[i][j][get_i(l)] + 1;
				q.push_back({i, j, 0});
			}
			if (((i + j) / 2) % abs(l) == 0) continue;
			int i1, j1;
			if (l > 0) {
				i1 = i + 1;
				j1 = j + 1;
			} else {
				i1 = i - 1;
				j1 = j - 1;
			}
			if (i1 >= 0 && i1 < n && j1 >= 0 && j1 < n) {
				if (d[i1][j1][get_i(l)] > d[i][j][get_i(l)]) {
					d[i1][j1][get_i(l)] = d[i][j][get_i(l)];
					q.push_front({i1, j1, l});
				}
			}
		}
	}

	if (d[n - 1][n - 1][2] == n * n) {
		cout << "-1\n";
	} else {
		cout << d[n - 1][n - 1][2] << "\n";
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 7964kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3972kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 47ms
memory: 101204kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 3ms
memory: 101536kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 11ms
memory: 101296kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 0ms
memory: 103472kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 3ms
memory: 103320kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 579ms
memory: 313320kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 20ms
memory: 303656kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

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

input:

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

output:

58

result:

ok 1 number(s): "58"