链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
题目描述
There is an n×mn\times mn×m mirror maze, where there is a mirror on each grid. The mirrors are in one of the following four types:
- ``-'', the light from above or below will be reflected back, the light from left or right will continue going forward without being reflected, respectively;
- ``|'', the light from left or right will be reflected back, the light from above or below will continue going forward without being reflected, respectively;
- ``/'', the light from left, right, above, below will be reflected to go above, below, left, right, respectively;
- ``\\backslash\'', the light from left, right, above, below will be reflected to go below, above, right, left, respectively.
Now there are qqq light sources. Little G, the believer of the light, wants to know the numbers of different mirrors the emitted light will be reflected by within sufficient time for each light source.
输入描述:
The first line contains two integers n,m (1≤n,m≤1 000)n,m\,(1 \le n,m \le 1\,000)n,m(1≤n,m≤1000), denoting the size of the mirror maze. Each of the following nnn lines contains a string of length mmm, where the jjj-th character in the iii-th line Si,j (Si,j∈{∣,−,/,\})S_{i,j}\,(S_{i,j} \in \{|, -, /, \backslash\})Si,j(Si,j∈{∣,−,/,\}) denotes the mirror on grid (i,j)(i,j)(i,j). The next line contains an integer q (1≤q≤105)q\,(1 \le q \le 10^5)q(1≤q≤105), denoting the number of light sources. Each of the following qqq lines contains two integers u (1≤u≤n)u\,(1 \le u \le n)u(1≤u≤n), v (1≤v≤m)v\,(1 \le v \le m)v(1≤v≤m) and a string dir (dir∈{above,below,left,right})dir\,(dir \in \{above, below, left, right\})dir(dir∈{above,below,left,right}), denoting that a light source is on grid (u,v)(u,v)(u,v) and emits light going along the dirdirdir direction. Specifically, the light will not be influenced by the mirror on grid (u,v)(u,v)(u,v) initially.
输出描述:
Output qqq lines each containing one integer, denoting the number of different mirrors the emitted light will be reflected by within sufficient time for each light source.
样例1:
输入:
3 3
/\|
|/|
\/|
1
2 3 left
输出:
7
(出了基本上没有问题)
#include<bits/stdc++.h>
using namespace std;
//上 下 左 右
//0 1 2 3
int n,m;
char s[1005][1005];
int vised[1005][1005][4];
int vis[1005][1005];
int dfs(int u,int v,int dir){
if(vised[u][v][dir])return 0;//搜过退出
if(u<1||v<1||u>n||v>m)return 0;//超 退出
int now=0;
switch(dir){//4方向
case 0:
if(s[u][v]=='|'){//直接穿过 不要忘记记录 now
now=dfs(u-1,v,dir);
}else{
vised[u][v][dir]=1;
if(vis[u][v]==1)now=-1;//如果成环 now-1 免的多加 退出
vis[u][v]=1;
switch(s[u][v]){//剩下3个方向
case '-':
now+=1+dfs(u+1,v,1);break;//折射后的方向
case '/':
now+=1+dfs(u,v+1,3);break;
case '\\':
now+=1+dfs(u,v-1,2);break;
}
vised[u][v][dir]=0;//回溯
vis[u][v]=0;
}
break;
case 1://-----------------------下面同理
if(s[u][v]=='|'){
now=dfs(u+1,v,dir);
}else{
vised[u][v][dir]=1;
if(vis[u][v]==1)now=-1;
vis[u][v]=1;
switch(s[u][v]){
case '-':
now+=1+dfs(u-1,v,0);break;
case '/':
now+=1+dfs(u,v-1,2);break;
case '\\':
now+=1+dfs(u,v+1,3);break;
}
vised[u][v][dir]=0;
vis[u][v]=0;
}
break;
case 2:
if(s[u][v]=='-'){
now=dfs(u,v-1,dir);
}else{
vised[u][v][dir]=1;
if(vis[u][v]==1)now=-1;
vis[u][v]=1;
switch(s[u][v]){
case '|':
now+=1+dfs(u,v+1,3);break;
case '/':
now+=1+dfs(u+1,v,1);break;
case '\\':
now+=1+dfs(u-1,v,0);break;
}
vis[u][v]=0;
vised[u][v][dir]=0;
}
break;
case 3:
if (s[u][v]=='-'){
now=dfs(u,v+1,dir);
}
else{
vised[u][v][dir]=1;
if (vis[u][v]==1) now=-1;
vis[u][v]=1;
switch(s[u][v]){
case '|':now+=1+dfs(u,v-1,2);break;
case '/':now+=1+dfs(u-1,v,0);break;
case '\\':now+=1+dfs(u+1,v,1);break;
}
vis[u][v]=0;
vised[u][v][dir]=0;
}
break;
}
return now;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>s[i][j];
int x,y;
string s;
int ttt;
cin>>ttt;
while(ttt--){
cin>>x>>y>>s;
int ans=0;
if(s=="above") ans=dfs(x-1,y,0); //4方向输入 dfs 下一方向
else if(s=="below") ans=dfs(x+1,y,1);
else if(s=="left") ans=dfs(x,y-1,2);
else if(s=="right") ans=dfs(x,y+1,3);
cout<<ans<<endl;
}
return 0;
}