QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#129286 | #6739. Teleport | SolitaryDream# | AC ✓ | 406ms | 242788kb | C++20 | 1.5kb | 2023-07-22 13:50:28 | 2023-07-22 13:50:29 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using pii=pair<int,int>;
const int N=5e3+1e2+7;
int n,k;
char s[N][N];
queue<pii>q;
int fa[N*2][N];
int d[N][N];
int find(int d,int x)
{
return fa[d][x]==x?x:fa[d][x]=find(d,fa[d][x]);
}
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(int i=1;i<=n+1;i++)
{
for(int j=1;j<=n+1;j++)
fa[i-j+n][i]=i;
}
memset(d,-1,sizeof(d));
q.push({1,1});
d[1][1]=0;
fa[0+n][1]=2;
while(!q.empty())
{
auto [x,y]=q.front();
q.pop();
if(s[x][y]=='*')
continue;
for(int t=0;t<4;t++)
{
int tx=x+dx[t],ty=y+dy[t];
if(tx<1||tx>n||ty<1||ty>n||d[tx][ty]!=-1)
continue;
d[tx][ty]=d[x][y]+1;
fa[tx-ty+n][tx]=tx+1;
q.push({tx,ty});
}
int c=k/2;
for(int i=find(x-y+n,x);i<=x+c;i=find(x-y+n,i))
{
if(i>n||y+i-x>n)
break;
d[i][y+i-x]=d[x][y]+1;
q.push({i,y+i-x});
fa[x-y+n][i]=i+1;
}
c=k-c-1;
for(int i=find(y+1-x+n,y+1);i<=n&&i<=y+1+c;i=find(y+1-x+n,i))
{
if(i>n||x-(y+1)+i>n)
break;
d[i][x-(y+1)+i]=d[x][y]+1;
q.push({i,x-(y+1)+i});
fa[(y+1)-x+n][i]=i+1;
}
}
printf("%d\n",d[n][n]);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 109716kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 7ms
memory: 108340kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 43ms
memory: 150836kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 33ms
memory: 151572kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 55ms
memory: 154156kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 43ms
memory: 154464kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 41ms
memory: 151312kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 339ms
memory: 242788kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 325ms
memory: 238708kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 406ms
memory: 241452kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"