結果

問題 No.348 カゴメカゴメ
ユーザー parukiparuki
提出日時 2016-06-17 18:20:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 3,138 bytes
コンパイル時間 2,115 ms
コンパイル使用メモリ 179,736 KB
実行使用メモリ 25,404 KB
最終ジャッジ日時 2024-04-19 12:55:39
合計ジャッジ時間 4,470 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
17,920 KB
testcase_01 AC 4 ms
12,800 KB
testcase_02 AC 81 ms
25,404 KB
testcase_03 AC 43 ms
20,608 KB
testcase_04 AC 4 ms
12,544 KB
testcase_05 AC 7 ms
16,512 KB
testcase_06 AC 4 ms
12,672 KB
testcase_07 AC 4 ms
12,672 KB
testcase_08 AC 5 ms
12,672 KB
testcase_09 AC 4 ms
12,672 KB
testcase_10 AC 4 ms
12,672 KB
testcase_11 AC 4 ms
12,544 KB
testcase_12 AC 6 ms
13,184 KB
testcase_13 AC 5 ms
12,800 KB
testcase_14 AC 5 ms
12,928 KB
testcase_15 AC 48 ms
16,512 KB
testcase_16 AC 4 ms
12,672 KB
testcase_17 AC 5 ms
12,544 KB
testcase_18 AC 4 ms
12,672 KB
testcase_19 AC 4 ms
12,672 KB
testcase_20 AC 5 ms
12,800 KB
testcase_21 AC 4 ms
12,672 KB
testcase_22 AC 3 ms
12,544 KB
testcase_23 AC 54 ms
18,596 KB
testcase_24 AC 56 ms
18,284 KB
testcase_25 AC 56 ms
18,488 KB
testcase_26 AC 54 ms
18,480 KB
testcase_27 AC 52 ms
18,388 KB
testcase_28 AC 53 ms
18,492 KB
testcase_29 AC 55 ms
18,448 KB
testcase_30 AC 55 ms
18,348 KB
testcase_31 AC 54 ms
18,488 KB
testcase_32 AC 56 ms
18,380 KB
testcase_33 AC 9 ms
13,696 KB
testcase_34 AC 9 ms
13,568 KB
testcase_35 AC 8 ms
13,568 KB
testcase_36 AC 8 ms
13,568 KB
testcase_37 AC 9 ms
13,696 KB
testcase_38 AC 8 ms
13,604 KB
testcase_39 AC 9 ms
13,616 KB
testcase_40 AC 8 ms
13,612 KB
testcase_41 AC 8 ms
13,568 KB
testcase_42 AC 8 ms
13,568 KB
testcase_43 AC 4 ms
13,184 KB
testcase_44 AC 4 ms
13,056 KB
testcase_45 AC 4 ms
13,184 KB
testcase_46 AC 5 ms
13,184 KB
testcase_47 AC 4 ms
13,184 KB
testcase_48 AC 5 ms
13,184 KB
testcase_49 AC 4 ms
13,184 KB
testcase_50 AC 4 ms
13,184 KB
testcase_51 AC 5 ms
13,184 KB
testcase_52 AC 5 ms
13,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

const int DY[] = {-1,0,1,0};
const int DX[] = {0,1,0,-1};
const int dy[] = { 0, 1, -1, 0, 1, 1, -1, -1 };
const int dx[] = { 1, 0, 0, -1, 1, -1, -1, 1 };

struct R{
    vector<pii> ps;
    vi ch;
};

const int MAX = 1010;
int H, W, vis[MAX][MAX];
char g[MAX][MAX];
vector<R> rn;

void mkR(int sy, int sx){
    R r;
    queue<pair<int, int>> Q;
    Q.push(mp(sy, sx));
    while(sz(Q)){
        int y, x;
        tie(y, x) = Q.front();
        Q.pop();
        if(vis[y][x]++)continue;
        r.ps.emplace_back(y, x);
        rep(i, 8){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny<0||nx<0||ny>=H||nx>=W||vis[ny][nx]||g[ny][nx]=='.')continue;
            Q.push(mp(ny, nx));
        }
    }
    rn.push_back(r);
}

vi bfs(int sy, int sx){
    vi res;
    queue<pair<int, int>> Q;
    Q.push(mp(sy, sx));
    while(sz(Q)){
        int y, x;
        tie(y, x) = Q.front();
        Q.pop();
        if(vis[y][x]++)continue;
        if(g[y][x] == 'x'){
            res.push_back(sz(rn));
            vis[y][x] = 0;
            mkR(y, x);
            continue;
        }
        rep(i, 4){
            int ny = y + DY[i];
            int nx = x + DX[i];
            if(ny<0||nx<0||ny>=H||nx>=W||vis[ny][nx])continue;
            Q.push(mp(ny, nx));
        }
    }
    return res;
}

void init(){
    cin >> H >> W;
    MEM(g, '.');
    rep(y, H){
        scanf("%s", g[y+1] + 1);
        g[y+1][W + 1] = '.';
    }
    H += 2;
    W += 2;
    rn.push_back(R());
    for(int p = 0; p < sz(rn); ++p){
        int sy = -1, sx = -1;
        if(p == 0){
            sy = sx = 0;
        } else{
            each(q, rn[p].ps){
                int y, x;
                tie(y, x) = q;
                rep(i, 4){
                    int ny = y + DY[i];
                    int nx = x + DX[i];
                    if(ny < 0 || nx < 0 || ny >= H || nx >= W)continue;
                    if(g[ny][nx] == '.' && !vis[ny][nx]){
                        sy = ny;
                        sx = nx;
                        break;
                    }
                }
                if(sy != -1)break;
            }
        }
        if(sy != -1)rn[p].ch = bfs(sy, sx);
    }
}

int mem[MAX*MAX][2];
int solve(int id, int use){
    int &res = mem[id][use];
    if(res != -1)return res;
    res = use*sz(rn[id].ps);
    each(ch, rn[id].ch){
        if(use)res += solve(ch, 0);
        else res += max(solve(ch, 0), solve(ch, 1));
    }
    return res;
}

int main(){
    init();
    int ans = 0;
    MEM(mem, -1);
    each(id, rn[0].ch)
        ans += max(solve(id, 0), solve(id, 1));
    cout << ans << endl;
}
0