SSLZYC 反射

题目大意:
在一个长方形的区域里,每个位置都有一面镜子,请问从长方形外任意一点照射,光最多可以被反射几次?


思路:
这道题个人认为是一道比较难的暴力模拟,我们用s[i][j]来表示第i行j列的镜子的放置情况,f表示镜子反射的方向,以上,下,左,右的顺序来尝试从每个位置开始照射,最终输出正确结果。
这道题只需多注意些细节就行了。


代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

char s[1001][1001];
int n,m,a[1001][1001],f,sum,maxn;

int main()
{
    freopen("mirror.in","r",stdin);
    freopen("mirror.out","w",stdout);
    scanf("%d%d",&n,&m);
    getchar();
    for (int i=1;i<=n;i++,getchar())
    {
        for (int j=1;j<=m;j++)
         s[i][j]=getchar();
    } 
     for (int i=1;i<=3;i+=2)  //从上和下寻找
      for (int j=1;j<=m;j++)
        {
            f=i;
            sum=0;
            int x,y=j;
            if (i==1) x=1; else x=n;
            while (x>=1&&y>=1&&x<=n&&y<=m)
            {
                if (s[x][y]=='/'&&f==1&&y>=1) y--,f=4;
                else if (s[x][y]=='/'&&f==2&&x>=1) x--,f=3;
                else if (s[x][y]=='/'&&f==3&&y<=m) y++,f=2;
                else if (s[x][y]=='/'&&f==4&&x<=n) x++,f=1;
                else if (s[x][y]!='/'&&f==1&&y<=m) y++,f=2;
                else if (s[x][y]!='/'&&f==2&&x<=n) x++,f=1;
                else if (s[x][y]!='/'&&f==3&&y>=1) y--,f=4;
                else if (s[x][y]!='/'&&f==4&&x>=1) x--,f=3;  //更改位置及方向
                sum++; 
            }
            maxn=max(sum,maxn);
        }

     for (int j=2;j<=4;j+=2)  //从左和右寻找
      for (int i=1;i<=n;i++)
       {
            f=j;
            sum=0;
            int x=i,y;
            if (j==2) y=1; else y=m;
            a[i][j]=1;  
            while (x>=1&&y>=1&&x<=n&&y<=m)
            {
                if (s[x][y]=='/'&&f==1&&y>=1) y--,f=4;
                else if (s[x][y]=='/'&&f==2&&x>=1) x--,f=3;
                else if (s[x][y]=='/'&&f==3&&y<=m) y++,f=2;
                else if (s[x][y]=='/'&&f==4&&x<=n) x++,f=1;
                else if (s[x][y]!='/'&&f==1&&y<=m) y++,f=2;
                else if (s[x][y]!='/'&&f==2&&x<=n) x++,f=1;
                else if (s[x][y]!='/'&&f==3&&y>=1) y--,f=4;
                else if (s[x][y]!='/'&&f==4&&x>=1) x--,f=3; //更改位置及方向
                sum++; 
            }
            maxn=max(sum,maxn);
       }
    printf("%d",maxn);
}
原文地址:https://www.cnblogs.com/hello-tomorrow/p/9313129.html