https://www.luogu.org/problemnew/show/P1519

题解:二进制状态压缩+BFS

注意:洛谷上数据以‘\r\n’结尾,并非‘\n’

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=200+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N][N];
int vis[N][N];
char str[N][N]={' '};
int dir[4][2]={-1,0,0,1,1,0,0,-1};
struct node{
    int x,y,w;
    node(){};
    node(int i,int j,int v):x(i),y(j),w(v){}
}s,tmp,f;
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d%d",&m,&n);
    getchar();
    getchar();
    for(int i=1;i<=2*n+1;i++)
        cin.getline(str[i]+1,1000);
    queue<node>q;
    for(int i=2;i<=2*n+1;i+=2){
        for(int j=2;j<=2*m+1;j+=2){
            int x=i/2;
            int y=j/2;
            for(int k=0;k<4;k++){
                if(str[i+dir[k][0]][j+dir[k][1]]!=(k%2?'|':'-')){
                    a[x][y]+=(1<<k);
                }
            }
            if(str[i][1]!='|'&&!vis[x][1]){
                vis[x][1]=1;
                q.push({x,1,1});
            }
            if(str[i][2*m+1]!='|'&&!vis[x][m]){
                vis[x][m]=1;
                q.push({x,m,1});
            }
            if(str[1][j]!='-'&&!vis[1][y]){
                vis[1][y]=1;
                q.push({1,y,1});
            }
            if(str[2*n+1][j]!='-'&&!vis[n][y]){
                vis[n][y]=1;
                q.push({n,y,1});
            }
        }
    }
    ans=1;
    while(!q.empty()){
        f=q.front();
        q.pop();
        //cout<<f.x<<" "<<f.y<<" "<<f.w<<endl;
        for(int i=0;i<4;i++){
            if(a[f.x][f.y]&(1<<i)){
                tmp.x=f.x+dir[i][0];
                tmp.y=f.y+dir[i][1];
                tmp.w=f.w+1;
                if(1<=tmp.x&&tmp.x<=n&&1<=tmp.y&&tmp.y<=m&&!vis[tmp.x][tmp.y]){
                    vis[tmp.x][tmp.y]=tmp.w;
                    ans=max(ans,tmp.w);
                    q.push(tmp);
                }
            }
        }
    }
    cout<<ans<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}