Submission #366810

#TimeUsernameProblemLanguageResultExecution timeMemory
366810ngpin04Tracks in the Snow (BOI13_tracks)C++14
2.19 / 100
561 ms47480 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 4e3 + 5;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

char a[N][N];

int n,m,ans;

bool vis[N][N];

void bfs(int i, int j)
{
    queue <pair <int, int>> q;

    q.push(mp(i, j));
    vis[i][j] = true;

    while (q.size())
    {
        pair <int, int> cur = q.front();
        q.pop();
        int x = cur.fi;
        int y = cur.se;

        for (int dir = 0; dir < 4; dir++)
        {
            int u = x + dx[dir];
            int v = y + dy[dir];
            if (a[u][v] == a[i][j] && min(u, v) > 0 && u <= n && v <= m && !vis[u][v])
            {
                vis[u][v] = true;
                q.push(mp(u, v));
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("file.inp","r",stdin);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        string s;
        cin >> s;
        for (int j = 1; j <= m; j++)
            a[i][j] = s[j - 1];
    }

    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) if (!vis[i][j] && a[i][j] == a[n][m])
    {
        bfs(i, j);
        ans++;
    }

    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
        if (a[i][j] != a[n][m])
            return cout << ans + 1, 0;

    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...