이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
const int N = 10;
const int p = 1e9 + 7;
ll dp[1001][11][1<<10];
vector<string> vec(1001);
void solve(){
int n, m;
cin >> n >> m;
int mask = 1 << m;
for(int i = 0; i < m; i++){
vec[0] += '.';
}
for(int i = 1; i <= n; i++){
cin >> vec[i];
}
for(int i = 0; i < mask; i++){
dp[0][m][i] = 0;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
for(int k = 0; k < mask; k++){
int nm = k ^ (1 << (j - 1));
if(vec[i][j - 1] == '#'){
if(k & (1 << (j - 1))){
if(j == 1){
dp[i][j][k] = min(dp[i - 1][m][k], dp[i - 1][m][nm]) + 1;
if(vec[i - 1][j - 1] == '#'){
dp[i][j][k] = min(dp[i][j][k], dp[i - 1][m][k]);
}
}else{
dp[i][j][k] = min(dp[i][j - 1][k], dp[i][j - 1][nm]) + 1;
if(vec[i - 1][j - 1] == '#'){
dp[i][j][k] = min(dp[i][j][k], dp[i][j - 1][k]);
}
}
}else{
if(j == 1){
dp[i][j][k] = min(dp[i - 1][m][k], dp[i - 1][m][nm]) + 1;
}else{
dp[i][j][k] = min(dp[i][j - 1][k], dp[i][j - 1][nm]) + 1;
if(vec[i][j - 2] == '#' && (k & (1 << (j - 2))) == 0){
dp[i][j][k] -= 1;
}
}
}
}else{
if(j == 1){
dp[i][j][k] = min(dp[i - 1][m][k], dp[i - 1][m][nm]);
}else{
dp[i][j][k] = min(dp[i][j - 1][k], dp[i][j - 1][nm]);
}
}
}
}
}
ll res = n*m;
for(int i = 0; i < mask; i++){
res = min(res, dp[n][m][i]);
}
cout << res << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while(t--){
solve();
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |