QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#218647 | #6739. Teleport | Geospiza# | AC ✓ | 559ms | 331868kb | C++20 | 2.0kb | 2023-10-18 16:15:54 | 2023-10-18 16:15:55 |
Judging History
answer
#include <bits/stdc++.h>
#define ll int
#define Ma 20210926
#define N 5005
#define mod 998244353
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb push_back
using namespace std;
ll dp[N][N],vis[N][N];
string s[N];
ll n,k;
ll id[N][N];
ll len[N][N];
ll tot=0;
ll lim[N];
vector <PLL> v[N];
ll px[4]={1,-1,0,0},py[4]={0,0,1,-1};
void sol()
{
cin>>n>>k;
for (ll l=0;l<=n*2-2;l++)
{
for (ll i=0;i<n;i++)
{
ll j=l-i;
if (j<0||j>=n)
continue;
if (id[i][j]==0)
{
id[i][j]=++tot;
len[i][j]=0;
v[tot].pb({i,j});
}
if (j+1<n)
{
id[j+1][i]=id[i][j];
len[j+1][i]=len[i][j]+1;
v[id[j+1][i]].pb({j+1,i});
}
}
}
for (ll i=0;i<n;i++)
cin>>s[i];
queue <PLL> q;
q.push({0,0});
vis[0][0]=1;
while (!q.empty())
{
PLL p=q.front();
q.pop();
if (p.x==n-1&&p.y==n-1)
{
printf("%d\n",dp[n-1][n-1]);
return;
}
for (ll i=0;i<4;i++){
ll nx=p.x+px[i],ny=p.y+py[i];
if (nx<0||nx>=n||ny<0||ny>=n||vis[nx][ny]||s[nx][ny]=='*')
continue;
vis[nx][ny]=1,dp[nx][ny]=dp[p.x][p.y]+1;
q.push({nx,ny});
}
while (lim[id[p.x][p.y]]<=len[p.x][p.y]+k&&lim[id[p.x][p.y]]<v[id[p.x][p.y]].size())
{
PLL ok=v[id[p.x][p.y]][lim[id[p.x][p.y]]];
lim[id[p.x][p.y]]++;
if (s[ok.x][ok.y]=='*'||vis[ok.x][ok.y])
continue;
dp[ok.x][ok.y]=dp[p.x][p.y]+1;
vis[ok.x][ok.y]=1;
q.push(ok);
}
}
printf("-1\n");
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
ll tt=1;
//cin>>tt;
while (tt--)
sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5924kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 8252kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 28ms
memory: 80444kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 11ms
memory: 80296kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 7ms
memory: 78808kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 13ms
memory: 76948kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 12ms
memory: 81080kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 559ms
memory: 331868kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 105ms
memory: 285864kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 114ms
memory: 297080kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"