QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#109597 | #4856. Puzzle: Patrick's Parabox | Dr_Gilbert | AC ✓ | 102ms | 64888kb | C++14 | 5.1kb | 2023-05-29 20:46:08 | 2023-05-29 20:46:09 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N=1e5+10,M=N<<3;
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
vector<vector<int>> a,G;
int dp[M][4],pre[M][4],suf[M][4],r,dfn[M];
int posx[8],posy[8],tot,cnt,tim,n,m,fat[M];
int st[M],ed[M],rt[M],stk[M],low[M],dis[M][4];
int getid(int x, int y){return (x-1)*m+y;}
pair<int,int> getpos(int id){
int x=(id-1)/m+1,y=(id-1)%m+1;
if (x<1||x>n) return {0,0};
if (y<1||y>m) return {0,0};
return {x,y};
}
bool invalid(int x, int y){
return (x<1||x>n||y<1||y>m||a[x][y]);
}
void add(int u, int v){
G[u].emplace_back(v);G[v].emplace_back(u);return;
}
void tarjan(int x, int fa){
int son=0;stk[++r]=x;dfn[x]=low[x]=++tot;
for (int k=0;k<4;k++){
auto [i,j]=getpos(x);
int v=getid(i+dx[k],j+dy[k]);
if (invalid(i+dx[k],j+dy[k])) continue;
if (!dfn[v]){
tarjan(v,x);son++;low[x]=min(low[x],low[v]);
if (low[v]>=dfn[x]){
cnt++;int k;add(x,cnt);
do{k=stk[r--];add(k,cnt);}while (k!=v);
}
}else if (v!=fa) low[x]=min(low[x],dfn[v]);
}if (!fa&&!son) add(x,++cnt);
return;
}
void dfs(int x, int fa, int col){
auto [i,j]=getpos(x);
st[x]=++tim;rt[tim]=col;fat[x]=fa;
pre[tim][0]=suf[tim][0]=dp[x][0]=(i==1);
pre[tim][1]=suf[tim][1]=dp[x][1]=(j==m);
pre[tim][2]=suf[tim][2]=dp[x][2]=(i==n);
pre[tim][3]=suf[tim][3]=dp[x][3]=(j==1);
for (int v:G[x]){
if (v==fa) continue;else dfs(v,x,col);
for (int i=0;i<4;i++) dp[x][i]|=dp[v][i];
}ed[x]=tim;return;
}
void build(){
tim=r=0;cnt=n*m;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
if (invalid(i,j)) continue;
if (dfn[getid(i,j)]) continue;
int x=getid(i,j);tarjan(x,0);dfs(x,0,x);
rt[++tim]=0;fill(pre[tim],pre[tim]+4,0);
fill(suf[tim],suf[tim]+4,0);
}
}fill(suf[tim+1],suf[tim+1]+4,0);
for (int i=0;i<4;i++){
for (int j=2;j<=tim;j++){
if (rt[j]==rt[j-1]) pre[j][i]|=pre[j-1][i];
}for (int j=tim-1;j;j--){
if (rt[j]==rt[j+1]) suf[j][i]|=suf[j+1][i];
}
}return;
}
bool in(int x, int y){
return (st[x]>=st[y]&&ed[x]<=ed[y]);
}
bool check1(int x, int y, int cut){
if (rt[st[x]]!=rt[st[y]]) return 0;
if (in(x,cut)!=in(y,cut)) return 0;
for (int v:G[cut]){
if (v==fat[cut]) continue;
if (in(x,v)&&in(y,v)) return 1;
}if (!in(x,cut)&&!in(y,cut)) return 1;
return 0;
}
bool check2(int x, int d, int cut){
if (rt[st[x]]!=rt[st[cut]]) return dp[rt[st[x]]][d];
if (!in(x,cut)){
return pre[st[cut]-1][d]|suf[ed[cut]+1][d];
}for (int v:G[cut]){
if (v==fat[cut]) continue;
if (in(x,v)&&dp[v][d]) return 1;
}return 0;
}
void bfs(){
deque<pair<int,int>> que;
for (int i=0;i<=n*m;i++){
dis[i][0]=dis[i][1]=dis[i][2]=dis[i][3]=1e9+1;
}for (int k=0;k<4;k++){
if (invalid(posx[5]-dx[k],posy[5]-dy[k])) continue;
int x=getid(posx[4],posy[4]),z=getid(posx[5],posy[5]);
int y=getid(posx[5]-dx[k],posy[5]-dy[k]);
if (check1(x,y,z)||check2(x,k^2,z)){
que.emplace_back(z,k);dis[z][k]=0;
}
}while (que.size()){
auto [x,d]=que.front();auto [i,j]=getpos(x);
que.pop_front();int pos=getid(i-dx[d],j-dy[d]);
for (int k=0;k<4;k++){ // 绕
if (k==d||dis[x][k]<1e9) continue;
if (invalid(i-dx[k],j-dy[k])) continue;
int v=getid(i-dx[k],j-dy[k]);
if (check1(pos,v,x)||check2(pos,k^2,x)){ // 方向相反
if (dis[x][k]<1e9) continue;
que.emplace_front(x,k);dis[x][k]=dis[x][d];
}
}if (!invalid(i+dx[d],j+dy[d])){ // 推
int v=getid(i+dx[d],j+dy[d]);
if (dis[v][d]<1e9) continue;
que.emplace_back(v,d);dis[v][d]=dis[x][d]+1;
}else if (i+dx[d]>0&&j+dy[d]>0&&i+dx[d]<=n&&j+dy[d]<=m){
if (invalid(posx[d^2],posy[d^2])) continue;
int nx=getid(posx[d^2],posy[d^2]);
for (int k=0;k<4;k++){
if (invalid(i-dx[k],j-dy[k])||d==k) continue;
int v=getid(i-dx[k],j-dy[k]);
if (check1(nx,v,x)||check2(nx,k^2,x)){
if (dis[x][k]<1e9) continue;
que.emplace_front(x,k);dis[x][k]=dis[x][d];
}
}
} // 瞬移到四个点
}int ans=1e9;
for (int d=0;d<4;d++){
int i=posx[7],j=posy[7],x=getid(i-dx[d],j-dy[d]);
int v=getid(posx[6],posy[6]),z=getid(i,j);
if (dis[z][d]>=ans) continue;
if (check1(x,v,z)) ans=min(ans,dis[z][d]);
if (!invalid(i+dx[d],j+dy[d])) continue;
if (i+dx[d]<1||i+dx[d]>n||j+dy[d]<1||j+dy[d]>m) continue;
if (invalid(posx[d^2],posy[d^2])) continue;
int nx=getid(posx[d^2],posy[d^2]);
if (check1(nx,v,x)) ans=min(ans,dis[z][d]);
}if (ans==1e9) cout<<-1<<endl;else cout<<ans<<endl;
return;
}
void solve(){
cin>>n>>m;G.clear();a.clear();tot=0;
G.resize(2*n*m+10),a.resize(n+10);
fill(dfn,dfn+1+n*m,0);
for (int i=1;i<=n;i++){
string s;cin>>s;s='0'+s;
a[i].resize(m+10);
for (int j=1;j<=m;j++){
a[i][j]=(s[j]=='#');
if (s[j]=='p') posx[4]=i,posy[4]=j;
if (s[j]=='b') posx[5]=i,posy[5]=j;
if (s[j]=='=') posx[6]=i,posy[6]=j;
if (s[j]=='-') posx[7]=i,posy[7]=j;
}
}posx[0]=1,posy[0]=(m+1)/2;
posx[1]=(n+1)/2,posy[1]=m;
posx[2]=n,posy[2]=(m+1)/2;
posx[3]=(n+1)/2,posy[3]=1;
build();bfs();return;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);cout.tie(nullptr);
int t;cin>>t;while (t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 23964kb
input:
3 9 9 ######### #####..-# #..=##.## #.p.##.## ....##.## #...b..## #...##.## #....#### ####.#### 9 9 ######### #.......# #.#####.# #.#=....# ..#....-# ###.p.#.# #.....#b# #.....#.# ####.#### 9 9 ####.#### #....#### #.####.## #.......# #.......# ###.##### #=.b#..## #-..p..## #########
output:
7 4 19
result:
ok 3 number(s): "7 4 19"
Test #2:
score: 0
Accepted
time: 0ms
memory: 23896kb
input:
1 2 2 pb -=
output:
-1
result:
ok 1 number(s): "-1"
Test #3:
score: 0
Accepted
time: 102ms
memory: 23988kb
input:
100000 2 2 -p =b 2 2 b- =p 2 2 -b p= 2 2 -p b= 2 2 p= -b 2 2 b= -p 2 2 =b -p 2 2 b= -p 2 2 -p b= 2 2 b= -p 2 2 p= b- 2 2 pb =- 2 2 p- =b 2 2 -= pb 2 2 =p -b 2 2 p= -b 2 2 -b =p 2 2 =p -b 2 2 =b -p 2 2 =- bp 2 2 -= bp 2 2 bp =- 2 2 =- bp 2 2 =b -p 2 2 bp -= 2 2 -b p= 2 2 =p b- 2 2 b- p= 2 2 pb =- 2 2...
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
ok 100000 numbers
Test #4:
score: 0
Accepted
time: 22ms
memory: 41900kb
input:
1 500 200 #....#.#.#.##.#..#.#..#......#...#..#...#.#...........#.#.#..#...###..##..#.#.#.....#........#..####..#..#..#####...............#.#......#.##.#..##.........##.###.#....###..#...#.#...#...##..##.#...#. ##..##.....#.##..#.#.###.#....#...#..#.#....##..............##.....#...#....#..##.##...##...
output:
-1
result:
ok 1 number(s): "-1"
Test #5:
score: 0
Accepted
time: 20ms
memory: 46952kb
input:
1 200 500 ...##..#.....#.......##..##.#......#...#.....#.##.##..#...##...##....#..##.##..##..#..........####.......#.#...#....##....#.#..#..##....#..#......###...#.#..#.#..#..#.....#.#......#.........###....#..#.............#......#..#.#..####.#...#..#.#..##...#...##.###....#.####..#......#...#..#.....
output:
-1
result:
ok 1 number(s): "-1"
Test #6:
score: 0
Accepted
time: 46ms
memory: 64888kb
input:
1 100000 2 .. .# .. ## .. .. .. ## .# #. #. ## ## #. ## ## .. #. ## #. .. .. .. ## .# .# .# .. .# .# .# .# ## ## ## ## .. .. #. .# #. .. .# ## ## ## ## .# ## .. .# .. .. #. #. ## #. ## .# #. #. #. ## .# .. .# .# .# ## #. .. .. ## .. #. ## .# .. ## ## .# .# #. ## .. .# #. .. .. #. #. ## .. .# #. #. #...
output:
-1
result:
ok 1 number(s): "-1"
Test #7:
score: 0
Accepted
time: 31ms
memory: 62280kb
input:
1 2 100000 .......##........###.#.#..###.#....##.##.#.#.##..#..##.#.#...##.#...######...#....##..#####.###.##..#####...#....####..#.#.###...####.###..#..#.###..#########...#.#.####..##.#####..#...##.####..#.###..##..##...##...#.##.#.#..##....###..##.#...#......##...#.####.#....##...#.#....#.#.###.##...
output:
-1
result:
ok 1 number(s): "-1"
Test #8:
score: 0
Accepted
time: 39ms
memory: 45380kb
input:
10 19 64 ..#...........#.........#.............#..........#...#....#....# ....#.##......#......#..#........##.......#................#...# ...................#....#....##...#....#...#...#................ #...#....#...#.#....#.#...............#....#.................#.. ...........#......###...#.........
output:
-1 -1 -1 87 87 274 10 17 16 118
result:
ok 10 numbers
Test #9:
score: 0
Accepted
time: 36ms
memory: 33768kb
input:
10 89 145 ........#........#..#.#.................................#..#.....#......#...#...#.#.......#..........................#.#...#.............#....... ..#...#.............#....#...........................#...#....................#.....##..##.......##.....#.#..#...#....#....#.#.#...................
output:
102 259 127 9 50 107 38 16 -1 -1
result:
ok 10 numbers
Test #10:
score: 0
Accepted
time: 44ms
memory: 47372kb
input:
10 102 56 .#..#.............#...#...............#..........#...... .........#..............#.............#...#............. ......#.......................#........................# .#.............##..#..#....#............##.............. .#........#................#....#.#...........#......... ........
output:
117 -1 64 2 74 13 252 -1 -1 -1
result:
ok 10 numbers
Test #11:
score: 0
Accepted
time: 32ms
memory: 36332kb
input:
10 134 115 ...............................#...............#..............##........#.....#.......#...#...............#........ ...........#...#.......#....#.....#...............#.............#..................#......#.........#..........#... .............#.................#....................#.......
output:
31 75 157 45 9 99 47 60 -1 -1
result:
ok 10 numbers
Test #12:
score: 0
Accepted
time: 15ms
memory: 24068kb
input:
1000 3 3 #p= .#. b-. 9 4 p.#. ##.# .#.# .#.b ...# =### ###- ###. .### 4 10 .p..###.=. ##..#..##. .b..##.#.# .##.-.#... 2 12 ....#....#p# ...#-#=..b## 21 33 #.########....#.#..#.#.#..###.#.. .##.##.....##.##.....#..#...###.. ..#####..###...#..#..#.#..##..### .#.#..####..#...###..####.###...# ##.#.#.....
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2 ...
result:
ok 1000 numbers
Test #13:
score: 0
Accepted
time: 29ms
memory: 24196kb
input:
1000 20 15 ##.b#.....##.## ##...##...##... .#.#####..#.##. .###......##... .....#.....##.. =.#.#..##.#.#.. .#....#.....#.# #-.....#.....#. ##..###.#.#..#. #..##..#..#.... ..#.#....#..#.. #..##.....###.. .#..#........## ...##.#...#..#. #.p#.###..#..#. ...#.##....#.#. ##...#...###... ...#..###..#..# ....
output:
-1 -1 2 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 -1 -1 -1 2 -1 6 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 14 -1 3 3 5 -1 -1 -1 28 12 -1 17 -1 -1 -1 -1 ...
result:
ok 1000 numbers
Test #14:
score: 0
Accepted
time: 19ms
memory: 26296kb
input:
1000 3 12 ..b##p..#..# .#...=....#- ...#.##.#..# 6 11 ....b..#... .#...#..#.# #.##p.###.# .....=....# ..#-#..##.. ..#.##...#. 9 36 #..........#..#.............#.##.... ..#.......#............##........... ....##.#p.......b...#.......#..#.... ..#...#....#...#.......#...##...#.#. ....###.##........#.#...
output:
-1 -1 16 -1 -1 -1 -1 16 8 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 1 4 -1 4 4 -1 -1 -1 -1 -1 -1 5 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 3 -1 -1 -1 -1 -1 -1 9 -1 5 -1 -1 11 -1 -1 -1 -1 3 3 16 -1 -1 -1 -1 -1 7 19 -1 -1 -1 -1 35 -1 1 -1 -1 -1 -1 3 18 -1 -1 -1 9 -1 -1 -1 10 -1 4 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 21 -1 -1 ...
result:
ok 1000 numbers
Test #15:
score: 0
Accepted
time: 32ms
memory: 24360kb
input:
1000 8 15 ........#b#...= ...#....#..#... ....#.......#.. ..p-#.#......#. ....##..#..#... .#.#........... #..#..........# .#.....#...#... 4 26 ..............#...#.#..... .....p..#.........#.#....# .#.........#.=.#......##.. .##....b..............#.-. 8 2 .b p. .. -. #= .. #. .. 11 8 ..=..-.. ...###....
output:
-1 -1 -1 10 -1 4 2 6 -1 3 -1 -1 -1 -1 -1 3 -1 -1 -1 -1 4 -1 -1 -1 -1 -1 -1 5 20 49 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 9 -1 3 2 2 -1 -1 -1 -1 5 12 -1 -1 4 -1 8 -1 3 2 -1 -1 -1 -1 1 -1 -1 -1 12 -1 -1 -1 -1 -1 -1 -1 -1 -1 5 -1 -1 -1 -1 2 -1 -1 -1 -1 11 2 7 -1 14 6 -1 -1 -1 4 -1 -1 -1 2 -1 -1 -1 -1 -1 7 -1 -...
result:
ok 1000 numbers
Test #16:
score: 0
Accepted
time: 27ms
memory: 24148kb
input:
1000 12 4 .... ..#. #p.. ##.. .... #=.. .#.# .... .... ..#. .#b. #-.. 31 5 ..... ...#. .#.## ..#.. .#..# #...# ..#.. .#... ..#.. ..... ..... .#b.. #.=.. #..#. ..... ..... ..... ..... ..... ..... #.... ....# ...## ..... ##... #..-. ...#. ..#p. #.... ..... ..... 16 14 .............. .##.###....... ......
output:
-1 15 -1 -1 -1 -1 -1 2 -1 5 4 2 -1 -1 -1 5 3 2 -1 -1 -1 18 10 2 -1 -1 -1 -1 14 -1 -1 -1 -1 -1 10 -1 -1 10 4 -1 -1 -1 -1 -1 3 2 -1 -1 -1 -1 -1 2 1 3 -1 -1 8 -1 -1 19 -1 -1 -1 3 -1 -1 -1 -1 -1 7 -1 -1 1 -1 -1 -1 6 -1 -1 -1 2 1 -1 -1 -1 5 -1 -1 5 -1 2 -1 -1 4 -1 6 4 -1 -1 -1 -1 -1 17 -1 -1 -1 4 -1 -1 -...
result:
ok 1000 numbers