QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#218634 | #6739. Teleport | Geospiza# | TL | 483ms | 94732kb | C++20 | 4.3kb | 2023-10-18 16:05:03 | 2023-10-18 16:05:03 |
Judging History
answer
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define ll int
#define Ma 20210926
#define N 5005
#define mod 998244353
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb push_back
using namespace std;
ll dp[N][N],vis[N][N];
string s[N];
ll n,k;
ll id[N][N];
ll len[N][N];
ll tot=0;
set <ll> st[N];
vector <PLL> v[N];
ll px[4]={1,-1,0,0},py[4]={0,0,1,-1};
struct Scanner {
FILE* fp = nullptr;
char line[(1 << 15) + 1];
size_t st = 0, ed = 0;
void reread() {
memmove(line, line + st, ed - st);
ed -= st;
st = 0;
ed += fread(line + ed, 1, (1 << 15) - ed, fp);
line[ed] = '\0';
}
bool succ() {
while (true) {
if (st == ed) {
reread();
if (st == ed) return false;
}
while (st != ed && isspace(line[st])) st++;
if (st != ed) break;
}
if (ed - st <= 50) reread();
return true;
}
template <class T, std::enable_if_t<std::is_same<T, std::string>::value, int> = 0>
bool read_single(T& ref) {
if (!succ()) return false;
while (true) {
size_t sz = 0;
while (st + sz < ed && !isspace(line[st + sz])) sz++;
ref.append(line + st, sz);
st += sz;
if (!sz || st != ed) break;
reread();
}
return true;
}
template <class T, std::enable_if_t<std::is_integral<T>::value, int> = 0>
bool read_single(T& ref) {
if (!succ()) return false;
bool neg = false;
if (line[st] == '-') {
neg = true;
st++;
}
ref = T(0);
while (isdigit(line[st])) {
ref = 10 * ref + (line[st++] - '0');
}
if (neg) ref = -ref;
return true;
}
template <class T> bool read_single(std::vector<T>& ref) {
for (auto& d : ref) {
if (!read_single(d)) return false;
}
return true;
}
void read() {}
template <class H, class... T> void read(H& h, T&... t) {
bool f = read_single(h);
assert(f);
read(t...);
}
Scanner(FILE* _fp) : fp(_fp) {}
};
Scanner sc(stdin);
void sol()
{
sc.read(n);
sc.read(k);
//cin>>n>>k;
for (ll l=0;l<=n*2-2;l++)
{
for (ll i=0;i<n;i++)
{
ll j=l-i;
if (j<0||j>=n)
continue;
if (id[i][j]==0)
{
id[i][j]=++tot;
len[i][j]=0;
st[tot].insert(len[i][j]);
v[tot].pb({i,j});
}
if (j+1<n)
{
id[j+1][i]=id[i][j];
len[j+1][i]=len[i][j]+1;
st[id[j+1][i]].insert(len[j+1][i]);
v[id[j+1][i]].pb({j+1,i});
}
}
}
for (ll i=0;i<n;i++)sc.read(s[i]);
//cin>>s[i];
for (ll i=0;i<n;i++)
for (ll j=0;j<n;j++)
if (s[i][j]=='*')
st[id[i][j]].erase(len[i][j]);
queue <PLL> q;
q.push({0,0});
st[id[0][0]].erase(0);
vis[0][0]=1;
while (!q.empty())
{
PLL p=q.front();
//printf("x=%d y=%d dp=%d\n",p.x,p.y,dp[p.x][p.y]);
q.pop();
for (ll i=0;i<4;i++){
ll nx=p.x+px[i],ny=p.y+py[i];
if (nx<0||nx>=n||ny<0||ny>=n||vis[nx][ny]||s[nx][ny]=='*')
continue;
vis[nx][ny]=1,dp[nx][ny]=dp[p.x][p.y]+1;
q.push({nx,ny});
st[id[nx][ny]].erase(len[nx][ny]);
}
vector <ll> er;
auto l=st[id[p.x][p.y]].lower_bound(len[p.x][p.y]);
//printf("l=%d\n",(*l));
while (l!=st[id[p.x][p.y]].end()&&(*l)<=len[p.x][p.y]+k)
er.pb((*l)),l++;
for (auto z:er)
{
PLL ok=v[id[p.x][p.y]][z];
dp[ok.x][ok.y]=dp[p.x][p.y]+1;
vis[ok.x][ok.y]=1;
st[id[p.x][p.y]].erase(z);
q.push(ok);
}
}
if (!vis[n-1][n-1])
{
cout<<-1;
return;
}
cout<<dp[n-1][n-1];
}
int main()
{
//ios::sync_with_stdio(0); cin.tie(0);
ll tt=1;
//cin>>tt;
while (tt--)
sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5984kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5972kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 452ms
memory: 89236kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 467ms
memory: 91332kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 483ms
memory: 89860kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 470ms
memory: 92088kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 477ms
memory: 94732kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: -100
Time Limit Exceeded
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...