#include <bits/stdc++.h>
using namespace std;
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;
int p[105];
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 k, bool turn, int last)
{
++nrop;
if(turn==1)
{
int Max = -oo;
for(int i=c.size()-1; i>=0; i--)
{
if(c[i].second==true)
{
if(p[i]==1)
{
p[i] = 3;
Max = max(Max,Back(k+1,1,1)+c[i].first);
p[i] = 1;
}
}
else
{
if(p[i]==1)
{
p[i] = 3;
Max = max(Max,Back(k+1,1,1)+c[i].first);
p[i] = 1;
}
}
if(nrop>10000000)
{
break;
}
}
for(int i=c.size()-1; i>=0; i--)
{
if(nrop>10000000)
{
break;
}
if(c[i].second==true)
{
if(p[i]==1)
{
if(c[i].first>4)
{
p[i] = 2;
Max = max(Max,Back(k+1,0,1)+c[i].first-4);
p[i] = 1;
}
}
}
else
{
if(p[i]==1)
{
if(c[i].first>2)
{
p[i] = 2;
Max = max(Max,Back(k+1,0,1)+c[i].first-2);
p[i] = 1;
}
}
}
}
for(int i=0; i<c.size(); i++)
{
if(nrop>10000000)
{
break;
}
if(c[i].second==true)
{
if(p[i]==2)
{
p[i] = 3;
Max = max(Max,Back(k+1,1,1)+4);
p[i] = 2;
break;
}
}
else
{
if(p[i]==2)
{
p[i] = 3;
Max = max(Max,Back(k+1,1,1)+2);
p[i] = 2;
break;
}
}
}
for(int i=0; i<c.size(); i++)
{
if(nrop>10000000)
{
break;
}
if(c[i].second==true)
{
if(p[i]==0 && last)
{
p[i] = 1;
Max = max(Max,Back(k+1,0,0));
p[i] = 0;
}
}
else
{
if(p[i]==0 && last)
{
p[i] = 1;
Max = max(Max,Back(k+1,0,0));
p[i] = 0;
}
}
}
if(Max==-oo)
{
Max = 0;
}
return Max;
}
else
{
int Min = oo;
for(int i=c.size(); i>=0; i--)
{
if(nrop>10000000)
{
break;
}
if(c[i].second==true)
{
if(p[i]==1)
{
p[i] = 3;
Min = min(Min,Back(k+1,0,1)-c[i].first);
p[i] = 1;
}
}
else
{
if(p[i]==1)
{
p[i] = 3;
Min = min(Min,Back(k+1,0,1)-c[i].first);
p[i] = 1;
}
}
}
for(int i=c.size(); i>=0; i--)
{
if(nrop>10000000)
{
break;
}
if(c[i].second==true)
{
if(p[i]==1)
{
if(c[i].first>4)
{
p[i] = 2;
Min = min(Min,Back(k+1,1,1)-(c[i].first-4));
p[i] = 1;
}
}
}
else
{
if(p[i]==1)
{
if(c[i].first>2)
{
p[i] = 2;
Min = min(Min,Back(k+1,1,1)-(c[i].first-2));
p[i] = 1;
}
}
}
}
for(int i=0; i<c.size(); i++)
{
if(nrop>10000000)
{
break;
}
if(c[i].second==true)
{
if(p[i]==2)
{
p[i] = 3;
Min = min(Min,Back(k+1,0,1)-4);
p[i] = 2;
break;
}
}
else
{
if(p[i]==2)
{
p[i] = 3;
Min = min(Min,Back(k+1,0,1)-2);
p[i] = 2;
break;
}
}
}
for(int i=0; i<c.size(); i++)
{
if(nrop>10000000)
{
break;
}
if(c[i].second==true)
{
if(p[i]==0 && last)
{
p[i] = 1;
Min = min(Min,Back(k+1,1,0));
p[i] = 0;
}
}
else
{
if(p[i]==0 && last)
{
p[i] = 1;
Min = min(Min,Back(k+1,1,0));
p[i] = 0;
}
}
}
if(Min==oo)
{
Min = 0;
}
return Min;
}
}
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});
}
}
sort(c.begin(),c.end());
cout<<Back(1,1,-1)<<'\n';
return 0;
}
Compilation message
game.cpp: In function 'int Back(int, bool, int)':
game.cpp:138: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]
138 | for(int i=0; i<c.size(); i++)
| ~^~~~~~~~~
game.cpp:165: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]
165 | for(int i=0; i<c.size(); i++)
| ~^~~~~~~~~
game.cpp:256: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]
256 | for(int i=0; i<c.size(); i++)
| ~^~~~~~~~~
game.cpp:283: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]
283 | for(int i=0; i<c.size(); i++)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
20 ms |
324 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
20 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
510 ms |
312 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 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 |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
20 ms |
324 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
20 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
0 ms |
332 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
332 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
332 KB |
Output is correct |
40 |
Correct |
0 ms |
332 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
0 ms |
204 KB |
Output is correct |
46 |
Correct |
0 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
0 ms |
204 KB |
Output is correct |
50 |
Correct |
0 ms |
332 KB |
Output is correct |
51 |
Execution timed out |
510 ms |
312 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |