#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;int m;
string gr[1010];
int dp[1010][12][2510];
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>gr[i];
}
for(int i=0;i<=n;i++){
for(int j=0;j<m;j++){
for(int A=0;A<(1<<m);A++){
dp[i][j][A]=1e9;
}
}
}
dp[0][m-1][0]=0;
for(int i=0;i<=n;i++){
for(int j=0;j<m-1;j++){
for(int A=0;A<(1<<m);A++){
//延伸
if(gr[i][j+1]=='#'&& ((A&(1<<(j+1)))!=0) ){
dp[i][j+1][A]=min(dp[i][j+1][A],dp[i][j][A]);
}
if(gr[i][j+1]=='#'&& ((A&(1<<(j)))==0)&&gr[i][j]=='#' ){
int xr=0;
if( ((A&(1<<(j+1)))!=0) ){xr+=(1<<(j+1));}
dp[i][j+1][A^xr]=min(dp[i][j+1][A^xr],dp[i][j][A]);
}
//延伸
//直接加
if(gr[i][j+1]=='#'){
dp[i][j+1][A|(1<<(j+1))]=min(dp[i][j+1][A|(1<<(j+1))],dp[i][j][A]+1);
}
if(gr[i][j+1]=='#'){
int xr=0;
if( ((A&(1<<(j+1)))!=0) ){xr=(1<<(j+1)); }
dp[i][j+1][A^xr]=min(dp[i][j+1][A^xr],dp[i][j][A]+1);
}
//直接加
if(gr[i][j+1]=='.'){
int xr=0;
if( ((A&(1<<(j+1)))!=0) ){xr=(1<<(j+1)); }
dp[i][j+1][A^xr]=min(dp[i][j+1][A^xr],dp[i][j][A]);
}
}
}
//dp[i][m-1]
for(int A=0;A<(1<<m);A++){
if(gr[i+1][0]=='#'){
if(((A&(1<<0))!=0)){
dp[i+1][0][A^(1)]=min(dp[i+1][0][A^1],dp[i][m-1][A]+1);
dp[i+1][0][A]=min(dp[i+1][0][A],dp[i][m-1][A]);
}else{
dp[i+1][0][A]=min(dp[i+1][0][A],dp[i][m-1][A]+1);
dp[i+1][0][A|(1<<0)]=min(dp[i+1][0][A|(1<<0)],dp[i][m-1][A]+1);
}
}else{
if(((A&(1<<0))==0)){
dp[i+1][0][A]=min(dp[i+1][0][A],dp[i][m-1][A]);
}else{
dp[i+1][0][A^(1<<0)]=min(dp[i+1][0][A^(1<<0)],dp[i][m-1][A]);
}
}
}
}
int ans=dp[n][m-1][0];
for(int A=1;A<(1<<m);A++){
ans=min(ans,dp[n][m-1][A]);
}
cout<<ans<<endl;
return 0;
int a;int b;int c;
while(cin>>a>>b>>c){
cout<<dp[a][b][c]<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Correct |
75 ms |
111156 KB |
Output is correct |
3 |
Correct |
26 ms |
44884 KB |
Output is correct |
4 |
Correct |
53 ms |
70468 KB |
Output is correct |
5 |
Correct |
72 ms |
118652 KB |
Output is correct |
6 |
Correct |
78 ms |
119156 KB |
Output is correct |
7 |
Correct |
92 ms |
116152 KB |
Output is correct |
8 |
Correct |
85 ms |
111244 KB |
Output is correct |
9 |
Correct |
72 ms |
111816 KB |
Output is correct |
10 |
Correct |
96 ms |
120620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1620 KB |
Output is correct |
2 |
Correct |
2 ms |
1624 KB |
Output is correct |
3 |
Correct |
2 ms |
1620 KB |
Output is correct |
4 |
Correct |
2 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Correct |
75 ms |
111156 KB |
Output is correct |
3 |
Correct |
26 ms |
44884 KB |
Output is correct |
4 |
Correct |
53 ms |
70468 KB |
Output is correct |
5 |
Correct |
72 ms |
118652 KB |
Output is correct |
6 |
Correct |
78 ms |
119156 KB |
Output is correct |
7 |
Correct |
92 ms |
116152 KB |
Output is correct |
8 |
Correct |
85 ms |
111244 KB |
Output is correct |
9 |
Correct |
72 ms |
111816 KB |
Output is correct |
10 |
Correct |
96 ms |
120620 KB |
Output is correct |
11 |
Correct |
2 ms |
1620 KB |
Output is correct |
12 |
Correct |
2 ms |
1624 KB |
Output is correct |
13 |
Correct |
2 ms |
1620 KB |
Output is correct |
14 |
Correct |
2 ms |
1620 KB |
Output is correct |
15 |
Correct |
1 ms |
1620 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
1620 KB |
Output is correct |
19 |
Correct |
10 ms |
21204 KB |
Output is correct |
20 |
Correct |
29 ms |
48244 KB |
Output is correct |
21 |
Correct |
45 ms |
72556 KB |
Output is correct |
22 |
Correct |
68 ms |
116944 KB |
Output is correct |
23 |
Correct |
71 ms |
116044 KB |
Output is correct |
24 |
Correct |
74 ms |
115404 KB |
Output is correct |
25 |
Correct |
78 ms |
118320 KB |
Output is correct |
26 |
Correct |
88 ms |
113340 KB |
Output is correct |
27 |
Correct |
89 ms |
113624 KB |
Output is correct |
28 |
Correct |
104 ms |
117508 KB |
Output is correct |