Submission #905060

#TimeUsernameProblemLanguageResultExecution timeMemory
905060pete555Tracks in the Snow (BOI13_tracks)C++17
89.06 / 100
1803 ms286688 KiB
#include<bits/stdc++.h>
using namespace std;

const int dx[]{0,1,0,-1};
const int dy[]{1,0,-1,0};

bool in(int x, int y, int n, int m) {
	return (x > -1 && x < n && y > -1 && y < m);
}

int main()
{
	int n, m;
	cin >> n >> m;
	char md[n][m];
	for(int i=0; i<n; i++)
		for(int j=0; j<m; j++)
			cin >> md[i][j];
	bool vis[n][m]{};
	int d[n][m];
	d[0][0] = 1;
	int ans = 1;
	deque<pair<int,int>> q;
	q.push_front({0,0});
	while(!q.empty()){
		int x=q.front().first;
		int y=q.front().second;
		q.pop_front();
		if(vis[x][y]) continue;
		vis[x][y] = true;
		ans = max(ans, d[x][y]);
		for(int i=0; i<4; i++){
			if(md[x+dx[i]][y+dy[i]] == md[x][y] 
			and in(x+dx[i], y+dy[i], n, m)){
				d[x+dx[i]][y+dy[i]] = d[x][y];
				q.push_front({x+dx[i], y+dy[i]});
			}else{
				if(md[x+dx[i]][y+dy[i]] == '.') continue;
				if(in(x+dx[i], y+dy[i], n, m)){
					d[x+dx[i]][y+dy[i]] = d[x][y]+1;
					q.push_back({x+dx[i], y+dy[i]});
				}
			}	
		}
	}
	cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...