QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#132675 | #6739. Teleport | de_sousa# | TL | 191ms | 39616kb | C++17 | 2.6kb | 2023-07-31 01:10:39 | 2023-07-31 01:10:40 |
Judging History
answer
#pragma GCC optimize("-O3","unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = a; i < b; i++)
#define all(x) begin(x),end(x)
#define trav(a, b) for (auto a : b)
#define lld long long
using i64 = long long;
void solve() {
int n,m,k;
cin>>n>>k;
m=n;
vector<string> a(n);
for(auto&i:a){
cin>>i;
}
vector<array<int,2>> Q;
Q.push_back({0,0});
vector<vector<int>> distance(n,vector<int>(m,-1));
distance[0][0]=0;
vector<set<array<int,2>>> sets(n+m); // id of (i,j) is i-j+m-1
auto id=[&](int x,int y)
{
int val=x-y+m-1;
assert(val>=0);
return val;
};
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(a[i][j]!='*'){
sets[id(i,j)].insert({i,j});
}
}
}
sets[id(0,0)].erase({0,0});
int Qi=0;
while(Qi<Q.size()){
auto[i,j]=Q[Qi];
if(i==n-1&&j==m-1){
cout<<distance[i][j]<<'\n';
return;
}
if(i>0&&sets[id(i-1,j)].find({i-1,j})!=sets[id(i-1,j)].end()){
distance[i-1][j]=1+distance[i][j];
Q.push_back({i-1,j});
sets[id(i-1,j)].erase({i-1,j});
}
if(i<n-1&&sets[id(i+1,j)].find({i+1,j})!=sets[id(i+1,j)].end()){
distance[i+1][j]=1+distance[i][j];
Q.push_back({i+1,j});
sets[id(i+1,j)].erase({i+1,j});
}
if(j>0&&sets[id(i,j-1)].find({i,j-1})!=sets[id(i,j-1)].end()){
distance[i][j-1]=1+distance[i][j];
Q.push_back({i,j-1});
sets[id(i,j-1)].erase({i,j-1});
}
if(j<m-1&&sets[id(i,j+1)].find({i,j+1})!=sets[id(i,j+1)].end()){
distance[i][j+1]=1+distance[i][j];
Q.push_back({i,j+1});
sets[id(i,j+1)].erase({i,j+1});
}
{
vector<array<int,2>> toErase;
for(auto iter=sets[id(i,j)].lower_bound({i,j});iter!=sets[id(i,j)].upper_bound({i+k/2,j+k/2});iter++){
toErase.push_back(*iter);
auto[xi,yi]=*iter;
distance[xi][yi]=1+distance[i][j];
Q.push_back({xi,yi});
}
for(auto iter:toErase){
sets[id(i,j)].erase(iter);
}
}
if(j<m-1){
vector<array<int,2>> toErase;
for(auto iter=sets[id(j+1,i)].lower_bound({j+1,i});iter!=sets[id(j+1,i)].upper_bound({j+1+(k-1)/2,i+(k-1)/2});iter++){
toErase.push_back(*iter);
auto[xi,yi]=*iter;
distance[xi][yi]=1+distance[i][j];
Q.push_back({xi,yi});
}
for(auto iter:toErase){
sets[id(j+1,i)].erase(iter);
}
}
Qi++;
}
cout<<distance[n-1][m-1]<<'\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tt = 1;
// cin >> tt;
while (tt--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3464kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3496kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 191ms
memory: 39616kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 126ms
memory: 35692kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 137ms
memory: 35200kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 118ms
memory: 35472kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 132ms
memory: 36176kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: -100
Time Limit Exceeded
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...