Submission #229474

#TimeUsernameProblemLanguageResultExecution timeMemory
229474grtZoo (COCI19_zoo)C++17
110 / 110
71 ms7296 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...