This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// wygzgyw
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
template <typename T> void write(T t) {
if (t<0) { putchar('-'); write(-t); return; }
if (t>9) write(t/10);
putchar('0'+t%10);
}
template <typename T> void writeln(T t) { write(t); puts(""); }
#define MP make_pair
const int INF=1e9;
const int maxn=810;
const int maxm=maxn*maxn;
char s[200010];
int len,n,m,d[maxn][maxn];
int mx[16],dy[300];
int fx[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
bool vis[maxn][maxn];
int msk[maxn][maxn];
pair<int,int> q[maxm];
int qq[maxm];
int hd,tl,flag;
vector<pair<int,int> > V;
int pos[maxn][maxn],tot;
pair<int,int> p[maxm];
int mn=INF,cnt;
vector<int> G[maxm];
int calc(int x,int y,int id) {
V.clear(); V.push_back(MP(x,y));
q[hd=tl=1]=MP(x,y); vis[x][y]=1;
while (hd<=tl) {
x=q[hd].first,y=q[hd].second,hd++;
for (int i=0;i<4;i++) {
int a=x+fx[i][0],b=y+fx[i][1];
if (1<=a&&a<=n&&1<=b&&b<=m&&!vis[a][b]&&d[a][b]) {
msk[a][b]|=(1<<((i+2)%4));
V.push_back(MP(a,b));
if (mx[msk[a][b]]>=d[a][b]) {
// printf(" %d %d\n",id,pos[a][b]);
G[id].push_back(pos[a][b]);
if (pos[a][b]<id) goto E;
q[++tl]=MP(a,b),vis[a][b]=1;
}
}
}
}
E:
for (pair<int,int> &A : V) msk[A.first][A.second]=vis[A.first][A.second]=0;
return tl;
}
int dfn[maxm],low[maxm],id;
int st[maxm],top;
int bel[maxm],col;
int sz[maxm];
void tarjan(int x) {
dfn[x]=low[x]=++id;
st[++top]=x;
for (int v : G[x]) {
if (!dfn[v]) {
tarjan(v);
low[x]=min(low[x],low[v]);
} else if (!bel[v]) low[x]=min(low[x],dfn[v]);
}
if (dfn[x]==low[x]) {
bel[x]=++col; sz[col]=1;
while(st[top]!=x) {
bel[st[top]]=col; sz[col]++;
--top;
}
--top;
}
}
int out[maxm];
vector<int> H[maxm];
int main() {
scanf("%d %d %d %s",&len,&n,&m,s+1);
dy['W']=3,dy['N']=0,dy['E']=1,dy['S']=2;
int all=0; flag=1;
for (int i=1;i<=len;i++) s[i+len]=s[i],all|=(1<<dy[s[i]]);
for (int t=0;t<16;t++) {
if ((t&all)==all) { mx[t]=INF; continue; }
int cnt=0;
for (int i=1;i<=len*2;i++) {
if (t>>dy[s[i]]&1) cnt++,mx[t]=max(mx[t],cnt); else cnt=0;
}
}
for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) {
read(d[i][j]);
if (d[i][j]) p[++tot]=MP(i,j);
}
random_shuffle(p+1,p+tot+1);
for (int i=1;i<=tot;i++) pos[p[i].first][p[i].second]=i;
all=0;
for (int i=1;i<=tot;i++) {
all+=calc(p[i].first,p[i].second,i);
}
// printf("%d\n",all);
for (int i=1;i<=tot;i++) if (!dfn[i]) tarjan(i);
for (int i=1;i<=tot;i++) for (int &j : G[i]) if (bel[i]!=bel[j]) out[bel[i]]++;
for (int i=1;i<=tot;i++) if (out[bel[i]]==0) {
int res=sz[bel[i]];
if (mn==res) cnt++; else if (res<mn) mn=res,cnt=1;
//printf("res=%d %d\n",res,i);
//assert(res==calc(p[i].first,p[i].second,0));
}
printf("%d\n%d\n",mn,cnt);
return 0;
}
/*
0. Enough array size? Enough array size? Enough array size? Integer overflow?
1. Think TWICE, Code ONCE!
Are there any counterexamples to your algo?
2. Be careful about the BOUNDARIES!
N=1? P=1? Something about 0?
3. Do not make STUPID MISTAKES!
Time complexity? Memory usage? Precision error?
*/
Compilation message (stderr)
virus.cpp: In function 'int main()':
virus.cpp:84:56: warning: array subscript has type 'char' [-Wchar-subscripts]
84 | for (int i=1;i<=len;i++) s[i+len]=s[i],all|=(1<<dy[s[i]]);
| ~~~^
virus.cpp:89:17: warning: array subscript has type 'char' [-Wchar-subscripts]
89 | if (t>>dy[s[i]]&1) cnt++,mx[t]=max(mx[t],cnt); else cnt=0;
| ~~~^
virus.cpp:81:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d %d %d %s",&len,&n,&m,s+1);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |