QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#230611 | #6739. Teleport | wxqwq | AC ✓ | 595ms | 281752kb | C++14 | 1.3kb | 2023-10-28 19:49:07 | 2023-10-28 19:49:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
const int N=5010;
int n,k;
PII fa[N][N];
PII q[N*N];
int dist[N][N];
char str[N][N];
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
PII find(int x,int y)
{
if(fa[x][y]!=(PII){x,y}) fa[x][y]=find(fa[x][y].x,fa[x][y].y);
return fa[x][y];
}
void merge(PII a,PII b)
{
PII x=find(a.x,a.y),y=find(b.x,b.y);
if(x!=y) fa[x.x][x.y]=y;
}
void bfs()
{
int tt=-1,hh=0;
memset(dist,-1,sizeof(dist));
dist[1][1]=0;
q[++tt]={1,1};
while(hh<=tt){
int x=q[hh].x,y=q[hh].y;++hh;
for(int i=0;i<4;i++){
int a=x+dx[i],b=y+dy[i];
if(a<1 || a>n || b<1 || b>n || dist[a][b]!=-1 || str[a][b]=='*') continue;
dist[a][b]=dist[x][y]+1;
q[++tt]={a,b};
}
PII t=find(x,y);
for(int a=t.x,b=t.y,i=t.x+t.y-x-y+1;i<=k;i++){
swap(a,b);++a;
if(a>n || b>n) break;
merge(t,{a,b});
if(find(a,b)!=(PII){a,b}) break;
if(dist[a][b]!=-1) continue;
if(str[a][b]=='*') continue;
dist[a][b]=dist[x][y]+1;
q[++tt]={a,b};
}
}
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%s",str[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
fa[i][j]={i,j};
bfs();
printf("%d\n",dist[n][n]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 106240kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 8ms
memory: 105996kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 43ms
memory: 154708kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 55ms
memory: 154508kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 58ms
memory: 155160kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 56ms
memory: 152240kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 67ms
memory: 154776kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 348ms
memory: 281752kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 508ms
memory: 274696kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 595ms
memory: 280776kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"