QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#185430 | #6739. Teleport | ucup-team134# | AC ✓ | 448ms | 248476kb | C++14 | 1.8kb | 2023-09-22 03:08:17 | 2023-09-22 03:08:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N=5050;
char s[N][N];
int dist[N][N];
//set<pair<int,int>> can[2*N];
int mv[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
const int M=N*N;
int p[M];
int Find(int x){return x==p[x]?x:p[x]=Find(p[x]);}
void Union(int x,int y){p[Find(x)]=Find(y);}
int idx[N][N];
pair<int,int> cell[M];
int main(){
int n,k;
scanf("%i %i",&n,&k);
for(int i=1;i<=n;i++){
scanf("%s",s[i]+1);
}
int c=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=-1;
idx[i][j]=++c;
cell[c]={i,j};
p[c]=c;
}
}
dist[1][1]=0;
queue<pair<int,int>> q;
q.push({1,1});
while(q.size()){
int x=q.front().first;
int y=q.front().second;
if(x==n && y==n)break;
q.pop();
for(int i=0;i<4;i++){
int nx=x+mv[i][0];
int ny=y+mv[i][1];
if(s[nx][ny]!='.')continue;
if(dist[nx][ny]==-1){
dist[nx][ny]=dist[x][y]+1;
q.push({nx,ny});
if(idx[nx+1][ny+1]){
Union(idx[nx][ny],idx[nx+1][ny+1]);
}
//can[nx-ny+N].erase({nx,ny});
}
}
int now=idx[x][y];
while(true){
now=Find(now);
int nx=cell[now].first;
int ny=cell[now].second;
if(x+y+k<nx+ny)break;
if(dist[nx][ny]==-1 && s[nx][ny]=='.'){
dist[nx][ny]=dist[x][y]+1;
q.push({nx,ny});
}
if(idx[nx+1][ny+1]){
Union(idx[nx][ny],idx[nx+1][ny+1]);
}else{
break;
}
}
now=idx[y+1][x];
while(true){
if(now==0)break;
now=Find(now);
int nx=cell[now].first;
int ny=cell[now].second;
if(x+y+k<nx+ny)break;
if(dist[nx][ny]==-1 && s[nx][ny]=='.'){
dist[nx][ny]=dist[x][y]+1;
q.push({nx,ny});
}
if(idx[nx+1][ny+1]){
Union(idx[nx][ny],idx[nx+1][ny+1]);
}else{
break;
}
}
}
printf("%i\n",dist[n][n]);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 11804kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 2ms
memory: 11920kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 36ms
memory: 70256kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 0ms
memory: 67724kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 8ms
memory: 65308kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 8ms
memory: 65100kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 8ms
memory: 67244kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 448ms
memory: 248476kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 35ms
memory: 240248kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 45ms
memory: 247820kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"