QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#125878 | #6739. Teleport | Liberty12619 | AC ✓ | 386ms | 209912kb | C++20 | 1.9kb | 2023-07-17 20:35:32 | 2023-07-17 20:35:32 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
using namespace std;
const int N = 5e3+10,M = 1<<22,mod = 998244353,INF=1e15;
typedef pair<int,int>PII;
typedef pair<PII,int>PIII;
typedef long long LL;
int dist[N][N],ne[N][N];
char s[N][N];
void solve()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
dist[i][j]=-1;
dist[1][1]=0;
for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
queue<PII>q;
if(s[1][1]=='.') q.push({1,1});
int dx[4]={0,-1,0,1},dy[4]={1,0,-1,0};
auto ph = [&](int tx,int ty,int x,int y)
{
dist[tx][ty]=dist[x][y]+1;
if(tx==n&&ty==n)
{
cout<<dist[tx][ty]<<endl;
exit(0);
}
q.push({tx,ty});
};
while(q.size())
{
auto it = q.front();
q.pop();
int x = it.x,y=it.y;
int tmp = min({k/2,n-x,n-y});
for(int i=ne[x][y]+1;i<=tmp;i++)
{
int tx = i+x,ty=i+y;
ne[tx][ty]=max(ne[tx][ty],tmp-i);
if(dist[tx][ty]==-1&&s[tx][ty]=='.') ph(tx,ty,x,y);
}
ne[x][y]=max(ne[x][y],tmp);
int rx = y+1,ry=x;
if(dist[rx][ry]==-1&&s[rx][ry]=='.') ph(rx,ry,x,y);
tmp = min({(k-1)/2,n-rx,n-ry});
for(int i=ne[rx][ry]+1;i<=tmp;i++)
{
int tx = i+rx,ty=i+ry;
ne[tx][ty]=max(ne[tx][ty],tmp-i);
if(dist[tx][ty]==-1&&s[tx][ty]=='.') ph(tx,ty,x,y);
}
ne[rx][ry]=max(ne[rx][ry],tmp);
for(int i=0;i<4;i++)
{
int tx = x+dx[i],ty=y+dy[i];
if(dist[tx][ty]==-1&&s[tx][ty]=='.')ph(tx,ty,x,y);
}
}
cout<<-1<<endl;
}
signed main()
{
int T =1;
//cin>>T;
while(T--)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5560kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5560kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 27ms
memory: 58696kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 1ms
memory: 55036kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 9ms
memory: 53900kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 7ms
memory: 54840kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 9ms
memory: 51968kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 386ms
memory: 209912kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 51ms
memory: 137080kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 42ms
memory: 141308kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"