이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 55;
#define MP make_pair
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, m;
int a[N][N];
int arr[20];
int code[256];
bool ill[N][N];
int cnt[N][N];
int virus(int I, int J){
memset(ill, 0, sizeof(ill));
ill[I][J] = true;
queue<pair<int, int>> q;
q.push(MP(I - 1, J));
q.push(MP(I, J - 1));
q.push(MP(I + 1, J));
q.push(MP(I, J + 1));
while (!q.empty()){
int i = q.front().first, j = q.front().second;
q.pop();
if (ill[i][j] || a[i][j] == 0) continue;
int mask = 0;
if (ill[i - 1][j]) mask |= 1;
if (ill[i][j + 1]) mask |= 2;
if (ill[i + 1][j]) mask |= 4;
if (ill[i][j - 1]) mask |= 8;
if (arr[mask] >= a[i][j]){
ill[i][j] = true;
q.push(MP(i - 1, j));
q.push(MP(i, j - 1));
q.push(MP(i + 1, j));
q.push(MP(i, j + 1));
}
}
//cout << I << ' ' << J << endl;
int res = 0;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
res += ill[i][j];
// cout << ill[i][j] << ' ';
}
// cout << endl;
}
return res;
}
void solve(){
int len; string s;
cin >> len >> n >> m >> s;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> a[i][j];
}
}
s += s;
len <<= 1;
code['N'] = 1, code['E'] = 2, code['S'] = 4, code['W'] = 8;
for (int mask = 1; mask < 16; mask++){
for (int i = 0; i < len; i++){
if (!(mask & code[s[i]])) continue;
int j = i;
while (j < len && (mask & code[s[j]])) j++;
j--;
arr[mask] = max(arr[mask], j - i + 1);
i = j;
}
if (arr[mask] == len) arr[mask] = 1e9;
// cout << (bitset<4>(mask)) << ' ';
// cout << arr[mask] << endl;
}
int ans = 1e9, cnt = 0;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
if (a[i][j] == 0) continue;
int cur = virus(i, j);
if (cur < ans){
ans = cur;
cnt = 1;
} else if (cur == ans) cnt++;
}
}
cout << ans << endl << cnt << endl;
}
int main(){
ios_base::sync_with_stdio(false);
//freopen("output.txt", "w", stdout);
int tests = 1;
// cin >> tests;
while (tests--){
solve();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
virus.cpp: In function 'void solve()':
virus.cpp:81:35: warning: array subscript has type 'char' [-Wchar-subscripts]
81 | if (!(mask & code[s[i]])) continue;
| ^
virus.cpp:83:48: warning: array subscript has type 'char' [-Wchar-subscripts]
83 | while (j < len && (mask & code[s[j]])) j++;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |