QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#190969 | #6739. Teleport | lyzqs# | AC ✓ | 915ms | 558524kb | C++14 | 2.7kb | 2023-09-29 16:22:29 | 2023-09-29 16:22:30 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define int ll
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define vpii vector<pi>
#define il inline
#define ri register
#define all(a) a.begin(), a.end()
#define fr(a) freopen(a, "r", stdin)
#define fo(a) freopen(a, "w", stdout);
#define mod 998244353
#define debug puts("------------------------")
#define lowbit(x) (x&-x)
#define ls(x) x << 1
#define rs(x) x << 1 | 1
template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
ll ksm(ll a, ll b) {if (b == 0) return 1; ll ns = ksm(a, b >> 1); ns = ns * ns % mod; if (b & 1) ns = ns * a % mod; return ns;}
void Read(int &a) {a=0;int c=getchar(),b=1; while(c>'9'||c<'0') {if(c=='-')b=-1;c=getchar();} while(c>='0'&&c<='9') a=(a<<3)+(a<<1)+c-48,c=getchar();a*=b; }
int read() {int a=0,c=getchar(),b=1; while(c>'9'||c<'0') {if(c=='-')b=-1;c=getchar();} while(c>='0'&&c<='9') a=(a<<3)+(a<<1)+c-48,c=getchar();return a*=b; }
void write(int x) {if(x>9)write(x/10);putchar('0'+x%10);}
void W(int x) {if(x<0){putchar('-'),x=-x;}write(x);}
#define LOCAL
using namespace std;
const int maxn = 5005;
/**/
const int fx[] = {0, 0, 1, -1};
const int fy[] = {1, -1, 0, 0};
int n, m, k, fa[maxn*maxn], dis[maxn][maxn], tot;
char s[maxn][maxn];
pi rec[maxn*maxn];
// queue<pi>q;
pi q[maxn*maxn];
int l = 1, r = 0;
/**/
int id[maxn][maxn];
il int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
il void calc(int x, int y, int lim, int d)
{
while(x <= lim && x <= n && y <= n && x >= 1 && y >= 1)
{
pi u = rec[find(id[x][y])];
x = u.fi, y = u.se;
if(x > lim || x > n || y > n || x < 1 || y < 1) break;
if(s[x][y] == '.' && dis[x][y] == -1) dis[x][y] = d, q[++r] = {x, y};
fa[id[x][y]] = id[x+1][y+1];
++x; ++y;
}
}
signed main()
{
n = read(); k = read();
for(int i = 1; i <= n; i++)
{
scanf("%s", s[i] + 1);
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
id[i][j] = ++tot;
fa[id[i][j]] = id[i][j];
rec[id[i][j]] = {i,j};
dis[i][j] = -1;
}
}
dis[1][1] = 0;
// q.push({1, 1});
q[++r] = {1, 1};
while(l <= r)
{
// pi u = q.front(); q.pop();
int x = q[l].fi, y = q[l].se; ++l;
int d = dis[x][y];
for(int i = 0; i < 4; i++)
{
int dx = x + fx[i], dy = y + fy[i];
if(dx > n || dx < 1 || dy > n || dy < 1) continue;
if(s[dx][dy] == '*' || dis[dx][dy] != -1) continue;
dis[dx][dy] = d + 1;
// q.push({dx, dy});
q[++r] = {dx, dy};
}
calc(x + 1, y + 1, x + k / 2, d + 1);
calc(y + 1, x, y + 1 + (k - 1) / 2, d + 1);
}
cout << dis[n][n] << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9920kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 9972kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 56ms
memory: 123708kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 62ms
memory: 125264kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 73ms
memory: 121796kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 63ms
memory: 123936kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 67ms
memory: 127772kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 471ms
memory: 558524kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 836ms
memory: 532452kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 915ms
memory: 550488kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"