1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
| void dfs(int x,int y,int d){
if(x>n||x<=0||y>m||y<=0)return;
sx[++hd]=x,sy[hd]=y,sd[hd]=d;
if(p[x][y][d]){
rg=1;
return;
}
p[x][y][d]=1;
int nd=dt[d][mir[x][y]];
dfs(x+dx[nd],y+dy[nd],nd);
}
int Dfs(int x,int y,int d){
if(x>n||x<=0||y>m||y<=0)return 0;
// cerr<<x<<' '<<y<<' '<<d<<endl;
sx[++hd]=x,sy[hd]=y,sd[hd]=d;
dp[x][y][d]=0;
assert(!p[x][y][d]);
p[x][y][d]=1;
int nd=dt[d][mir[x][y]];
dp[x][y][d]+=Dfs(x+dx[nd],y+dy[nd],nd);
if(!vm[x][y]&&ok(d,mir[x][y])){
vm[x][y]=1,++dp[x][y][d];
}
return dp[x][y][d];
}
void work(){
cin>>n>>m;
memset(dp,-1,sizeof dp);
FOR(i,1,n){
cin>>s;
FOR(j,1,m){
if(s[j-1]=='-')mir[i][j]=0;
if(s[j-1]=='|')mir[i][j]=1;
if(s[j-1]=='/')mir[i][j]=2;
if(s[j-1]=='\\')mir[i][j]=3;
}
}
cin>>q;
while(q--){
int x,y,d;
cin>>x>>y>>s;
if(s=="above")d=0;
if(s=="below")d=1;
if(s=="left")d=2;
if(s=="right")d=3;
x+=dx[d],y+=dy[d];
if(x>n||x<=0||y>m||y<=0){
puts("0");
continue;
}
if(get(x,y,d)!=-1){
printf("%d\n",get(x,y,d));
continue;
}
dfs(x,y,d);
if(rg){
FOR(i,1,hd){
int X=sx[i],Y=sy[i];
if(!vm[X][Y]&&ok(sd[i],mir[X][Y])){
vm[X][Y]=1,++ans;
}
}
FOR(i,1,hd)dp[sx[i]][sy[i]][sd[i]]=ans;
clr();
printf("%d\n",get(x,y,d));
continue;
}
int X=sx[hd],Y=sy[hd],D=dt[sd[hd]][mir[X][Y]]^1;
clr();
Dfs(X,Y,D);
X=sx[hd],Y=sy[hd],D=dt[sd[hd]][mir[X][Y]]^1;
clr();
Dfs(X,Y,D);
clr();
printf("%d\n",get(x,y,d));
}
}
|