镜子田地(找规律+dfs)
题目分析:镜子有两种放置方式 ‘/’ 和 ‘\’ 将被镜子分为的上下看做两个节点,根据光路的可逆性,可以得出,每个节点的度数最大为2,故不会在外部射入到内部然后内部出现环,我们需要去枚举光路,因为保证光路是垂直射入,所以如图枚举 我们需要标记每一步的方向,因为镜子的存在会导致我们下一步的方向是会发生改变的,要根据遇到的镜子分析 1.遇到 ‘ / ’ 2.遇到 ‘ \ ’ 详细看代码
#include<bits/stdc++.h>
#define ll long long
#define PI 3.141592653589793
#define E 2.718281828459045
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define mem(a,b) memset(a,b,sizeof(a))
#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define FO( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define lowbit(a) ((a)&-(a))
#define PII pair<ll ,ll >
#define ft first
#define sd second
typedef unsigned long long ull;
const ll mod=10007;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll Max=1e3+10;
using namespace std;
ll t,n,m,l,k;
ll ans;
char mp[Max][Max];
ll mov[][2]={-1,0,0,1,1,0,0,-1};
ll dfs(ll x,ll y,ll d)
{
if(x<1||x>n||y<1||y>m)return 0;
if(mp[x][y]=='/')d^=1;
else d^=3;
return dfs(x+mov[d][0],y+mov[d][1],d)+1;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(ll i=1;i<=n;i++)
for(ll j=1;j<=m;j++)
cin>>mp[i][j];
for(ll i=1;i<=n;i++)
{
ans=max(ans,dfs(i,1,1));
ans=max(ans,dfs(i,m,3));
}
for(ll i=1;i<=m;i++)
{
ans=max(ans,dfs(1,i,2));
ans=max(ans,dfs(n,i,0));
}
cout<<ans<<endl;
return 0;
}
|