QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#184634 | #6739. Teleport | bulijiojiodibuliduo# | AC ✓ | 585ms | 96424kb | C++ | 2.0kb | 2023-09-21 00:30:36 | 2023-09-21 00:30:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=5010,M=N*N;
int n,k,f[M],dis[M];
char s[N][N];
vector<PII> dir{{-1,0},{1,0},{0,-1},{0,1}};
int find(int x) {
return f[x]==x?x:f[x]=find(f[x]);
}
void del(int fx,int fy) {
assert(fx<n&&fy<n);
f[find(fx*(n+1)+fy)]=find((fx+1)*(n+1)+(fy+1));
}
PII getx(int fx,int fy) {
int id=find(fx*(n+1)+fy);
return mp(id/(n+1),id%(n+1));
}
int main() {
scanf("%d%d",&n,&k);
rep(i,0,n) scanf("%s",s[i]);
rep(i,0,n+1) rep(j,0,n+1) f[i*(n+1)+j]=i*(n+1)+j;
rep(i,0,n*n) dis[i]=n*n+1;
rep(i,0,n) rep(j,0,n) if (s[i][j]=='*') {
del(i,j);
}
dis[0]=0;
queue<PII> q;
q.push(mp(0,0));
del(0,0);
while (!q.empty()) {
auto [x,y]=q.front();
q.pop();
for (auto [dx,dy]:dir) {
int nx=x+dx,ny=y+dy;
if (nx>=0&&nx<n&&ny>=0&&ny<n&&dis[nx*n+ny]>dis[x*n+y]+1&&
s[nx][ny]!='*') {
dis[nx*n+ny]=dis[x*n+y]+1;
q.push(mp(nx,ny));
del(nx,ny);
}
}
while (1) {
auto [fx,fy]=getx(x,y);
if (fx>=n||fy>=n||(fx-x)*2>k) break;
dis[fx*n+fy]=dis[x*n+y]+1;
q.push(mp(fx,fy));
del(fx,fy);
}
while (1) {
auto [fx,fy]=getx(y+1,x);
if (fx>=n||fy>=n||(fy-x)*2+1>k) break;
dis[fx*n+fy]=dis[x*n+y]+1;
q.push(mp(fx,fy));
del(fx,fy);
}
}
int ans=dis[n*n-1];
if (ans>=n*n) ans=-1;
printf("%d\n",ans);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 8004kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 7996kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 43ms
memory: 19916kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 49ms
memory: 23164kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 53ms
memory: 19988kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 50ms
memory: 19756kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 51ms
memory: 22808kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 433ms
memory: 96424kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 468ms
memory: 88868kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 585ms
memory: 93216kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"