結果

問題 No.179 塗り分け
ユーザー hamrayhamray
提出日時 2020-02-11 14:16:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 129 ms / 3,000 ms
コード長 10,594 bytes
コンパイル時間 1,460 ms
コンパイル使用メモリ 173,008 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-23 15:04:58
合計ジャッジ時間 3,873 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 4 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 19 ms
6,944 KB
testcase_11 AC 16 ms
6,944 KB
testcase_12 AC 72 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 39 ms
6,940 KB
testcase_19 AC 36 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 81 ms
6,944 KB
testcase_23 AC 26 ms
6,940 KB
testcase_24 AC 78 ms
6,940 KB
testcase_25 AC 38 ms
6,944 KB
testcase_26 AC 38 ms
6,940 KB
testcase_27 AC 103 ms
6,940 KB
testcase_28 AC 58 ms
6,940 KB
testcase_29 AC 119 ms
6,944 KB
testcase_30 AC 29 ms
6,940 KB
testcase_31 AC 129 ms
6,940 KB
testcase_32 AC 37 ms
6,944 KB
testcase_33 AC 116 ms
6,940 KB
testcase_34 AC 46 ms
6,944 KB
testcase_35 AC 122 ms
6,940 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 1 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 1 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 4 ms
6,944 KB
testcase_44 AC 16 ms
6,944 KB
testcase_45 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//typedef
//-------------------------#include <bits/stdc++.h>
 
#define M_PI       3.14159265358979323846
 
using namespace std;
 
//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
inline int readInt() { int x; scanf("%d", &x); return x; }
 
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
 
 
//container util
 
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a)*(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
 
//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007
 
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
 
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-8;
 
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
 
class UnionFind {
public:
    vector <ll> par; 
    vector <ll> siz; 
    
    vector <ll> maxv;
    UnionFind(ll sz_): par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
 
    ll root(ll x) { 
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
 
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }
 
    bool issame(ll x, ll y) { 
        return root(x) == root(y);
    }
 
    ll size(ll x) { 
        return siz[root(x)];
    }
};
 
 
ll mod_pow(ll x, ll n, ll mod){
    ll res = 1;
    while(n){
        if(n&1) res = (res * x)%mod;
 
        res %= mod;
        x = x * x %mod;
        n >>= 1;
    }
    return res;
}
 
#define SIEVE_SIZE 5000000+10
bool sieve[SIEVE_SIZE];
void make_sieve(){
    for(int i=0; i<SIEVE_SIZE; ++i) sieve[i] = true;
    sieve[0] = sieve[1] = false;
    for(int i=2; i*i<SIEVE_SIZE; ++i) if(sieve[i]) for(int j=2; i*j<SIEVE_SIZE; ++j) sieve[i*j] = false;
}
 
bool isprime(ll n){
    if(n == 0 || n == 1) return false;
    for(ll i=2; i*i<=n; ++i) if(n%i==0) return false;
    return true;
}
 
const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];
 
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
 
// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
 
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return d;
}
// 負の数にも対応した mod (a = -11 とかでも OK) 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
 
// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m) {
    long long x, y;
    extGCD(a, m, x, y);
    return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}
ll GCD(ll a, ll b){
    
    if(b == 0) return a;
    return GCD(b, a%b);
}
 
 
 
struct LazySegmentTree {
private:
    int n;
    vector<ll> node, lazy;
 
public:
    LazySegmentTree(vector<ll> v) {
        int sz = (int)v.size();
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1);
        lazy.resize(2*n-1, 0);
 
        for(int i=0; i<sz; i++) node[i+n-1] = v[i];
        for(int i=n-2; i>=0; i--) node[i] = node[i*2+1] + node[i*2+2];
    }
 
    void eval(int k, int l, int r) {
        if(lazy[k] != 0) {
            node[k] += lazy[k];
            if(r - l > 1) {
                lazy[2*k+1] += lazy[k] / 2;
                lazy[2*k+2] += lazy[k] / 2;
            }
            lazy[k] = 0;
        }
    }
 
    void add(int a, int b, ll x, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            lazy[k] += (r - l) * x;
            eval(k, l, r);
        }
        else {
            add(a, b, x, 2*k+1, l, (l+r)/2);
            add(a, b, x, 2*k+2, (l+r)/2, r);
            node[k] = node[2*k+1] + node[2*k+2];
        }
    }
 
    ll getsum(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a) return 0;
        if(a <= l && r <= b) return node[k];
        ll vl = getsum(a, b, 2*k+1, l, (l+r)/2);
        ll vr = getsum(a, b, 2*k+2, (l+r)/2, r);
        return vl + vr;
    }
};

