QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#230765 | #6739. Teleport | dingdingtang11514 | AC ✓ | 868ms | 607956kb | C++14 | 1.9kb | 2023-10-28 20:47:14 | 2023-10-28 20:47:14 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;
namespace Mine{
inline int read(){
int x=1,a=0;
char ch=getchar();
while(ch>'9' || ch<'0')x=(ch=='-')?-1:x,ch=getchar();
while(ch>='0' && ch<='9')
a=(a<<1)+(a<<3)+(ch-'0'),ch=getchar();
return a*x;
}
const int N=5005;
int n,k;
char ch[N][N];
int fa[N*N],dep[N*N];
inline int get(int x,int y){
return (x-1)*N+y;
}
void init(){
for(int i=0;i<N*N;i++)
fa[i]=i,dep[i]=1;
return ;
}
int find(int x){
return (fa[x]==x) ? x: fa[x]=find(fa[x]);
}
void merge(int x,int y){
x=find(x),y=find(y);
fa[x]=y;
}
int dist[N*N];
queue<int> q;
int dx[]={1,0,0,-1},dy[]={0,1,-1,0};
void gmin(int &x,int y){if(y<x)x=y; return ;}
bool book[N*N];
int bfs(){
q.push(get(1,1));
memset(dist,-1,sizeof dist);
dist[get(1,1)]=0;
while(q.size()){
int tmp=q.front(),u;
u=tmp;
q.pop();
int nx,ny;
int y=u%N,x=u/N+1;
// cout<<x<<" "<<y<<endl;
// if(ch[x][y]=='*') continue;
for(int i=0;i<4;i++){
nx=x+dx[i],ny=y+dy[i];
// cout<<nx<<" "<<ny<<endl;
if(nx<1 || nx>n || ny<1 || ny>n) continue;
if(ch[nx][ny]=='*') continue;
if(dist[get(nx,ny)]!=-1) continue;
dist[get(nx,ny)]=dist[u]+1;
q.push(get(nx,ny));
}
u=find(u);
ny=u%N,nx=u/N+1;
while((ny+nx)-(x+y)<k){
merge(get(nx,ny),get(ny+1,nx));
swap(nx,ny);
nx++;
if(nx>n || ny>n) break;
if(ch[nx][ny]=='*' || dist[get(nx,ny)]!=-1)
continue;
dist[get(nx,ny)]=dist[get(x,y)]+1;
q.push(get(nx,ny));
}
}
return dist[get(n,n)];
}
signed main(){
init();
n=read(),k=read();
for(int i=1;i<=n;i++){
scanf("%s",ch[i]+1);
}
printf("%lld",bfs());
return 0;
}
}
signed main(){
// freopen("play.in","r",stdin);
// freopen("play.out","w",stdout);
return Mine::main();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 32ms
memory: 591456kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 28ms
memory: 593448kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 77ms
memory: 597672kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 117ms
memory: 597584kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 80ms
memory: 597556kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 103ms
memory: 597552kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 102ms
memory: 597568kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 364ms
memory: 605844kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 868ms
memory: 607956kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 487ms
memory: 607764kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"