#include <bits/stdc++.h>
using namespace std;
#define btninh signed main
#define int long long
#define REP(i, n) for(int i = 0; i < n; ++i)
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)
#define fast ios::sync_with_stdio(0); cin.tie(0)
#define IO(x) if(fopen(x".inp","r")){freopen(x".inp","r",stdin);freopen(x".out","w",stdout);}
#define vi vector<int>
#define vii vector<vector<int>>
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
template<class Typ> bool mini(Typ &x, Typ y){if (x > y) return x = y, true; return false;}
template<class Typ> bool maxi(Typ &x, Typ y){if (x < y) return x = y, true; return false;}
const int N = 1005;
const int inf = 1e18;
int n, m, dp[2][1 << 11][2];
char a[N][15];
int getBit(int m, int i){
return ((m >> i) & 1);
}
btninh(){
fast;
IO("main");
cin >> n >> m;
FOR(i, 1, n) FOR(j, 1, m) cin >> a[i][j];
memset(dp, 0x3f3f3f, sizeof dp);
dp[0][0][0] = 0;
int cur = 1, lst = 0;
FOR(i, 1, n){
FOR(j, 1, m){
FOR(msk, 0, (1 << m) - 1){
FOR(l, 0, 1){
if (a[i][j] == '.'){
// Không cần dán nếu là cửa sổ mở
mini(dp[cur][msk & ~(1LL << (j - 1))][0], dp[lst][msk][l]);
} else {
bool fromLeft = l; // có đang dán ngang từ bên trái không
bool fromTop = getBit(msk, j - 1); // có đang dán dọc từ trên xuống không
if (fromLeft || fromTop){
// Kế thừa dán cũ
mini(dp[cur][msk | (1LL << (j - 1))][fromLeft], dp[lst][msk][l]); // dán dọc
mini(dp[cur][msk][fromLeft & (j != m)], dp[lst][msk][l]); // dán ngang
// Bắt đầu mới
mini(dp[cur][msk | (1LL << (j - 1))][l], dp[lst][msk][l] + 1); // dán mới dọc
mini(dp[cur][msk][j != m], dp[lst][msk][l] + 1); // dán mới ngang
}
else {
// Chưa được dán, bắt buộc dán mới
mini(dp[cur][msk | (1LL << (j - 1))][l], dp[lst][msk][l] + 1); // dọc
mini(dp[cur][msk][j != m], dp[lst][msk][l] + 1); // ngang
}
}
}
}
// Reset dp[lst]
FOR(msk, 0, (1 << m) - 1){
dp[lst][msk][0] = dp[lst][msk][1] = inf;
}
swap(cur, lst);
}
}
int ans = 0x3f3f3f;
FOR(msk, 0, (1 << m) - 1){
ans = min(ans, min(dp[lst][msk][0], dp[lst][msk][1]));
}
cout << ans;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:10:45: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | #define IO(x) if(fopen(x".inp","r")){freopen(x".inp","r",stdin);freopen(x".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:31:5: note: in expansion of macro 'IO'
31 | IO("main");
| ^~
Main.cpp:10:72: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | #define IO(x) if(fopen(x".inp","r")){freopen(x".inp","r",stdin);freopen(x".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:31:5: note: in expansion of macro 'IO'
31 | IO("main");
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |