QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#483244#6739. Teleportucup-team052#AC ✓612ms155432kbC++141.7kb2024-07-18 14:20:322024-07-18 14:20:32

Judging History

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

  • [2024-07-18 14:20:32]
  • 评测
  • 测评结果:AC
  • 用时:612ms
  • 内存:155432kb
  • [2024-07-18 14:20:32]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define D(...) fprintf(stderr,__VA_ARGS__)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
using namespace std;
const int N=5005,dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
int n,K;
char s[N][N];
int dis[N][N];
int fa[N*N];
int zip(int u,int v){
	if(u>=1&&u<=n&&v>=1&&v<=n)return (u-1)*n+v;
	else return 0;
}
int fd(int x){return fa[x]==x?x:fa[x]=fd(fa[x]);}
int main() {
#ifdef xay5421
	freopen("a.in","r",stdin);
#endif
	cin>>n>>K;
	rep(i,1,n)scanf("%s",s[i]+1);
	rep(i,1,n*n)fa[i]=i;
	rep(i,1,n)rep(j,1,n)if(s[i][j]=='*')fa[zip(i,j)]=zip(i+1,j+1);
	queue<pair<int,int> >q;
	q.emplace(1,1);
	memset(dis,63,sizeof(dis));
	dis[1][1]=0;
	fa[zip(1,1)]=zip(2,2);
	while(!q.empty()){
		int x,y;
		tie(x,y)=q.front();
		q.pop();
		// D("%d %d\n",x,y);
		auto up=[&](int nx,int ny){
			if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&s[nx][ny]=='.'){
				if(dis[x][y]+1<dis[nx][ny]){
					// D("! %d %d\n",nx,ny);
					dis[nx][ny]=dis[x][y]+1;
					assert(fd(zip(nx,ny))==zip(nx,ny));
					fa[zip(nx,ny)]=fd(zip(nx+1,ny+1));
					q.emplace(nx,ny);
				}
			}
		};
		rep(k,0,3){
			up(x+dx[k],y+dy[k]);
		}
		{
			int nx=x,ny=y;
			while(1){
				int cur=fd(zip(x,y));
				if(!cur)break;
				nx=(cur-1)/n+1;
				ny=(cur-1)%n+1;
				if((nx-x)*2<=K){
					up(nx,ny);
				}else{
					break;
				}
			}
			while(1){
				int cur=fd(zip(y+1,x));
				if(!cur)break;
				nx=(cur-1)/n+1;
				ny=(cur-1)%n+1;
				if((nx-(y+1))*2+1<=K){
					up(nx,ny);
				}else{
					break;
				}
			}
		}
	}
	printf("%d\n",dis[n][n]<1e9?dis[n][n]:-1);
	return 0;
}

详细

Test #1:

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

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 7ms
memory: 102400kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

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

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 55ms
memory: 113752kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 63ms
memory: 114032kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

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

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 57ms
memory: 113228kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 416ms
memory: 155192kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 453ms
memory: 153372kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 612ms
memory: 155432kb

input:

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

output:

58

result:

ok 1 number(s): "58"