結果

問題 No.348 カゴメカゴメ
ユーザー mamekinmamekin
提出日時 2016-04-02 13:33:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,954 bytes
コンパイル時間 1,203 ms
コンパイル使用メモリ 116,044 KB
実行使用メモリ 11,920 KB
最終ジャッジ日時 2023-08-01 07:39:50
合計ジャッジ時間 5,126 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
7,684 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 92 ms
11,920 KB
testcase_03 AC 84 ms
9,300 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 66 ms
4,596 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,508 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 3 ms
4,376 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 WA -
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

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

pair<int, int> solve2(int sy, int sx);

int h, w;
vector<string> s;
vector<vector<bool> > check;

pair<int, int> solve1(int sy, int sx)
{
    queue<pair<int, int> > q, select;
    q.push(make_pair(sy, sx));
    check[sy][sx] = true;

    while(!q.empty()){
        int y = q.front().first;
        int x = q.front().second;
        q.pop();

        for(int i=0; i<4; ++i){
            int y2 = y + dy[i];
            int x2 = x + dx[i];
            if(!(0 <= y2 && y2 < h && 0 <= x2 && x2 < w) || check[y2][x2])
                continue;
            
            if(s[y2][x2] == '.'){
                check[y2][x2] = true;
                q.push(make_pair(y2, x2));
            }
            else{
                select.push(make_pair(y2, x2));
            }
        }
    }

    pair<int, int> ans(0, 0);
    while(!select.empty()){
        int y = select.front().first;
        int x = select.front().second;
        select.pop();

        if(!check[y][x]){
            pair<int, int> p = solve2(y, x);
            ans.first += p.first;
            ans.second += p.second;
        }
    }

    return ans;
}

pair<int, int> solve2(int sy, int sx)
{
    queue<pair<int, int> > q;
    q.push(make_pair(sy, sx));
    check[sy][sx] = true;
    int cnt = 1;

    while(!q.empty()){
        int y = q.front().first;
        int x = q.front().second;
        q.pop();

        for(int i=0; i<8; ++i){
            int y2 = y + dy[i];
            int x2 = x + dx[i];
            if(check[y2][x2])
                continue;

            if(s[y2][x2] == 'x'){
                check[y2][x2] = true;
                q.push(make_pair(y2, x2));
                ++ cnt;
            }
        }
    }

    for(int i=0; i<4; ++i){
        int y2 = sy + dy[i];
        int x2 = sx + dx[i];
        if(check[y2][x2])
            continue;

        if(s[y2][x2] == '.'){
            pair<int, int> p = solve1(y2, x2);
            swap(p.first, p.second);
            p.second = max(p.second, p.first);
            p.first += cnt;
            return p;
        }
    }

    return make_pair(cnt, 0);
}

int main()
{
    cin >> h >> w;
    h += 2;
    w += 2;
    s.assign(h, string(w, '.'));
    for(int y=1; y<h-1; ++y){
        for(int x=1; x<w-1; ++x){
            cin >> s[y][x];
        }
    }

    check.assign(h, vector<bool>(w, false));
    pair<int, int> ans = solve1(0, 0);
    cout << max(ans.first, ans.second) << endl;

    return 0;
}
0