QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#204523#6739. TeleportkiwiHM#AC ✓510ms156708kbC++142.0kb2023-10-07 12:48:022023-10-07 12:48:02

Judging History

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

  • [2023-10-07 12:48:02]
  • 评测
  • 测评结果:AC
  • 用时:510ms
  • 内存:156708kb
  • [2023-10-07 12:48:02]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int maxn = 5005;
const int maxv = maxn * maxn;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

int n, m, k;
int dis[maxv], fa[maxv];
char s[maxn][maxn];

inline bool valid(int i, int j){ return (i >= 0 && i < n && j >= 0 && j < m && s[i][j] == '.'); }

inline void del(int i, int j){
	if(i + 1 < n && j + 1 < m) fa[i * m + j] = (i + 1) * m + (j + 1);
	else fa[i * m + j] = -1;
}

inline int find(int u){
	if(u == -1 || fa[u] == u) return u;
	return fa[u] = find(fa[u]);
}

queue<int> que;

inline void upd(int si, int sj, int ti, int tj, int w){
	// printf("si = %d sj = %d ti = %d tj = %d\n", si, sj, ti, tj);
	if(si >= n || sj >= m) return;
	int s = si * m + sj, t = ti * m + tj;
	for(int u = find(si * m + sj), i, j; u != -1 && u <= ti * m + tj; u = find(u)){
		dis[u] = w;
		// printf("upd u = %d\n", u);
		que.push(u);
		i = u / m, j = u % m;
		del(i, j);
		// printf("fa = %d\n", fa[u]);
	}
}

int main(){
	// freopen("G.in", "r", stdin);
	
	scanf("%d%d", &n, &k);
	m = n;
	// printf("n = %d k = %d\n", n, k);
	for(int i = 0; i < n; ++i){
		scanf("%s", s[i]);
		for(int j = 0; j < m; ++j){
			if(s[i][j] == '.'){
				fa[i * m + j] = i * m + j;
			}
			else{
				del(i, j);
			}
		}
	}
	
	memset(dis, 0x3f, sizeof(dis));
	dis[0] = 0;
	del(0, 0);
	for(que.push(0); !que.empty(); que.pop()){
		int u = que.front();
		// printf("u = %d (%d %d) %d\n", u, u / m, u % m, dis[u]);
		int i = u / m, j = u % m;
		for(int k = 0; k < 4; ++k){
			int ni = i + dx[k], nj = j + dy[k];
			int v = ni * m + nj;
			if(valid(ni, nj) && dis[v] > dis[u] + 1){
				// printf("nei v = %d\n", v);
				dis[v] = dis[u] + 1;
				del(ni, nj);
				que.push(v);
			}
		}
		upd(i + 1, j + 1, i + k / 2, j + k / 2, dis[u] + 1);
		upd(j + 1, i, j + 1 + (k - 1) / 2, i + (k - 1) / 2, dis[u] + 1);
	}
	
	if(dis[(n - 1) * m + (m - 1)] == 0x3f3f3f3f) puts("-1");
	else printf("%d\n", dis[(n - 1) * m + (m - 1)]);
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 104028kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 4ms
memory: 104240kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 45ms
memory: 113712kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 42ms
memory: 113692kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 53ms
memory: 113396kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 54ms
memory: 113420kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 50ms
memory: 115452kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 353ms
memory: 156708kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 359ms
memory: 152332kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 510ms
memory: 154376kb

input:

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

output:

58

result:

ok 1 number(s): "58"