#include<bits/stdc++.h>
using namespace std;
const int maxn=10,maxm=1000;
int all[maxn][maxm],dp0[(1<<maxn)],dp1[(1<<maxn)],n,m;
void vorod(){
cin>>n>>m;
swap(n,m);
for(int j=0;j<m;j++){
for(int i=0;i<n;i++){
char c;
cin>>c;
if(c=='#'){
all[i][j]=1;
}else{
all[i][j]=0;
}
}
}
}
void solve(){
for(int i=0;i<(1<<n);i++){
for(int j=0;j<n;j++){
if(all[j][0]==0){
continue;
}
if((i>>j)&1){
dp0[i]++;
}else{
if(j==0||all[j-1][0]==0||((i>>(j-1))&1)==1){
dp0[i]++;
}
}
}
}
for(int j=1;j<m;j++){
for(int i=0;i<n;i++){
for(int mask=0;mask<(1<<n);mask++){
if(all[i][j]==0){
dp1[mask]=min(dp0[mask],dp0[mask^(1<<i)]);
}else{
if(((mask>>i)&1)==1){
if(all[i][j-1]==0){
dp1[mask]=dp0[mask]+1;
}else{
dp1[mask]=min(dp0[mask],dp0[mask^(1<<i)]+1);
}
}else{
if(i==0||all[i-1][j]==0||((mask>>(i-1))&1)==1){
dp1[mask]=min(dp0[mask],dp0[mask^(1<<i)])+1;
}else{
dp1[mask]=min(dp0[mask],dp0[mask^(1<<i)]);
}
}
}
}
for(int mask=0;mask<(1<<n);mask++){
dp0[mask]=dp1[mask];
dp1[mask]=0;
}
}
}
int res=n*m;
for(int mask=0;mask<(1<<n);mask++){
res=min(res,dp0[mask]);
}
cout<<res<<"\n";
}
int main(){
vorod();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
12 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
492 KB |
Output is correct |
5 |
Correct |
13 ms |
348 KB |
Output is correct |
6 |
Correct |
13 ms |
348 KB |
Output is correct |
7 |
Correct |
14 ms |
348 KB |
Output is correct |
8 |
Correct |
12 ms |
348 KB |
Output is correct |
9 |
Correct |
12 ms |
492 KB |
Output is correct |
10 |
Correct |
14 ms |
496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
12 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
492 KB |
Output is correct |
5 |
Correct |
13 ms |
348 KB |
Output is correct |
6 |
Correct |
13 ms |
348 KB |
Output is correct |
7 |
Correct |
14 ms |
348 KB |
Output is correct |
8 |
Correct |
12 ms |
348 KB |
Output is correct |
9 |
Correct |
12 ms |
492 KB |
Output is correct |
10 |
Correct |
14 ms |
496 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
348 KB |
Output is correct |
21 |
Correct |
7 ms |
448 KB |
Output is correct |
22 |
Correct |
16 ms |
348 KB |
Output is correct |
23 |
Correct |
12 ms |
348 KB |
Output is correct |
24 |
Correct |
13 ms |
492 KB |
Output is correct |
25 |
Correct |
14 ms |
348 KB |
Output is correct |
26 |
Correct |
18 ms |
348 KB |
Output is correct |
27 |
Correct |
16 ms |
348 KB |
Output is correct |
28 |
Correct |
17 ms |
348 KB |
Output is correct |