#include <bits/stdc++.h>
using namespace std;
const int limit = 75000;
const int oo = 0x3f3f3f3f;
int n,m;
bool sus[105][105],st[105][105];
int sel[105][105];
bool loop = false;
int nr = 0;
int cnt = 0;
vector<pair<int,int>> c;
unordered_map<string,short int> rez[2];
unordered_map<string,bool> reached[2];
int nrop = 0;
void Fill(int i, int j, int si, int sj)
{
++nr;
sel[i][j] = cnt;
if(!sus[i][j] && i-1>0)
{
if(sel[i-1][j])
{
if(sel[i-1][j]==cnt && (i-1!=si || j!=sj))
{
loop = true;
}
}
else
{
Fill(i-1,j,i,j);
}
}
if(!sus[i+1][j] && i+1<=n)
{
if(sel[i+1][j])
{
if(sel[i+1][j]==cnt && (i+1!=si || j!=sj))
{
loop = true;
}
}
else
{
Fill(i+1,j,i,j);
}
}
if(!st[i][j] && j-1>0)
{
if(sel[i][j-1])
{
if(sel[i][j-1]==cnt && (i!=si || j-1!=sj))
{
loop = true;
}
}
else
{
Fill(i,j-1,i,j);
}
}
if(!st[i][j+1] && j+1<=m)
{
if(sel[i][j+1])
{
if(sel[i][j+1]==cnt && (i!=si || j+1!=sj))
{
loop = true;
}
return;
}
else
{
Fill(i,j+1,i,j);
}
}
}
int Back(int turn, string state)
{
++nrop;
reached[turn][state] = true;
if(turn)
{
int Max = -oo;
for(int i=0; i<c.size(); i++)
{
if(state[i]=='1')
{
continue;
}
int Min = oo;
string new_state = state;
new_state[i] = '1';
if(reached[0][new_state])
{
Min = min(Min,rez[0][new_state] - c[i].first);
}
else
{
Min = min(Min,Back(0,new_state) - c[i].first);
}
if(c[i].second==false && c[i].first>2)
{
if(reached[1][new_state])
{
Min = min(Min,rez[1][new_state] - c[i].first + 4);
}
else
{
Min = min(Min,Back(1,new_state) - c[i].first + 4);
}
}
if(c[i].second==true)
{
if(reached[1][new_state])
{
Min = min(Min,rez[1][new_state] - c[i].first + 8);
}
else
{
Min = min(Min,Back(1,new_state) - c[i].first + 8);
}
}
Max = max(Max,Min);
if(nrop>limit)
{
break;
}
}
if(Max==-oo)
{
Max = 0;
}
rez[1][state] = Max;
return Max;
}
else
{
int Min = oo;
for(int i=0; i<c.size(); i++)
{
if(state[i]=='1')
{
continue;
}
int Max = -oo;
string new_state = state;
new_state[i] = '1';
if(reached[1][new_state])
{
Max = max(Max,rez[1][new_state] + c[i].first);
}
else
{
Max = max(Max,Back(1,new_state) + c[i].first);
}
if(c[i].second==false && c[i].first>2)
{
if(reached[0][new_state])
{
Max = max(Max,rez[0][new_state] + c[i].first - 4);
}
else
{
Max = max(Max,Back(0,new_state) + c[i].first - 4);
}
}
if(c[i].second==true)
{
if(reached[0][new_state])
{
Max = max(Max,rez[0][new_state] + c[i].first - 8);
}
else
{
Max = max(Max,Back(0,new_state) + c[i].first - 8);
}
}
Min = min(Min,Max);
if(nrop>limit)
{
break;
}
}
if(Min==oo)
{
Min = 0;
}
rez[0][state] = Min;
return Min;
}
}
bool cmp(pair<int,int> a, pair<int,int> b)
{
if(a.second==b.second)
{
return a.first<b.first;
}
bool ok1 = false, ok2 = false;
if((a.second==false && a.first>3) || (a.second==true && a.second>=4))
{
ok1 = true;
}
if((b.second==false && b.first>3) || (b.second==true && b.second>=4))
{
ok2 = true;
}
if(ok1 && ok2)
{
return (a.first<b.first);
}
if(!ok1 && !ok2)
{
return a.first<b.first;
}
return (ok1<ok2);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1; i<=n+1; i++)
{
for(int j=1; j<=m; j++)
{
char ch;
cin>>ch;
sus[i][j] = ch-'0';
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m+1; j++)
{
char ch;
cin>>ch;
st[i][j] = ch-'0';
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(sus[i][j] && sus[i+1][j] && st[i][j] && st[i][j+1])
{
sel[i][j] = -1;
continue;
}
if(sel[i][j])
{
continue;
}
nr = 0;
++cnt;
loop = false;
Fill(i,j,0,0);
c.push_back({nr,loop});
}
}
int nr = 0;
for(int i=0; i<c.size(); i++)
{
if((c[i].first<=6 && c[i].second==true) || (c[i].first<=3 && c[i].second==false))
{
++nr;
}
}
for(int i=0; i<c.size(); i++)
{
for(int j=i+1; j<c.size(); j++)
{
if(!cmp(c[i],c[j]))
{
swap(c[i],c[j]);
}
}
}
string init;
for(int i=0; i<c.size(); i++)
{
init.push_back('0');
}
cout<<Back(1,init)<<'\n';
return 0;
}
Compilation message
game.cpp: In function 'int Back(int, std::string)':
game.cpp:85:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i=0; i<c.size(); i++)
| ~^~~~~~~~~
game.cpp:140:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
140 | for(int i=0; i<c.size(); i++)
| ~^~~~~~~~~
game.cpp: In function 'int main()':
game.cpp:262:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
262 | for(int i=0; i<c.size(); i++)
| ~^~~~~~~~~
game.cpp:269:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
269 | for(int i=0; i<c.size(); i++)
| ~^~~~~~~~~
game.cpp:271:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
271 | for(int j=i+1; j<c.size(); j++)
| ~^~~~~~~~~
game.cpp:280:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
280 | for(int i=0; i<c.size(); i++)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
588 KB |
Output is correct |
3 |
Correct |
5 ms |
844 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
7 ms |
844 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
11 ms |
1484 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
131 ms |
9732 KB |
Output is correct |
10 |
Correct |
205 ms |
15736 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
5 ms |
844 KB |
Output is correct |
15 |
Correct |
13 ms |
1484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
0 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
0 ms |
332 KB |
Output is correct |
47 |
Correct |
0 ms |
332 KB |
Output is correct |
48 |
Correct |
0 ms |
332 KB |
Output is correct |
49 |
Correct |
0 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
3 ms |
588 KB |
Output is correct |
53 |
Correct |
5 ms |
844 KB |
Output is correct |
54 |
Correct |
1 ms |
332 KB |
Output is correct |
55 |
Correct |
7 ms |
844 KB |
Output is correct |
56 |
Correct |
2 ms |
460 KB |
Output is correct |
57 |
Correct |
11 ms |
1484 KB |
Output is correct |
58 |
Correct |
2 ms |
460 KB |
Output is correct |
59 |
Correct |
131 ms |
9732 KB |
Output is correct |
60 |
Correct |
205 ms |
15736 KB |
Output is correct |
61 |
Correct |
2 ms |
588 KB |
Output is correct |
62 |
Correct |
2 ms |
460 KB |
Output is correct |
63 |
Correct |
2 ms |
460 KB |
Output is correct |
64 |
Correct |
5 ms |
844 KB |
Output is correct |
65 |
Correct |
13 ms |
1484 KB |
Output is correct |
66 |
Correct |
303 ms |
20404 KB |
Output is correct |
67 |
Correct |
315 ms |
22720 KB |
Output is correct |
68 |
Correct |
285 ms |
22772 KB |
Output is correct |
69 |
Correct |
279 ms |
20484 KB |
Output is correct |
70 |
Correct |
296 ms |
20460 KB |
Output is correct |
71 |
Correct |
302 ms |
22972 KB |
Output is correct |
72 |
Correct |
368 ms |
22756 KB |
Output is correct |
73 |
Correct |
403 ms |
32192 KB |
Output is correct |
74 |
Correct |
306 ms |
22708 KB |
Output is correct |
75 |
Correct |
316 ms |
22756 KB |
Output is correct |
76 |
Correct |
328 ms |
22876 KB |
Output is correct |
77 |
Correct |
320 ms |
22784 KB |
Output is correct |
78 |
Correct |
286 ms |
22804 KB |
Output is correct |
79 |
Correct |
283 ms |
22804 KB |
Output is correct |
80 |
Correct |
308 ms |
22804 KB |
Output is correct |
81 |
Correct |
331 ms |
20432 KB |
Output is correct |
82 |
Correct |
302 ms |
22804 KB |
Output is correct |
83 |
Correct |
321 ms |
22736 KB |
Output is correct |