int const INF = INT_MAX;

struct SegmentTree {
private:
    int n;
    vector<int> node;

public:
    SegmentTree(vector<int> v) {
        int sz = v.size();
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, INF);
        for(int i=0; i<sz; i++) node[i+n-1] = v[i];
        for(int i=n-2; i>=0; i--) node[i] = min(node[2*i+1], node[2*i+2]);
    }

    void update(int x, int val) {
        x += (n - 1);
        node[x] = val;
        while(x > 0) {
            x = (x - 1) / 2;
            node[x] = min(node[2*x+1], node[2*x+2]);
        }
    }

    int getmin(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        if(r <= a || b <= l) return INF;
        if(a <= l && r <= b) return node[k];

        int vl = getmin(a, b, 2*k+1, l, (l+r)/2);
        int vr = getmin(a, b, 2*k+2, (l+r)/2, r);
        return min(vl, vr);
    }
};
using Weight = int;
using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;
 
void add_edge(Graph &g, int a, int b, Weight w = 1) {
    g[a].emplace_back(a, b, w);
    g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); }

// template<typename T> class basic_stopwatch: T {
//     typedef typename T BaseTimer;

// public:
//     explicit basic_stopwatch(bool start);
//     explicit basic_stopwatch(char const* activity = "Stopwatch", bool start = true);
//     basic_stopwatch(std::ostream& log, char const* activity = "Stopwatch", bool start = true);
//     basic_stopwatch();
//     unsigned LapGet() const;
//     bool IsStarted() const;
//     unsigned Show(char const* event = "show");
//     unsigned Start(char const* event_name = "start");
//     unsigned Stop(char const* event_name = "stop");
// private:
//     char const*     m_activity;
//     unsigned        m_lap;
//     std::ostream&   m_log;
// };

// #include <chrono>
// using namespace std::chrono;
// class TimerBase {
// public: 

//     // タイマをクリアする
//     TimerBase(): m_start(system_clock::time_point::min()) {}

//     void Clear(){
//         m_start = system_clock::time_point::min();
//     }

//     bool IsStarted() const {
//         return (m_start.time_since_epoch() != system_clock::duration(0));
//     }

//     void Start() {
//         m_start = system_clock::now();
//     }

//     unsigned long GetMs(){
//         if(IsStarted()) {
//             system_clock::duration diff;
//             diff = system_clock::now() - m_start;
//             return (unsigned)(duration_cast<milliseconds>(diff).const();
//         }
//         return 0;
//     }
// private:
//     system_clock::time_point m_start;
// };

int H, W; 
vector<string> f;
int dxy[5] = {-1, 0, 1, 0, -1};
char c[55][55];
void dfs(int ry, int rx, int by, int bx){
        c[ry][rx] = '.';
        c[by][bx] = '.';
        for(int i=-1; i<=1; i++){
            for(int j=-1; j<=1; j++){
            int rry = ry+i, rrx = rx+j, bby = by+i, bbx = bx+j;

            if(rry>=0 && rry < H && rrx>=0 && rrx < W && bby >= 0 && bby<H && bbx>=0 && bbx<W){
                if(c[rry][rrx] == '#' && c[bby][bbx] == '#'){
                    dfs(rry,rrx,bby,bbx);
                }
            }
            }
        }
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    
    cin >> H >> W;
    int num = 0;
    REP(i,H){
        string s; cin >> s;
        f.push_back(s);

        for(int j=0; j<s.size(); j++){
            if(s[j] == '#') num++;
        }
    }
    if(num%2 == 1 || num == 0){
        cout << "NO" << endl;
        return 0;
    }

    for(int i=-H; i<=H; i++){
        for(int j=-W; j<=W; j++){
            for(int k=0; k<H; k++){
                for(int l=0; l<W; l++){
                    c[k][l] = f[k][l];
                }
            }
            bool ok = true;
            for(int k=0; k<H; k++){
                for(int l=0; l<W; l++){
                    if(!(k==k+i && l==l+j) && k+i>=0 && k+i<H && l+j>=0 && l+j<W && c[k][l] == '#' && c[k+i][l+j] == '#'){
                        c[k][l] =  c[k+i][l+j] = '.';
                    }
                    
                }
                if(!ok) break;
            }
            
            for(int k=0; k<H; k++){
                for(int l=0; l<W; l++){
                    if(c[k][l] =='#') ok = false;
                }
            }
            if(ok){
                //cout << sy << " " << sx << " " << i << " " << j << endl;
                cout << "YES" << endl;
                return 0;
            }
        }
    }

    cout << "NO" << endl;

    return 0;
}
0