Submission #340188

#TimeUsernameProblemLanguageResultExecution timeMemory
340188lohachoSandwich (JOI16_sandwich)C++14
100 / 100
5802 ms4068 KiB
#include <bits/stdc++.h>

using namespace std;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int N, M;
	cin >> N >> M;
	vector<string> a(N);
	for(int i = 0; i < N; ++i){
		cin >> a[i];
	}
	vector<vector<int>> chk(N, vector<int>(M, 0));
	vector<vector<int>> ans(N, vector<int>(M, (int)1e9));
	vector<int> NN = {0, 0, 1, 1}, ZZ = {0, 1, 1, 0};
	vector<int> wx = {-1, 0, 1, 0}, wy = {0, 1, 0, -1};
	function<int(int, int, int)> dfs = [&](int x, int y, int dir){
		if(chk[x][y] == 1){
			return (int)1e9;
		}
		if(chk[x][y] == 2){
			return 0;
		}
		int rv = 1;
		chk[x][y] = 1;
		for(int i = 0; i < 4; ++i){
			if((a[x][y] == 'N' && NN[i] == dir) || (a[x][y] == 'Z' && ZZ[i] == dir)){
				int nx = x + wx[i], ny = y + wy[i];
				if(nx >= 0 && ny >= 0 && nx < N && ny < M){
					if(a[nx][ny] == 'N'){
						rv += dfs(nx, ny, NN[i]);
						if(rv >= (int)1e9) return rv;
					}
					else{
						rv += dfs(nx, ny, ZZ[i]);
						if(rv >= (int)1e9) return rv;
					}
				}
			}
		}
		chk[x][y] = 2;
		return rv;
	};
	for(int j = 0; j < M; ++j){
		chk = vector<vector<int>>(N, vector<int>(M, 0));
		int sum = 0;
		for(int i = 0; i < N; ++i){
			if(chk[i][j]) break;
			sum += dfs(i, j, 0);
			if(sum > (int)1e8){
				break;
			}
			ans[i][j] = min(sum * 2, ans[i][j]);
		}
		chk = vector<vector<int>>(N, vector<int>(M, 0));
		sum = 0;
		for(int i = N - 1; i >= 0; --i){
			if(chk[i][j]) break;
			sum += dfs(i, j, 1);
			if(sum > (int)1e8){
				break;
			}
			ans[i][j] = min(sum * 2, ans[i][j]);
		}
	}
	for(int i = 0; i < N; ++i){
		for(int j = 0; j < M; ++j){
			if(ans[i][j] == (int)1e9){
				ans[i][j] = -1;
			}
			cout << ans[i][j] << ' ';
		}
		cout << '\n';
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...