Submission #345050

# Submission time Handle Problem Language Result Execution time Memory
345050 2021-01-07T01:00:12 Z limabeans Zoo (COCI19_zoo) C++17
110 / 110
66 ms 4716 KB
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl








const int maxn = 1010;


int n, m;
string g[maxn];


bool viz[maxn][maxn];
int cc = 0;

vector<pair<int,int>> moves = { {-1,0}, {1,0}, {0,-1}, {0,1} };


vector<pair<int,int>> qq;

void dfs(int x, int y, char c) {
    if (x<0||y<0||x>=n||y>=m) return;
    if (viz[x][y]) return;
    if (g[x][y]==c) {
	viz[x][y]=true;
	qq.push_back({x,y});
	dfs(x-1,y,c);
	dfs(x+1,y,c);
	dfs(x,y-1,c);
	dfs(x,y+1,c);
    }
}


char other(char c) {
    if (c=='B') return 'T';
    if (c=='T') return 'B';
    assert(false);
}


int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    cin>>n>>m;
    for (int i=0; i<n; i++) {
	cin>>g[i];
    }

    
    dfs(0,0,g[0][0]);

    while (qq.size()) {
	cc++;
	vector<pair<int,int>> cur = qq;
	qq.clear();
	char c = g[cur.back().first][cur.back().second];
	
	for (auto p: cur) {
	    int x=p.first; int y=p.second;
	    for (auto mo: moves) {
		int nx = mo.first+x;
		int ny = mo.second+y;
		if (nx>=0 && nx<n && ny>=0 && ny<m && !viz[nx][ny] && g[nx][ny]==other(c)) {
		    dfs(nx,ny,g[nx][ny]);
		}
	    }
	}
    }


    out(cc);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 10 ms 4600 KB Output is correct
17 Correct 11 ms 4476 KB Output is correct
18 Correct 10 ms 4592 KB Output is correct
19 Correct 13 ms 4716 KB Output is correct
20 Correct 11 ms 4444 KB Output is correct
21 Correct 54 ms 3564 KB Output is correct
22 Correct 53 ms 3524 KB Output is correct
23 Correct 54 ms 3436 KB Output is correct
24 Correct 56 ms 3564 KB Output is correct
25 Correct 55 ms 3564 KB Output is correct
26 Correct 54 ms 3564 KB Output is correct
27 Correct 56 ms 3436 KB Output is correct
28 Correct 66 ms 3456 KB Output is correct
29 Correct 59 ms 3564 KB Output is correct
30 Correct 55 ms 3564 KB Output is correct