Source Code
//in the name of Allah

#include<bits/stdc++.h>

#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define SPEED ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sz size()
#define all(x) x.begin(),x.end()
#define OO 1e16
#define R return
#define Test int TT;cin>>TT;for(int T=1;T<=TT;T++)

using namespace std;
const ll N = 2000,Mod = 1e9 + 7;
ll n,m;
char a[N][N];
string s;
/// 0 -> Down , 1 -> Top , 2 -> Right, 3 -> Left
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
ll Dp[N][N][5];
pll C[N][N][5],Cur = {-1,-1};
ll Cur_d;
int Vis[N][N][5];

ll Solve(int i,int j,int d)
{
    if(i >= n || i < 0 || j >= m || j < 0)
        R 0;
    if(a[i][j] == '#')
        R 0;
    ll &Res = Dp[i][j][d];
    if(Res + 1)
        R Res;
    Res = 0;
    int Nxt_i = i + dx[d];
    int Nxt_j = j + dy[d];
    int new_d = d;
    if(a[i][j] == '.')
    {
        Res = 1 + Solve(Nxt_i,Nxt_j,d);
    }
    else if(a[i][j] == '/')
    {
        if(d == 0) // Down
        {
            Nxt_i = i;
            Nxt_j = j-1;
            new_d = 3;
        }
        else if(d == 1) // Top
        {
            Nxt_i = i;
            Nxt_j = j+1;
            new_d = 2;
        }
        else if(d == 2) // Right
        {
            Nxt_i = i-1;
            Nxt_j = j;
            new_d = 1;
        }
        else if(d == 3) // Left
        {
            Nxt_i = i+1;
            Nxt_j = j;
            new_d = 0;
        }
        Res = Solve(Nxt_i,Nxt_j,new_d);
    }
    else
    {
        if(d == 0) // Down
        {
            Nxt_i = i;
            Nxt_j = j+1;
            new_d = 2;
        }
        else if(d == 1) // Top
        {
            Nxt_i = i;
            Nxt_j = j-1;
            new_d = 3;
        }
        else if(d == 2) // Right
        {
            Nxt_i = i+1;
            Nxt_j = j;
            new_d = 0;
        }
        else if(d == 3) // Left
        {
            Nxt_i = i-1;
            Nxt_j = j;
            new_d = 1;
        }
        Res = Solve(Nxt_i,Nxt_j,new_d);
    }
    if(Cur.fr == Nxt_i && Cur.sc == Nxt_j)
    {
        C[Cur.fr][Cur.sc][Cur_d] = {i,j};
    }
    R Res;
}


int main()
{
    SPEED;

    cin >> n >> m;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            cin >> a[i][j];
            for(int d=0; d<4; d++)
                C[i][j][d] = {-1,-1};
        }
    }
    memset(Dp,-1,sizeof Dp);
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            for(int d=0; d<4; d++)
            {
                if(a[i][j] == '.')
                {
                    Cur = {i,j};
                    Cur_d = d;
                    Solve(i,j,d);
                }
            }
        }
    }
    ll Ans = 0;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            ll Res = 1;
            if(a[i][j] != '.')
                continue;
            for(int d=0; d<4; d++)
            {
                int new_i = i + dx[d];
                int new_j = j + dy[d];
                Res += Solve(new_i,new_j,d);
            }
            vector<pii> xx;
            for(int d=0; d<4; d++)
            {
                int new_i = i + dx[d];
                int new_j = j + dy[d];

                if(C[i][j][d].fr != -1)
                {
                    bool zzz = false;
                    for(int x=0; x<xx.sz; x++)
                    {
                        if(xx[i].fr == new_i && xx[i].sc == new_j)
                            zzz = true;
                    }
                    if(!zzz)
                    Res -= Solve(new_i,new_j,d);
                    xx.pb({C[i][j][d].fr,C[i][j][d].sc});
                }
            }
            Ans = max(Ans,Res);
        }
    }
    cout << Ans << endl;
    R 0;
}
Copy
Mirrors yaman_alwaza
GNU G++17
78 ms
157.0 MB
Wrong Answer