QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#166863 | #6739. Teleport | PhantomThreshold# | AC ✓ | 995ms | 324608kb | C++20 | 1.6kb | 2023-09-06 19:35:58 | 2023-09-06 19:35:58 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
//#define int long long
using namespace std;
const int maxn = 5050;
int n,K;
int dis[maxn][maxn],a[maxn][maxn];
pair<int,int>loc[maxn*maxn];
int fa[maxn*maxn],id[maxn][maxn],idn;
int findfa(const int x) { return fa[x]==x?x:fa[x]=findfa(fa[x]); }
queue< pair<int,int> >q;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,1,0,-1};
int calc(const int ix,const int iy,const int x,const int y)
{
if( x-y==ix-iy ) return (x-ix)*2;
else return (x-(iy+1))*2+1;
}
void upd(const int ix,const int iy,const int uk,int D)
{
if(a[ix][iy]) return;
int i=id[ix][iy];
while(1)
{
i=findfa(i);
auto [x,y]= loc[i];
int kk=calc(ix,iy,x,y);
if(kk>uk) break;
if(dis[x][y]==-1)
{
dis[x][y]=D;
q.emplace( x,y );
}
int j=id[y+1][x];
if(!j) break;
fa[i]=j;
}
}
void bfs()
{
memset(dis,-1,sizeof dis);
upd(1,1,0,0);
while(!q.empty())
{
const auto [x,y]=q.front(); q.pop();
upd(x,y,K,dis[x][y]+1);
for(int dir=0;dir<4;dir++)
{
upd(x+dx[dir],y+dy[dir],0,dis[x][y]+1);
}
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>K;
for(int i=0;i<=n;i++) a[0][i]=a[n+1][i]=a[i][0]=a[i][n+1]=1;
for(int i=1;i<=n;i++)
{
string str; cin>>str; str=' '+str;
for(int j=1;j<=n;j++)
{
id[i][j]=++idn;
loc[idn]=make_pair(i,j);
fa[idn]=idn;
if(str[j]=='.') a[i][j]=0;
else a[i][j]=1;
}
}
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(a[i][j])
{
int u=id[i][j], v=id[j+1][i];
if(v) fa[u]=v;
}
bfs();
cout<<dis[n][n]<<endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 111024kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 111084kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 84ms
memory: 158452kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 68ms
memory: 158764kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 84ms
memory: 158964kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 80ms
memory: 160616kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 77ms
memory: 160592kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 779ms
memory: 323292kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 864ms
memory: 315180kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 995ms
memory: 324608kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"