QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#143196 | #6739. Teleport | Kidding_Ma | AC ✓ | 295ms | 48500kb | C++20 | 6.7kb | 2023-08-20 21:12:40 | 2023-08-20 21:12:42 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
using i128 = __int128;
namespace cpstd {
#define range(a) begin(a), end(a)
template <class T>
T max(const vector<T> &a) {
return *std::max_element(a.begin(), a.end());
}
template <class T>
T min(const vector<T> &a) {
return *std::min_element(a.begin(), a.end());
}
template <class T>
struct Fenwick {
int n;
std::vector<T> a;
Fenwick(const int &n = 0) : n(n), a(n, T()) {}
void modify(int i, T x) {
for (i++; i <= n; i += i & -i) {
a[i - 1] += x;
}
}
T get(int i) {
T res = T();
for (; i > 0; i -= i & -i) {
res += a[i - 1];
}
return res;
}
T sum(int l, int r) { // [l, r)
return get(r) - get(l);
}
T kth(T k) {
int x = 0;
for (int i = 1 << std::__lg(n); i; i >>= 1) {
if (x + i <= n && k >= a[x + i - 1]) {
x += i;
k -= a[x - 1];
}
}
return x;
}
};
template <class T>
T exgcd(T a, T b, T &x, T &y) {
if (!b) {
x = 1, y = 0;
return a;
}
T g = exgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
template <class T>
T floor_sum(T n, T m, T a, T b) {
T ans = 0;
if (a < 0) {
T a2 = (a % m + m) % m;
ans -= n * (n - 1) / 2 * ((a2 - a) / m);
a = a2;
}
if (b < 0) {
T b2 = (b % m + m) % m;
ans -= n * ((b2 - b) / m);
b = b2;
}
while (1) {
if (a >= m) {
ans += n * (n - 1) / 2 * (a / m);
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}
T y_max = a * n + b;
if (y_max < m) {
break;
}
n = y_max / m;
b = y_max % m;
swap(m, a);
}
return ans;
}
template <class T, class U1, class U2>
T power(T a, U1 b, U2 p) {
T res = 1;
for (; b; b >>= 1, a = a * a % p) {
if (b & 1) {
res = res * a % p;
}
}
return res;
}
template <class T, class U>
T power(T a, U b) {
T res = 1;
for (; b; b >>= 1, a = a * a) {
if (b & 1) {
res = res * a;
}
}
return res;
}
__int128 abs(const __int128 &v) {
return (v < 0 ? -v : v);
}
std::mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
istream &operator>>(istream &is, __int128 &v) {
std::string s;
is >> s;
v = 0;
for (auto &si : s) {
v = v * 10 + si - '0';
}
return is;
}
ostream &operator<<(ostream &os, const __int128 &v) {
if (v <= 1000000000000000000) {
return os << (long long) (v);
}
return os << (long long) (v / 1000000000000000000) << std::setw(18) << std::setfill('0') << (long long) (v % 1000000000000000000);
}
template <class T>
T crt(const vector<T> &r, const vector<T> &m) {
int n = r.size();
T r0 = 0, m0 = 1;
for (int i = 0; i < n; i++) {
T m1 = m[i];
T r1 = r[i] % m1;
if (r1 < 0) {
r1 += m1;
}
if (m0 < m1) {
std::swap(r0, r1);
std::swap(m0, m1);
}
if (!(m0 % m1)) {
if (r0 % m1 != r1) {
return -1;
}
continue;
}
T x, y;
T g = exgcd(m0, m1, x, y);
if ((r1 - r0) % g) {
return -1;
}
T u1 = m1 / g;
r0 += (r1 - r0) / g % u1 * x % u1 * m0;
m0 *= u1;
if (r0 < 0) {
r0 += m0;
}
}
return r0;
}
constexpr int B = 777;
constexpr long long P = 100000000000031;
long long *p;
void initHash(int N) {
p = new long long [N + 1];
for (int i = 0; i <= N; i++) {
p[i] = 0;
}
p[0] = 1;
for (int i = 1; i <= N; i++) {
p[i] = p[i - 1] * B % P;
}
}
struct StringHash {
std::vector<long long> h;
StringHash() : h(1) {}
void push_back(char ch) {
h.push_back((h.back() * B + ch) % P);
}
long long get(int l, int r) { // [l, r)
return (h[r] + __int128(h[l]) * (P - p[r - l])) % P;
}
};
struct UnionFind {
int n;
std::vector<int> f, sz;
UnionFind(const int &n = 0) : n(n), f(n), sz(n, 1) {
std::iota(f.begin(), f.end(), 0);
}
int get(int x) {
while (x != f[x]) {
x = f[x] = f[f[x]];
}
return x;
}
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x != y) {
f[y] = x;
sz[x] += sz[y];
return 1;
}
return 0;
}
bool united(int x, int y) {
return get(x) == get(y);
}
int size(int x) {
x = get(x);
return sz[x];
}
};
}
using namespace cpstd;
void solve() {
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<string> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int sx = 0, sy = 0, tx = n - 1, ty = n - 1;
queue<array<int, 3>> q;
q.push({0, sx, sy});
int dx[] = {+1, -1, +0, +0};
int dy[] = {+0, +0, +1, -1};
vector<vector<int>> vis(n, vector<int>(n));
vis[sx][sy] = 1;
while (!q.empty()) {
auto [res, x, y] = q.front();
if (x == tx && y == ty) {
cout << res << '\n';
return 0;
}
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n) {
continue;
}
if (vis[nx][ny]) {
continue;
}
if (a[nx][ny] == '*') {
continue;
}
vis[nx][ny] = 1;
q.push({res + 1, nx, ny});
}
int X = x, Y = y;
for (int i = 0; i <= k; i++) {
int nx = X;
int ny = Y;
if (nx < 0 || ny < 0 || nx >= n || ny >= n) {
continue;;
}
if (vis[nx][ny]) {
int tmp = X;
X = Y + 1;
Y = tmp;
continue;
}
if (a[nx][ny] == '*') {
int tmp = X;
X = Y + 1;
Y = tmp;
continue;
}
vis[nx][ny] = 1;
q.push({res + 1, nx, ny});
int tmp = X;
X = Y + 1;
Y = tmp;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3520kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3568kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 21ms
memory: 8240kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 8ms
memory: 8328kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 15ms
memory: 8244kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 9ms
memory: 8256kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 3ms
memory: 8404kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 295ms
memory: 48500kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 34ms
memory: 45328kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 64ms
memory: 47956kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"