답안 #379094

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379094 2021-03-17T09:51:48 Z topovik Selotejp (COCI20_selotejp) C++14
35 / 110
365 ms 748 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
#define M ll(1e9 + 7)
#define sz(x) (ll)x.size()
#define re return
#define oo ll(1e18)
#define el '\n'
#define pii pair <ll, ll>
#define all(x) (x).begin(), (x).end()
#define arr_all(x, n) (x + 1), (x + 1 + n)
#define vi vector<ll>
#define L 0
#define R 1
#define D 2
#define U 3
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <ll, null_type, less_equal <ll> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int N = 1e3 + 500;

int dp[N][N];
int n, m;
string s[N];
bool mrk[N][N];
int ans;

int state(int x, int y)
{
    if (x < 0 || y < 0 || x == n || y == m) return 0;
    return s[x][y] == '#';
}

int colvo(int x, int y)
{
    return state(x - 1, y) + state(x + 1, y) + state(x, y - 1) + state(x, y + 1);
}

int not_vert(int x, int y)
{
    if (state(x, y - 1) && state(x, y + 1)) return 0;
    if (state(x - 1, y) && state(x + 1, y)) return 0;
    return 1;
}

int Calc(int *a)
{
    int sum = 0;
    if (a[0] == 1) sum++;
    for (int i = 1; i < m; i++)
        if (a[i] == 1) sum += a[i] != a[i - 1];
    return sum;
}

void Rec(int x, int y, int _x, int _y)
{
    if (_x == -1)
    {
        ans++;
        _x = 1;
        _y = 0;
        if (state(x + 1, y)) _x = 1, _y = 0;
        if (state(x - 1, y)) _x = -1, _y = 0;
        if (state(x, y + 1)) _y = 1, _x = 0;
        if (state(x, y - 1)) _y = -1, _x = 0;
    }
    s[x][y] = '.';
    if (state(x + _x, y + _y)) Rec(x + _x, y + _y, _x, _y);
    else
    {
       if (state(x + 1, y)) Rec(x + 1, y, -1, -1);
       if (state(x - 1, y)) Rec(x - 1, y, -1, -1);
       if (state(x, y + 1)) Rec(x, y + 1, -1, -1);
       if (state(x, y - 1)) Rec(x, y - 1, -1, -1);
    }
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> s[i];
    if (n > 10)
    {
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
              if (state(i, j) && colvo(i, j) == 1) Rec(i, j, -1, -1);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
              if (state(i, j)) Rec(i, j, -1, -1);
        cout << ans;
    }
    else
    {
        for (int i = 0; i < (1 << m); i++)
        {
            int c[m];
            for (int j = 0; j < m; j++) c[j] = (i >> j) & 1;
            for (int j = 0; j < m; j++)
                if (s[0][j] == '.') c[j] = 2;
            dp[0][i] = Calc(c);
            for (int j = 0; j < m; j++) dp[0][i] += (c[j] == 0);
        }
        for (int i = 1; i < n; i++)
            for (int msk = 0; msk < (1 << m); msk++)
            {
                int c[m];
                for (int j = 0; j < m; j++) c[j] = (msk >> j) & 1;
                for (int j = 0; j < m; j++)
                    if (s[i][j] == '.') c[j] = 2;
                dp[i][msk] = Calc(c);
                int mx = M;
                for (int msk1 = 0; msk1 < (1 << m); msk1++)
                {
                    int c1[m];
                    for (int j = 0; j < m; j++) c1[j] = (msk1 >> j) & 1;
                    for (int j = 0; j < m; j++)
                        if (s[i - 1][j] == '.') c1[j] = 2;
                    int val = dp[i - 1][msk1];
                    for (int j = 0; j < m; j++) val += (c[j] == 0 && c1[j] != c[j]);
                    mx = min(mx, val);
                }
                dp[i][msk] += mx;
            }
        ans = M;
        for (int msk = 0; msk < (1 << m); msk++) ans = min(ans, dp[n - 1][msk]);
        cout << ans;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 305 ms 488 KB Output is correct
2 Correct 308 ms 492 KB Output is correct
3 Correct 311 ms 492 KB Output is correct
4 Correct 321 ms 492 KB Output is correct
5 Correct 347 ms 492 KB Output is correct
6 Correct 267 ms 388 KB Output is correct
7 Correct 15 ms 512 KB Output is correct
8 Correct 365 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -