#include <bits/stdc++.h>
using namespace std;
const int Nmax = 808, inf = 1e9;
int ans[Nmax][Nmax];
int sz[Nmax * Nmax], t[Nmax * Nmax];
int A[Nmax][Nmax], last_visited[Nmax][Nmax];
string moves;
int L, N, M, id_bfs = 0;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
string directions = "NSWE";
int best[20];
void max_to(int &x, int y) { if(x < y) x = y; }
int get_comp(int x, int y) { return (x-1) * M + y; }
int boss(int x)
{
if(x == t[x]) return x;
return t[x] = boss(t[x]);
}
void prep()
{
int i;
moves = moves + moves;
for(auto &c : moves)
c = find(directions.begin(), directions.end(), c) - directions.begin();
for(i=0; i<(1<<4); ++i)
{
int j, nr = 0;
for(j=0; j<moves.size(); ++j)
if(i & (1<<moves[j])) ++nr;
else max_to(best[i], nr), nr = 0;
for(j=0; j<moves.size(); ++j)
if(i & (1<<moves[j])) ++nr;
else break;
max_to(best[i], nr);
if(best[i] > moves.size() / 2) best[i] = inf;
}
}
void init()
{
int i, j;
for(i=1; i<=N; ++i)
for(j=1; j<=M; ++j)
if(A[i][j])
{
ans[i][j] = inf;
int now = get_comp(i, j);
t[now] = now;
sz[now] = 1;
}
}
void connect(int a, int b)
{
if(sz[b] == -1)
{
sz[a] = -1;
return;
}
t[a] = b;
sz[b] += sz[a];
return;
}
bool bfs(int x, int y)
{
++id_bfs;
queue<int> q;
vector<pair<int,int>> visited;
visited.push_back({x, y});
last_visited[x][y] = id_bfs;
int id = get_comp(x, y);
int cursor = 0;
while(cursor < visited.size())
{
tie(x, y) = visited[cursor++];
int i;
for(i=0; i<4; ++i)
{
int nx, ny;
nx = x + dx[i];
ny = y + dy[i];
if(A[nx][ny] == 0) continue;
int j; int mask = 0;
for(j=0; j<4; ++j)
if(last_visited[nx + dx[j]][ny + dy[j]] == id_bfs) mask |= (1<<j);
if(A[nx][ny] > best[mask]) continue;
int nid = boss(get_comp(nx, ny));
if(nid == id)
{
if(id_bfs == last_visited[nx][ny]) continue;
visited.push_back({nx, ny});
last_visited[nx][ny] = id_bfs;
}
else
{
connect(id, nid);
return 1;
}
}
}
sz[id] = -1;
for(auto it : visited) ans[it.first][it.second] = visited.size();
return 0;
}
bool solve()
{
int i, j;
bool done = 0;
for(i=1; i<=N; ++i)
for(j=1; j<=M; ++j)
{
if(A[i][j] == 0) continue;
int curr = get_comp(i, j);
if(boss(curr) == curr && sz[curr] != -1)
done |= bfs(i, j);
}
return done;
}
int main()
{
// freopen("virus.in", "r", stdin);
cin.tie(0); cin.sync_with_stdio(false);
cin >> L >> N >> M;
cin >> moves;
int i, j;
for(i=1; i<=N; ++i)
for(j=1; j<=M; ++j)
cin >> A[i][j];
prep();
init();
bool ok = 1;
while(ok) ok = solve();
int mn = inf, cnt = 0;
for(i=1; i<=N; ++i)
for(j=1; j<=M; ++j)
if(A[i][j])
{
if(mn > ans[i][j]) mn = ans[i][j], cnt = 1;
else if(mn == ans[i][j]) ++cnt;
}
cout << mn << '\n' << cnt << '\n';
return 0;
}
Compilation message
virus.cpp: In function 'void prep()':
virus.cpp:40:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0; j<moves.size(); ++j)
~^~~~~~~~~~~~~
virus.cpp:45:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0; j<moves.size(); ++j)
~^~~~~~~~~~~~~
virus.cpp:50:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(best[i] > moves.size() / 2) best[i] = inf;
~~~~~~~~^~~~~~~~~~~~~~~~~~
virus.cpp: In function 'bool bfs(int, int)':
virus.cpp:94:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(cursor < visited.size())
~~~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
760 KB |
Output is correct |
2 |
Correct |
185 ms |
14424 KB |
Output is correct |
3 |
Correct |
200 ms |
16604 KB |
Output is correct |
4 |
Correct |
165 ms |
14164 KB |
Output is correct |
5 |
Correct |
203 ms |
16816 KB |
Output is correct |
6 |
Correct |
8 ms |
5752 KB |
Output is correct |
7 |
Correct |
258 ms |
14784 KB |
Output is correct |
8 |
Correct |
95 ms |
8832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
508 KB |
Output is correct |
2 |
Correct |
7 ms |
924 KB |
Output is correct |
3 |
Correct |
19 ms |
924 KB |
Output is correct |
4 |
Correct |
8 ms |
924 KB |
Output is correct |
5 |
Correct |
19 ms |
888 KB |
Output is correct |
6 |
Correct |
20 ms |
1312 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
20 ms |
1308 KB |
Output is correct |
9 |
Correct |
3 ms |
764 KB |
Output is correct |
10 |
Correct |
7 ms |
928 KB |
Output is correct |
11 |
Correct |
3 ms |
888 KB |
Output is correct |
12 |
Correct |
4 ms |
888 KB |
Output is correct |
13 |
Correct |
18 ms |
1308 KB |
Output is correct |
14 |
Correct |
18 ms |
1308 KB |
Output is correct |
15 |
Correct |
18 ms |
1308 KB |
Output is correct |
16 |
Correct |
18 ms |
1308 KB |
Output is correct |
17 |
Correct |
12 ms |
1144 KB |
Output is correct |
18 |
Correct |
4 ms |
888 KB |
Output is correct |
19 |
Correct |
20 ms |
1308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
760 KB |
Output is correct |
2 |
Correct |
185 ms |
14424 KB |
Output is correct |
3 |
Correct |
200 ms |
16604 KB |
Output is correct |
4 |
Correct |
165 ms |
14164 KB |
Output is correct |
5 |
Correct |
203 ms |
16816 KB |
Output is correct |
6 |
Correct |
8 ms |
5752 KB |
Output is correct |
7 |
Correct |
258 ms |
14784 KB |
Output is correct |
8 |
Correct |
95 ms |
8832 KB |
Output is correct |
9 |
Correct |
2 ms |
508 KB |
Output is correct |
10 |
Correct |
7 ms |
924 KB |
Output is correct |
11 |
Correct |
19 ms |
924 KB |
Output is correct |
12 |
Correct |
8 ms |
924 KB |
Output is correct |
13 |
Correct |
19 ms |
888 KB |
Output is correct |
14 |
Correct |
20 ms |
1312 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
20 ms |
1308 KB |
Output is correct |
17 |
Correct |
3 ms |
764 KB |
Output is correct |
18 |
Correct |
7 ms |
928 KB |
Output is correct |
19 |
Correct |
3 ms |
888 KB |
Output is correct |
20 |
Correct |
4 ms |
888 KB |
Output is correct |
21 |
Correct |
18 ms |
1308 KB |
Output is correct |
22 |
Correct |
18 ms |
1308 KB |
Output is correct |
23 |
Correct |
18 ms |
1308 KB |
Output is correct |
24 |
Correct |
18 ms |
1308 KB |
Output is correct |
25 |
Correct |
12 ms |
1144 KB |
Output is correct |
26 |
Correct |
4 ms |
888 KB |
Output is correct |
27 |
Correct |
20 ms |
1308 KB |
Output is correct |
28 |
Correct |
284 ms |
23040 KB |
Output is correct |
29 |
Correct |
171 ms |
14656 KB |
Output is correct |
30 |
Correct |
191 ms |
14748 KB |
Output is correct |
31 |
Correct |
234 ms |
14328 KB |
Output is correct |
32 |
Correct |
302 ms |
14032 KB |
Output is correct |
33 |
Correct |
166 ms |
14328 KB |
Output is correct |
34 |
Correct |
453 ms |
22880 KB |
Output is correct |
35 |
Correct |
284 ms |
15260 KB |
Output is correct |
36 |
Correct |
347 ms |
14712 KB |
Output is correct |
37 |
Correct |
360 ms |
18400 KB |
Output is correct |
38 |
Correct |
265 ms |
22624 KB |
Output is correct |
39 |
Correct |
217 ms |
15624 KB |
Output is correct |
40 |
Correct |
232 ms |
15720 KB |
Output is correct |
41 |
Correct |
149 ms |
14456 KB |
Output is correct |
42 |
Correct |
250 ms |
18344 KB |
Output is correct |
43 |
Correct |
218 ms |
14744 KB |
Output is correct |
44 |
Correct |
92 ms |
8848 KB |
Output is correct |