Submission #174529

#TimeUsernameProblemLanguageResultExecution timeMemory
174529BruteforcemanVirus Experiment (JOI19_virus)C++11
6 / 100
469 ms23160 KiB
#include <bits/stdc++.h>
using namespace std;
#define count cnt
string s;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'E', 'W', 'S', 'N'};

typedef pair <int, int> pii;

bool vis[55][55];
bool infect[1 << 4][805][805];
int a[805][805];
int ans[805][805];
int optimal;
int count;
int r, c;


bool inside(int x, int y) {
	return (0 <= x && x < r) && (0 <= y && y < c);
}
int check(int i, int j) {
	int mask = 0;
	for(int p = 0; p < 4; p++) {
		int x = i + dx[p];
		int y = j + dy[p];
		if(inside(x, y) && vis[x][y]) {
			mask |= 1 << p;
		}
	}
	return infect[mask][i][j];
}
void solve(int x, int y) {
	memset(vis, 0, sizeof vis);
	queue <pii> Q;
	vector <pii> reach;
	vis[x][y] = true;
	Q.push(pii(x, y));
	while(!Q.empty()) {
		int p = Q.front().first;
		int q = Q.front().second;
		Q.pop();
		reach.push_back(pii(p, q));
		for(int i = 0; i < 4; i++) {
			int nx = p + dx[i];
			int ny = q + dy[i];
			if(!inside(nx, ny) || vis[nx][ny]) continue;
			if(check(nx, ny)) {
				Q.push(pii(nx, ny));
				vis[nx][ny] = true;
			}
		}
	}
	ans[x][y] = reach.size();
	if(optimal == ans[x][y]) {
		++count;
	} else if (ans[x][y] < optimal) {
		optimal = ans[x][y];
		count = 1;
	}
}
int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(0);
	
	int n;
	cin >> n >> r >> c;
	cin >> s;
	s = s + s;
	for(int x = 0; x < r; x++) {
		for(int y = 0; y < c; y++) {
			cin >> a[x][y];
		}
	}
	for(int i = 0; i < (1 << 4); i++) {
		set <char> allow;
		for(int j = 0; j < 4; j++) {
			if((i >> j) & 1) {
				allow.insert(dir[j]);
			}
		}
		int opt = 0;
		int cur = 0;
		for(int j = 0; j < (int) s.size(); j++) {
			int num;
			if(allow.find(s[j]) == allow.end()) {
				num = 0;
			} else num = 1;
			if(num == 0) cur = 0;
			else {
				cur += 1;
				opt = max(opt, cur);
			}
		}
		if(opt == n + n) {
			opt = INT_MAX;
		}
		for(int x = 0; x < r; x++) {
			for(int y = 0; y < c; y++) {
				if(a[x][y] > 0 && a[x][y] <= opt) {
					infect[i][x][y] = true;
				}
			}
		}
	}
	optimal = INT_MAX;
	count = 0;
	for(int x = 0; x < r; x++) {
		for(int y = 0; y < c; y++) {
			if(a[x][y] != 0) {
				solve(x, y);
			}
		}
	}
	cout << optimal << endl;
	cout << count << endl;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...