Submission #1280598

#TimeUsernameProblemLanguageResultExecution timeMemory
1280598alialiCollecting Mushrooms (NOI18_collectmushrooms)C++20
60 / 100
12 ms6860 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define ar array
#define boost ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
const int N = 1e6 + 12, INF = 1e18 + 7, mod = 1e9 + 7;
signed main () {
	boost
	int r, c, d, k;
	cin >> r >> c >> d >> k;
	char arr[r + 1][c + 1];
	vector<pair<int, int>>v, v2;
	for(int i = 1; i <= r; ++i){
		for(int j = 1; j <= c; ++j){
			cin >> arr[i][j];
			if(arr[i][j] == 'M'){
				v.push_back({i, j});
			} 
			if(arr[i][j] == 'S'){
				v2.push_back({i, j});
			}
		}
	}
	if(r != 1){
		int cnt = 0;
		for(auto [x, y] : v){
			int cn = 0;
			for(auto [x2, y2]: v2){
				int hm = max(abs(x2 - x), abs(y2 - y));
				if(hm <= d){
					cn += 1;
				}
				if(cn >= k){
					break;
				}
			}
			if(cn >= k){
				cnt += 1;
			}
		}
		cout << cnt << '\n';
	} else {
		int cnt2 = 0;
		for(auto [x, y] : v){
			int cnt = 0;
			if(x - 1 >= 1 and arr[x - 1][y] == 'S'){
				cnt += 1;
			}
			if(x + 1 <= r and arr[x + 1][y] == 'S'){
				cnt += 1;
			} 
			if(y - 1 >= 1 and arr[x][y - 1] == 'S'){
				cnt += 1;
			}
			if(y + 1 <= c and arr[x][y + 1] == 'S'){
				cnt += 1;
			}
			if(x - 1 >= 1 and y - 1 >= 1 and arr[x - 1][y - 1] == 'S'){
				cnt += 1;
			}
			if(x - 1 >= 1 and y + 1 <= c and arr[x - 1][y + 1] == 'S'){
				cnt += 1;
			}
			if(x + 1 <= r and y - 1 >= 1 and arr[x + 1][y - 1] == 'S'){
				cnt += 1;
			}
			if(x + 1 <= r and y + 1 <= c and arr[x + 1][y + 1] == 'S'){
				cnt += 1;
			}
			if(cnt >= k){
				cnt2 += 1;
			}
		}
		cout << cnt2;
	}
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...