Submission #229474

# Submission time Handle Problem Language Result Execution time Memory
229474 2020-05-04T15:11:30 Z grt Zoo (COCI19_zoo) C++17
110 / 110
71 ms 7296 KB
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
#define _ ios_base::sync_with_stdio(0); cin.tie(0);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

const int nax = 1010;
int n,m;
char t[nax][nax];
bool visited[nax][nax];
int dist[nax][nax];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
queue<pi>Q1,Q2;

bool valid(int &x,int &y) {
	return 0<=x&&x<n&&0<=y&&y<m;
}

void bfs() {
	Q1.push({0,0});
	visited[0][0] = 1;
	while(!Q1.empty()) {
		while(!Q1.empty()) {
			pi coo = Q1.front();
			Q1.pop();
			//if(visited[coo.ST][coo.ND]) continue;
			int d = dist[coo.ST][coo.ND];
			char c = t[coo.ST][coo.ND];
			for(int i = 0; i < 4; ++i) {
				coo.ST+=dx[i]; coo.ND+=dy[i];
				if(valid(coo.ST,coo.ND)&&t[coo.ST][coo.ND] != '*'&& !visited[coo.ST][coo.ND]) {
					if(c == t[coo.ST][coo.ND]) {
						dist[coo.ST][coo.ND] = d;
						visited[coo.ST][coo.ND] = 1;
						Q1.push(coo);
					} else {
						dist[coo.ST][coo.ND] = d+1;
						visited[coo.ST][coo.ND] = 1;
						Q2.push(coo);
					}
				}
				coo.ST-=dx[i]; coo.ND-=dy[i];
			}
		}
		Q1.swap(Q2);
	}
}

int main() {_
	cin >> n >> m;
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j<m; ++j) {
			cin>>t[i][j];
		}
	}
	bfs();
	int ans = 0;
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < m; ++j) {
			ans = max(ans,dist[i][j]);
		}
	}
	cout << ans + 1;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 6 ms 896 KB Output is correct
6 Correct 5 ms 896 KB Output is correct
7 Correct 5 ms 896 KB Output is correct
8 Correct 5 ms 896 KB Output is correct
9 Correct 5 ms 896 KB Output is correct
10 Correct 5 ms 1024 KB Output is correct
11 Correct 5 ms 896 KB Output is correct
12 Correct 5 ms 896 KB Output is correct
13 Correct 5 ms 768 KB Output is correct
14 Correct 6 ms 1024 KB Output is correct
15 Correct 6 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 6 ms 896 KB Output is correct
6 Correct 5 ms 896 KB Output is correct
7 Correct 5 ms 896 KB Output is correct
8 Correct 5 ms 896 KB Output is correct
9 Correct 5 ms 896 KB Output is correct
10 Correct 5 ms 1024 KB Output is correct
11 Correct 5 ms 896 KB Output is correct
12 Correct 5 ms 896 KB Output is correct
13 Correct 5 ms 768 KB Output is correct
14 Correct 6 ms 1024 KB Output is correct
15 Correct 6 ms 768 KB Output is correct
16 Correct 25 ms 7168 KB Output is correct
17 Correct 30 ms 7200 KB Output is correct
18 Correct 25 ms 7296 KB Output is correct
19 Correct 27 ms 7168 KB Output is correct
20 Correct 27 ms 7040 KB Output is correct
21 Correct 62 ms 6912 KB Output is correct
22 Correct 69 ms 7012 KB Output is correct
23 Correct 63 ms 7040 KB Output is correct
24 Correct 69 ms 7296 KB Output is correct
25 Correct 64 ms 7168 KB Output is correct
26 Correct 65 ms 7032 KB Output is correct
27 Correct 66 ms 6912 KB Output is correct
28 Correct 71 ms 6912 KB Output is correct
29 Correct 64 ms 7296 KB Output is correct
30 Correct 65 ms 7160 KB Output is correct