Submission #52883999


Source Code Expand

// #pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf (ll)1e18
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define fi first
#define se second
#define lll __int128
const int N = 1005, M = 1e6 + 5;
char g[N][N];
ll ds[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
ll d[N][N], h, w;
bool vis[N][N];

ll fa[M];
ll find(ll x){
    if(fa[x] == x) return x;
    return fa[x] = find(fa[x]); //路径压缩
}
void merge(ll x, ll y){
    fa[find(x)] = find(y);
}
bool check(ll x, ll y) {
    for (ll k = 0; k < 4; k++) {
        ll nx = x + ds[k][0], ny = y + ds[k][1];
        if (nx < 1 || nx > h || ny < 0 || ny > w) {
            continue;
        }
        if (g[nx][ny] == '#')
            return true;
    }
    return false;
}
ll id(ll x, ll y) {
    return (x - 1) * w + y;
}
void solve() {
    cin >> h >> w;
    for (ll i = 1; i <= h; i++)
        for (ll j = 1; j <= w; j++)
            cin >> g[i][j];
    for (ll i = 1; i <= h; i++)
        for (ll j = 1; j <= w; j++)
            fa[id(i, j)] = id(i, j);
    ll ans = 0;
    for (ll i = 1; i <= h; i++) {
        for (ll j = 1; j <= w; j++) {
            if (g[i][j] == '#')
                continue;
            else
            	ans = 1;
            for (ll k = 0; k < 4; k++) {
                ll ni = i + ds[k][0], nj = j + ds[k][1];
                if (ni < 1 || ni > h || nj < 1 || nj > w || g[ni][nj] == '#')
                    continue;
                if (!check(i, j) && !check(ni, nj)) {
//                    cout << i << " " << j << " " << ni << " " << nj << endl;
                    merge(id(i, j), id(ni, nj));
                }
            }
        }
    }
    map<ll, ll> mp;
    for (ll i = 1; i <= h; i++) {
        for (ll j = 1; j <= w; j++) {
            if (g[i][j] != '#' && !check(i, j)) {
                ll r = id(i, j);
                ll rr = find(r);
                mp[find(id(i, j))]++;
            }
        }
    }
    for (ll i = 1; i <= h; i++) {
        for (ll j = 1; j <= w; j++) {
            if (g[i][j] == '#' || !check(i, j))
                continue;
            set<ll> st;
            for (ll k = 0; k < 4; k++) {
                ll ni = i + ds[k][0], nj = j + ds[k][1];
                if (ni < 1 || ni > h || nj < 1 || nj > w || g[ni][nj] == '#')
                    continue;
                if (!check(ni, nj)) {
                    ll r = find(id(ni, nj));
//                    cout << r << " " << i << " " << j << endl;
                    if (!st.count(r)) {
                        mp[r]++;
                        st.insert(r);
                    }

                }
            }
        }
    }
    for (auto [x, y] : mp)
        ans = max(ans, y);
    cout << ans << endl;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

Submission Info

Submission Time
Task D - Grid and Magnet
User TopCloser
Language C++ 20 (gcc 12.2)
Score 425
Code Size 3005 Byte
Status AC
Exec Time 216 ms
Memory 27744 KiB

Compile Error

Main.cpp: In function ‘void solve()’:
Main.cpp:71:20: warning: unused variable ‘rr’ [-Wunused-variable]
   71 |                 ll rr = find(r);
      |                    ^~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status
AC × 2
AC × 52
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3516 KiB
example_01.txt AC 1 ms 3372 KiB
hand_00.txt AC 1 ms 3452 KiB
hand_01.txt AC 104 ms 12352 KiB
hand_02.txt AC 13 ms 12232 KiB
hand_03.txt AC 20 ms 12356 KiB
hand_04.txt AC 213 ms 19296 KiB
hand_05.txt AC 103 ms 12364 KiB
hand_06.txt AC 107 ms 12308 KiB
hand_07.txt AC 99 ms 12308 KiB
hand_08.txt AC 95 ms 12164 KiB
hand_09.txt AC 103 ms 12252 KiB
hand_10.txt AC 99 ms 12372 KiB
hand_11.txt AC 1 ms 3568 KiB
hand_12.txt AC 1 ms 3448 KiB
hand_13.txt AC 1 ms 3500 KiB
random_00.txt AC 120 ms 12132 KiB
random_01.txt AC 126 ms 12208 KiB
random_02.txt AC 126 ms 12020 KiB
random_03.txt AC 130 ms 13396 KiB
random_04.txt AC 136 ms 13396 KiB
random_05.txt AC 134 ms 13364 KiB
random_06.txt AC 153 ms 15988 KiB
random_07.txt AC 153 ms 16108 KiB
random_08.txt AC 154 ms 16036 KiB
random_09.txt AC 75 ms 13600 KiB
random_10.txt AC 78 ms 13760 KiB
random_11.txt AC 75 ms 13624 KiB
random_12.txt AC 14 ms 12244 KiB
random_13.txt AC 14 ms 12348 KiB
random_14.txt AC 14 ms 12200 KiB
random_15.txt AC 118 ms 12200 KiB
random_16.txt AC 127 ms 12216 KiB
random_17.txt AC 134 ms 12116 KiB
random_18.txt AC 13 ms 12192 KiB
random_19.txt AC 13 ms 12184 KiB
random_20.txt AC 13 ms 12272 KiB
random_21.txt AC 20 ms 12312 KiB
random_22.txt AC 20 ms 12076 KiB
random_23.txt AC 21 ms 12432 KiB
random_24.txt AC 212 ms 27600 KiB
random_25.txt AC 212 ms 27652 KiB
random_26.txt AC 209 ms 27280 KiB
random_27.txt AC 214 ms 27684 KiB
random_28.txt AC 216 ms 27744 KiB
random_29.txt AC 211 ms 27460 KiB
random_30.txt AC 207 ms 19064 KiB
random_31.txt AC 184 ms 19044 KiB
random_32.txt AC 154 ms 18824 KiB
random_33.txt AC 214 ms 19112 KiB
random_34.txt AC 189 ms 18948 KiB
random_35.txt AC 163 ms 18872 KiB