#include <bits/stdc++.h>
#define fi first
#define se second
#define hsh(x, y) (((x)-1)*R+(y))
using namespace std;
typedef pair<int, int> pii;
const int xx[]={-1, 0, 1, 0}, yy[]={0, 1, 0, -1};
int M, R, C, U[805][805], S[16], v; string D;
int chk[805][805], A[805][805];
int pr[640005], Do[640005];
int get(int x) { return pr[x]?(pr[x]=get(pr[x])):x; }
void Un(int x, int y) {
if (Do[y]) { Do[x]=1; return ; }
x=get(x), y=get(y);
if (x!=y) pr[y]=x;
}
pii stk[640005]; int tp;
bool bfs(int x, int y) {
tp=0; chk[x][y]=v, stk[tp++]=pii(x, y);
for (int i=0; i<tp; i++) {
int a, b; tie(a, b)=stk[i];
for (int j=0; j<4; j++) {
int aa=a+xx[j], bb=b+yy[j], w=0;
if (!U[aa][bb]||chk[aa][bb]==v) continue;
for (int k=0; k<4; k++) if (chk[aa+xx[k]][bb+yy[k]]==v) w|=(1<<k);
if (S[w]<U[aa][bb]) continue;
if (hsh(x, y)!=get(hsh(aa, bb))) { Un(get(hsh(aa, bb)), hsh(x, y)); return 1; }
chk[aa][bb]=v, stk[tp++]=pii(aa, bb);
}
}
for (int i=0; i<tp; i++) A[stk[i].fi][stk[i].se]=tp;
Do[hsh(x, y)]=1;
return 0;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>M>>R>>C>>D;
for (int i=0; i<M; i++) D[i]=(D[i]=='N'?0:D[i]=='S'?2:D[i]=='E'?1:3);
for (int i=1; i<=R; i++) for (int j=1; j<=C; j++) cin>>U[i][j];
D+=D, M*=2;
for (int i=1; i<16; i++) for (int j=0, k; j<M; j++) {
if ((i&(1<<D[j]))!=(1<<D[j])) continue;
for (k=j; k<M; k++) if ((i&(1<<D[k]))!=(1<<D[k])) break;
S[i]=max(S[i], k-j); j=k-1;
}
for (int i=1; i<16; i++) if (S[i]==M) S[i]=1e5;
memset(A, 0x3f, sizeof(A));
while (1) {
bool fl=0;
for (int i=1; i<=R; i++) for (int j=1; j<=C; j++)
if (U[i][j]&&!Do[hsh(i, j)]&&get(hsh(i, j))==hsh(i, j)) v++, fl|=bfs(i, j);
if (!fl) break;
}
int A1=1e9, A2=0;
for (int i=1; i<=R; i++) for (int j=1; j<=C; j++) {
if (!U[i][j]) continue;
if (A1>A[i][j]) A1=A[i][j], A2=0;
if (A1==A[i][j]) A2++;
}
cout<<A1<<'\n'<<A2<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
3192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2936 KB |
Output is correct |
2 |
Correct |
10 ms |
3356 KB |
Output is correct |
3 |
Correct |
22 ms |
3484 KB |
Output is correct |
4 |
Correct |
10 ms |
3352 KB |
Output is correct |
5 |
Correct |
22 ms |
3356 KB |
Output is correct |
6 |
Correct |
24 ms |
3740 KB |
Output is correct |
7 |
Correct |
6 ms |
2936 KB |
Output is correct |
8 |
Correct |
23 ms |
3612 KB |
Output is correct |
9 |
Correct |
7 ms |
3196 KB |
Output is correct |
10 |
Correct |
10 ms |
3356 KB |
Output is correct |
11 |
Correct |
6 ms |
3192 KB |
Output is correct |
12 |
Correct |
8 ms |
3320 KB |
Output is correct |
13 |
Correct |
24 ms |
3740 KB |
Output is correct |
14 |
Correct |
23 ms |
3612 KB |
Output is correct |
15 |
Correct |
22 ms |
3740 KB |
Output is correct |
16 |
Correct |
22 ms |
3740 KB |
Output is correct |
17 |
Correct |
16 ms |
3448 KB |
Output is correct |
18 |
Correct |
7 ms |
3320 KB |
Output is correct |
19 |
Correct |
23 ms |
3612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
3192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |