This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MX = 1005;
const int MXM = (1 << 10) + 5;
const int inf = 1e9 + 69;
int n, m;
string G[MX];
int dp[2][MXM]; 
bool bit(int msk, int tp){
	return ((msk >> tp) & 1);
}
int main(){
	cin.tie(0) -> sync_with_stdio(0);
	cin >> n >> m;
	for(int i = 0; i < n; i ++)
		cin >> G[i];
	int cr = 0;
	for(int i = 1; i < MXM; i ++)
		dp[cr][i] = inf;
	dp[cr][0] = 0;
	for(int i = 0; i < n; i ++){
		for(int j = 0; j < m; j ++){
			for(int msk = 0; msk < (1 << m); msk ++){
				if(bit(msk, j) == 0){ // horizontal
					dp[cr ^ 1][msk] = min(dp[cr][msk], dp[cr][msk + (1 << j)]); //horizontal means above doesnt matter
					if(G[i][j] == '#' && (j == 0 || G[i][j - 1] == '.' || bit(msk, j - 1))) // if last square doesnt exist, new tape
						dp[cr ^ 1][msk] ++;
				}else{ // vertical
					if(G[i][j] == '.') dp[cr ^ 1][msk] = inf;
					else
						dp[cr ^ 1][msk] = min(dp[cr][msk], dp[cr][msk - (1 << j)] + 1); //compare with above current square is horizontal, new tape
				}
			}
			cr ^= 1;
		}
	}
	int ans = inf;
	for(int i = 0; i < (1 << m); i ++)
		ans = min(ans, dp[cr][i]);
	cout << ans << endl;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |