This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define x first
#define y second
const ll INF = 1e18;
ll N, M, K, U[805][805]; string S;
ll Mx[16], App[100005];
ll f(char c){
if(c == 'N') return 0;
if(c == 'S') return 1;
if(c == 'W') return 2;
if(c == 'E') return 3;
}
ll g(vector<ll> &V){
ll r = 0;
for(ll i = 0; i < 4; i++){
if(V[i]) r |= (1 << i);
}
return r;
}
void init(){
string T = S;
while(S.size() <= 100000){
for(auto e : T) S.push_back(e);
}
for(ll k = 0; k < 16; k++){
ll pv = -1;
for(ll i = 0; i < S.size(); i++){
if(!(k & (1 << f(S[i])))){
Mx[k] = max(Mx[k], i - pv - 1); pv = i;
}
}
if(pv == -1) Mx[k] = 100000;
App[Mx[k]] |= (1 << k);
}
for(ll i = 100000; i >= 1; i--){
App[i - 1] |= App[i];
}
}
bool In(ll x, ll y) { return (1 <= x && x <= N && 1 <= y && y <= M); }
ll Vir[805][805], F[805][805];
ll dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
bool chk(ll x, ll y){
ll b = 0;
for(ll i = 0; i < 4; i++){
ll nx = x + dx[i], ny = y + dy[i];
if(In(nx, ny) && Vir[nx][ny]) b |= (1 << i);
}
if(App[U[x][y]] & (1 << b)) return 1;
return 0;
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> K >> N >> M >> S;
for(ll i = 1; i <= N; i++){
for(ll j = 1; j <= M; j++){
cin >> U[i][j];
}
}
init();
ll mn = 1e18, cnt = 0;
for(ll p = 1; p <= N; p++){
for(ll q = 1; q <= M; q++){
if(U[p][q] == 0) continue;
fill(Vir[0], Vir[804] + 805, 0LL);
queue<pair<ll, ll>> Q;
Q.push({p, q});
Vir[p][q] = F[p][q] = 1;
ll Res = 1, bk = 0;
while(!Q.empty()){
ll x = Q.front().x, y = Q.front().y; Q.pop();
for(ll i = 0; i < 4; i++){
ll nx = x + dx[i], ny = y + dy[i];
if(!In(nx, ny) || U[nx][ny] == 0 || !chk(nx, ny) || Vir[nx][ny]) continue;
Vir[nx][ny] = 1; Res++;
Q.push({nx, ny});
if(F[nx][ny]){
bk = 1; break;
}
}
if(bk) break;
}
if(bk) continue;
if(mn > Res){
mn = Res, cnt = Res;
} else if(mn == Res){
cnt += Res;
}
}
}
cout << mn << '\n' << cnt;
}
Compilation message (stderr)
virus.cpp: In function 'void init()':
virus.cpp:33:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(ll i = 0; i < S.size(); i++){
| ~~^~~~~~~~~~
virus.cpp: In function 'll f(char)':
virus.cpp:17:1: warning: control reaches end of non-void function [-Wreturn-type]
17 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |