QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#882203 | #6314. 过河卒 | ETO_leader | 95 | 1365ms | 341176kb | C++23 | 6.7kb | 2025-02-04 22:03:24 | 2025-02-04 22:03:24 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define cir(i,a,b) for(int i=a;i<b;++i)
using namespace std;
#define fcin cin
#define fcout cout
static constexpr auto _inf=(int)(1e9+7);
enum user_type{usr_red,usr_black,TBD};
class graph{
private:
struct statu{
user_type win_usr;
int step;
statu(user_type w=TBD,int _step=_inf):win_usr(w),step(_step){}
};
vector<user_type> usr;
vector<vector<int>> gr,rgr;
vector<statu> res;
vector<int> vis,fcnt;
auto update(int u){
for(auto&v:rgr[u]) if(res[v].win_usr==usr[u]){
res[u].step=(res[u].win_usr==usr[u])?min(res[u].step,res[v].step+1):res[v].step+1;
res[u].win_usr=usr[u];
}else if(res[u].win_usr!=usr[u]&&res[v].win_usr!=TBD){
res[u].step=(res[u].win_usr!=TBD)?max(res[u].step,res[v].step+1):res[v].step+1;
res[u].win_usr=res[v].win_usr;
}
}
auto accessable(int u,int v){
--fcnt[u];
return (res[v].win_usr==usr[u])||(!fcnt[u]);
}
public:
auto link(int u,int v){
gr[u].emplace_back(v);
rgr[v].emplace_back(u);
++fcnt[v];
}
auto bfs(vector<int> rwin,vector<int> bwin,int qs){
queue<int> qwq;
for(auto&u:rwin) if(!vis[u]) res[u]=statu(usr_red,0),qwq.emplace(u),vis[u]=true;
for(auto&u:bwin) if(!vis[u]) res[u]=statu(usr_black,0),qwq.emplace(u),vis[u]=true;
if(res[qs].win_usr!=TBD) return res[qs];
while(!qwq.empty()){
const auto u=qwq.front();qwq.pop();
if(u==qs) break;
for(auto&v:gr[u]) if((!vis[v])&&accessable(v,u)){
update(v);
vis[v]=true;qwq.emplace(v);
}
}
return res[qs];
}
auto clear(vector<int> rusr,vector<int> busr){
for(auto&x:gr) x.clear();
for(auto&x:rgr) x.clear();
fill(fcnt.begin(),fcnt.end(),0);
fill(usr.begin(),usr.end(),TBD);
for(auto&u:rusr) usr[u]=usr_red;
for(auto&u:busr) usr[u]=usr_black;
fill(res.begin(),res.end(),statu());
fill(vis.begin(),vis.end(),false);
}
graph(int _n):gr(_n),rgr(_n),fcnt(_n),usr(_n,TBD),res(_n),vis(_n){
for(auto&x:gr) x.reserve(8);
for(auto&x:rgr) x.reserve(8);
}
};
static constexpr auto maxn=(int)(2e6+7);
int main(){
ios::sync_with_stdio(false),fcin.tie(nullptr);
int tid,T;fcin>>tid>>T;
graph gr(maxn);
vector<int> rusr,busr,rwin,bwin;
while(T--) [&]{
int n,m;fcin>>n>>m;
rusr.clear();busr.clear();rwin.clear();bwin.clear();
vector<string> script(n);
for(auto&s:script) fcin>>s;
vector<pair<int,int>> posb,posr;
cir(i,0,n) cir(j,0,m){
if(script[i][j]=='O') posr.emplace_back(i,j);
if(script[i][j]=='X') posb.emplace_back(i,j);
}
int cnt;
auto st_hash=[&](int usr,int uxb,int uyb,int rx1,int ry1,int rx2,int ry2){
if(rx1>rx2||(rx1==rx2&&ry1>ry2)){
swap(rx1,rx2);swap(ry1,ry2);
}
return
usr*1000000+
uxb*100000+
uyb*10000+
rx1*1000+
ry1*100+
rx2*10+
ry2*1;
};
auto vaild=[&](int uxb,int uyb,int rx1,int ry1,int rx2,int ry2){
if(rx1==rx2&&ry1==ry2) return false;
if(uxb<0||uxb>n-1||uyb<0||uyb>m-1||rx1<0||rx1>n-1||ry1<0||ry1>m-1||rx2<0||rx2>n-1||ry2<0||ry2>m-1) return false;
return script[uxb][uyb]!='#'&&script[rx1][ry1]!='#'&&script[rx2][ry2]!='#';
};
cir(uxb,0,n) cir(uyb,0,m) cir(rx1,0,n) cir(ry1,0,m) cir(rx2,0,n) cir(ry2,0,m){
rusr.emplace_back(st_hash(usr_red,uxb,uyb,rx1,ry1,rx2,ry2));
busr.emplace_back(st_hash(usr_black,uxb,uyb,rx1,ry1,rx2,ry2));
}
gr.clear(rusr,busr);
cir(uxb,0,n) cir(uyb,0,m) cir(rx1,0,n) cir(ry1,0,m) cir(rx2,0,n) cir(ry2,0,m) if(rx1<rx2||(rx1==rx2&&ry1<ry2)){
if(!vaild(uxb,uyb,rx1,ry1,rx2,ry2)) continue;
// Move Red
[&]{
if(uxb==rx1&&uyb==ry1) return;
if(uxb==rx2&&uyb==ry2) return;
auto cnt=0;
cir(dx,-1,2) cir(dy,-1,2) if(abs(dx)^abs(dy)){
if(vaild(uxb,uyb,rx1+dx,ry1+dy,rx2,ry2)){
++cnt;
gr.link(st_hash(usr_black,uxb,uyb,rx1+dx,ry1+dy,rx2,ry2),st_hash(usr_red,uxb,uyb,rx1,ry1,rx2,ry2));
}
if(vaild(uxb,uyb,rx1,ry1,rx2+dx,ry2+dy)){
++cnt;
gr.link(st_hash(usr_black,uxb,uyb,rx1,ry1,rx2+dx,ry2+dy),st_hash(usr_red,uxb,uyb,rx1,ry1,rx2,ry2));
}
}
if(!cnt) bwin.emplace_back(st_hash(usr_red,uxb,uyb,rx1,ry1,rx2,ry2)); // Can't move
}();
// Move Black
[&]{
set<pair<int,int>> ok{{-1,0},{0,1},{0,-1}};
if(uxb==rx1&&uyb==ry1) return;
if(uxb==rx2&&uyb==ry2) return;
auto cnt=0;
cir(dx,-1,2) cir(dy,-1,2) if(ok.count({dx,dy})){
const auto nx=uxb+dx,ny=uyb+dy;
if(vaild(uxb+dx,uyb+dy,rx1,ry1,rx2,ry2)){
++cnt;
gr.link(st_hash(usr_red,uxb+dx,uyb+dy,rx1,ry1,rx2,ry2),st_hash(usr_black,uxb,uyb,rx1,ry1,rx2,ry2));
}
}
if(!cnt) rwin.emplace_back(st_hash(usr_black,uxb,uyb,rx1,ry1,rx2,ry2));
}();
}
// Eaten
cir(uxb,0,n) cir(uyb,0,m) cir(rx,0,n) cir(ry,0,m){
if(vaild(uxb,uyb,uxb,uyb,rx,ry)){
rwin.emplace_back(st_hash(usr_black,uxb,uyb,uxb,uyb,rx,ry));
bwin.emplace_back(st_hash(usr_red,uxb,uyb,uxb,uyb,rx,ry));
}
if(vaild(uxb,uyb,rx,ry,uxb,uyb)){
rwin.emplace_back(st_hash(usr_black,uxb,uyb,rx,ry,uxb,uyb));
bwin.emplace_back(st_hash(usr_red,uxb,uyb,rx,ry,uxb,uyb));
}
}
// Arrive
cir(uyb,0,m) cir(rx1,0,n) cir(ry1,0,m) cir(rx2,0,n) cir(ry2,0,m){
if(vaild(0,uyb,rx1,ry1,rx2,ry2)) bwin.emplace_back(st_hash(usr_red,0,uyb,rx1,ry1,rx2,ry2));
}
// Get Answer
const auto[result,step]=gr.bfs(
rwin,bwin,
st_hash(usr_red,posb[0].first,posb[0].second,posr[0].first,posr[0].second,posr[1].first,posr[1].second)
);
if(result==TBD) fcout<<"Tie\n";
else fcout<<(result==usr_red?"Red":"Black")<<' '<<step<<'\n';
fcout.flush();
}();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 5
Accepted
time: 1000ms
memory: 340936kb
input:
1 10 10 10 .#......#. ..#....#.. #..#..#..# ..#.O..#.. .#......#. ...####... ##..X...## .......... .##.O####. .......... 10 10 .##..##... .....##... ..X#.#.... #........# ..#.#.#... .#...#.#.. #..#.#.#.. ..#..#.#.# ...##O#... ..##O#.#.. 4 1 O O # X 10 10 .##.....## ...#....## .#.#...#.. .O###..... #...
output:
Tie Black 0 Black 0 Tie Black 0 Tie Black 0 Black 0 Tie Tie
result:
ok 10 lines
Test #2:
score: 5
Accepted
time: 897ms
memory: 340972kb
input:
2 10 10 10 .#.####..# .##..##### ##.###X### #....####. .#.####### #.#O###.## ..##.####. ..######## ########## ##O#.#.### 10 10 ..#.###.## ......#..# ....#O.... #..#.....# ...#.##### .....#..#. ..#.....#O X#....###. #.....##.. .#..##.... 10 10 .......##. .O##...##. ..#....... ####..O... ....#...## .....
output:
Black 0 Tie Tie Black 0 Black 0 Tie Black 0 Tie Tie Black 0
result:
ok 10 lines
Test #3:
score: 5
Accepted
time: 849ms
memory: 340696kb
input:
3 10 10 10 ##.####### ###..###OO ##X####.## ...####### #..###...# ##...##### ##..#.#### ..##..##.# ###..#.#.# #.###..##. 10 10 .##..##... .....##... ..X#.#.... #........# ..#.#.#... .#...#.#.. #..#.#.#.. ..#..#.#.# ...##O#... ..##O#.#.. 10 10 .......... .X........ .......... .......... ..#....... .....
output:
Black 0 Black 0 Black 0 Black 0 Black 0 Tie Tie Tie Tie Tie
result:
ok 10 lines
Test #4:
score: 5
Accepted
time: 745ms
memory: 340508kb
input:
4 10 10 10 .#......#. ..#....#.. #..#..#..# ..#.O..#.. .#......#. ...####... ##..X...## .......... .##.O####. .......... 10 10 ...#.##... ..####.##. ###.###### .####O#.X# ...####..# .##O#..#.# ##.#..###. #.#.#....# .#.#####.# .##.#.#.## 3 2 OO ## #X 10 10 .##.##...# ..##..#.#O .#O#.#...# #.#.#..##. ...
output:
Tie Black 0 Black 0 Black 0 Black 0 Black 0 Tie Tie Tie Tie
result:
ok 10 lines
Test #5:
score: 5
Accepted
time: 1318ms
memory: 341044kb
input:
5 10 10 10 .......... ....O..... .......... ...X...... .......... .......... .......... .......... ########## .......O.. 10 10 .......... ..O....... .......... .......... .......... X......... .......... .......... ########## .......O.. 10 1 . . . O . . . X # O 10 1 O . . . . . . X # O 10 10 ..........
output:
Red 9 Red 21 Black 12 Red 7 Black 8 Red 23 Black 14 Red 25 Red 23 Red 1
result:
ok 10 lines
Test #6:
score: 5
Accepted
time: 1205ms
memory: 341000kb
input:
6 10 10 10 .....O.... .......... .......... .......... .......... .X........ .......... .......... ########## ...O...... 10 1 O . . . . . . X # O 10 10 .......... ..O....... .......... .......... .......... X......... .......... .......... ########## .......O.. 10 10 .......... .....O.... .............
output:
Red 17 Red 7 Red 21 Red 17 Black 2 Black 12 Black 6 Red 25 Red 23 Black 10
result:
ok 10 lines
Test #7:
score: 5
Accepted
time: 210ms
memory: 323748kb
input:
7 10 10 1 . O # . . X . # . O 10 1 O . . . . . . X . O 10 1 . . # O O # # . . X 5 1 O O . X . 10 1 O # . . . . . X . O 9 1 O # O . . . . . X 10 1 . . . . X O . # . O 10 1 O O . # . . . . . X 10 1 . . . . . . X . O O 10 1 O . . . # X # O . .
output:
Red 5 Red 7 Black 0 Black 2 Red 11 Black 10 Red 1 Red 11 Black 12 Red 1
result:
ok 10 lines
Test #8:
score: 5
Accepted
time: 196ms
memory: 323764kb
input:
8 10 10 1 . . # . X # . O # O 10 1 . O O . X . . . . . 10 1 O O . . . . . . . X 5 1 . # O X O 10 1 O # . . . . X . . O 9 1 O # O . . . . X . 10 1 # . . . X O . # . O 10 1 O O # # . . . . . X 10 1 # . . . . . X . O O 10 1 . # O . # O . X # .
output:
Red 3 Red 3 Red 9 Red 1 Red 9 Red 5 Red 1 Black 0 Red 11 Red 3
result:
ok 10 lines
Test #9:
score: 5
Accepted
time: 217ms
memory: 323636kb
input:
9 10 10 1 # O # . . X . # . O 10 1 O . . . X . . . . O 10 1 . . # O O . # . . X 5 1 O O . X . 10 1 O # . # . . . X . O 9 1 O . # O . . . X . 10 1 . . . . X O . . . O 10 1 O O . # # . . . . X 10 1 . . . . X . # . O O 10 1 . # O # O . . X # .
output:
Red 5 Red 5 Red 5 Black 2 Red 7 Red 5 Red 1 Red 9 Black 8 Red 3
result:
ok 10 lines
Test #10:
score: 5
Accepted
time: 981ms
memory: 340824kb
input:
10 10 10 10 .###..###. .....O...# .##.#..... ..##.##.X# ##......#. #...#..... ....##...# ..#..O##.# #..#.##... .....##.#. 10 10 #...##..#. #......##. ..##....#. #.#.##..#O .O...#.##. .....##.X. .###...... ....#.#.#. .......##. ###...##.# 10 10 #.#....... ..##..##.. ..##.#X..O ....#..... #..#....#. #...
output:
Red 7 Red 5 Red 3 Black 2 Red 9 Tie Tie Red 9 Red 9 Red 7
result:
ok 10 lines
Test #11:
score: 5
Accepted
time: 1062ms
memory: 340804kb
input:
11 10 10 10 ...#.....# ..#....... ##.##.###. ##...#.##X .....#...# ...#.#.O.# ..#...#... .....#.... ......#..# #...#...O# 10 10 ..###O#O#. .#.###.##. ##..#..#.. ....#X.... ........## ........## #...##.... ...#..###. ........#. ..#...#.#. 10 10 ######...# O.X.O####. #.#.#.#... #.......#. ...##...#. ....
output:
Black 8 Black 0 Black 2 Tie Tie Red 9 Tie Red 9 Red 9 Red 1
result:
ok 10 lines
Test #12:
score: 5
Accepted
time: 1132ms
memory: 341120kb
input:
12 10 10 10 ##..##..O. .#........ .#.......# .......... #......... .XO##..... #......... .......... .......... .......... 5 6 #.#### #.#### #.OO## #X#### ###### 10 10 ....###### .######### O...##.### .######### ....###### ####....#. ####.####. ####.O..#. #######.#. ####....#X 10 10 .......... .........
output:
Red 1 Black 2 Red 9 Tie Red 9 Black 6 Tie Tie Red 3 Tie
result:
ok 10 lines
Test #13:
score: 5
Accepted
time: 1157ms
memory: 341172kb
input:
13 10 10 10 .##...#... .#####..## #..#..#..# #........# #...#.#.#. ....#..... ..#####... ....#..#.. ..##O#.... .X...O##.# 10 10 .##......# ..#..##..O ...#.##... ##..#O..## ..#...#... ###.....#X ..#....... .##.#.#... .#.#.#..#. #......#.# 10 10 .........# .......... O......... .......... ....O..... ....
output:
Black 6 Red 9 Red 9 Red 9 Black 8 Tie Red 7 Red 7 Tie Tie
result:
ok 10 lines
Test #14:
score: 0
Time Limit Exceeded
input:
14 10 10 10 .......O.. .......... .......... O......... .......... .......... .......... .......... .......X.. .......... 10 10 .##...#... .#####..## ...#..##.# #........# #...#.#.#. ....#..... ..#####... ....#..#.. ..##O#.... .X...O##.# 10 10 .#..O...#. ...####... ##.X....## .......... .#.O..#... ....
output:
Red 27 Black 6 Tie Black 8 Red 17 Red 63 Black 12 Red 17 Black 16
result:
Test #15:
score: 5
Accepted
time: 951ms
memory: 340156kb
input:
15 10 10 10 .########O .........# ########.# .........# .######### .......... #########. .......... .######### .......O.X 10 10 .##...#... .#####..## ...#..##.# #........# #...#.#.#. ....#..... ..#####... ....#..#.. ..##O#.... .X...O##.# 10 8 ####.... ...#..#. .###..#. ...#.O#. ##.#..#. ...####. .##...
output:
Black 102 Black 6 Red 13 Red 9 Tie Red 63 Red 93 Red 139 Red 45 Black 44
result:
ok 10 lines
Test #16:
score: 5
Accepted
time: 1012ms
memory: 340344kb
input:
16 10 10 10 ....###### .######### O...##.### .######### ....###### ####....#. ####.####. ####.O..#. #######.#. ####....#X 10 10 .#..O...#. ...####... ##.X....## .......... .#.O..#... .#....#... .#....#... ..####.... .......... .......... 10 10 O######### #..#..#... ..#..#..## .#..#..#.. ...#..#..# ....
output:
Red 9 Tie Black 50 Red 333 Red 41 Black 6 Red 133 Black 24 Red 111 Red 101
result:
ok 10 lines
Test #17:
score: 5
Accepted
time: 1365ms
memory: 340920kb
input:
17 10 10 10 ########## .......... .......... ..######## .......... #########. .O........ .......... #......... O#X....... 10 10 .##...#... .#####..## ...#..##.# #........# #...#.#.#. ....#..... ..#####... ....#..#.. ..##O#.... .X...O##.# 10 10 #.......O. ..#....... ...##.##.. .......... .......... ....
output:
Black 60 Black 6 Red 53 Red 35 Tie Tie Red 9 Red 65 Tie Red 63
result:
ok 10 lines
Test #18:
score: 5
Accepted
time: 1042ms
memory: 341176kb
input:
18 10 10 10 O......... #..#..###. .#.##...#. ..X....... .####..... .##...##.# ...#..###. ...###..O# ..#..#.... .......### 10 10 ...#.....O ........#. ........X. .......... ...#...... .......... .........# .....O.... ..#....... .......... 10 10 ..O.#..... #.#...#... ...#..###. ....#..... ..#..#..#. #...
output:
Red 159 Tie Red 73 Black 36 Black 56 Black 6 Red 15 Black 0 Red 63 Black 2
result:
ok 10 lines
Test #19:
score: 5
Accepted
time: 1105ms
memory: 341028kb
input:
19 10 10 10 .....#.... ...#.#.#.# #..#.##.#. .##.....#. .#..##.#.# ...#...... #.#.#.##.. ##...#O##. ..#.#O.... .#...#..X. 10 10 .O....#... .####....# .###...#.. .......... ###....##. ....#..##. .#.#.#.... .##.O#..#. #..#...#.. ...#....X# 10 10 O######### #..#..#... ..#..#..## .#..#..#.. ...#..#..# ....
output:
Black 6 Red 57 Black 50 Red 11 Tie Red 129 Red 99 Black 0 Red 111 Red 27
result:
ok 10 lines
Test #20:
score: 5
Accepted
time: 1019ms
memory: 340576kb
input:
20 10 10 10 .##.....## ...#....## .#.#...#.. .O###..... ###....... ......###. .....#..O. ##........ ..#...X... .##....... 10 10 #...#.##.. .O##.....# #.#.....#. .#...#.... ....#..#.. .###...... ....O##.#. .#.#.....# .......... .#.#...X#. 10 10 ......###. ..#.#..##. .#.......# .#.#..###. ..#.#..#.. #...
output:
Tie Red 63 Red 61 Red 9 Black 6 Black 0 Tie Red 333 Red 57 Tie
result:
ok 10 lines