CF2045M

· · 题解

#include<bits/stdc++.h>
#define int long long
using namespace std;
int r,c;
int a[2100][2100];
char s[2100][2100];
int vis1[2100];//记录哪些发射器已经被使用过了
int vis[2100][2100]; 
int timee;
int tot;
struct edge
{
    int cnt;
    char side[2100];
    int num[2100];
}ans;
inline void get_start(int id,int &x,int &y,int &dx,int &dy)
{
    if(id<=c)//north
    {
        x=1,y=id;
        dx=1,dy=0;
    }
    else if(id<=2*c)//south
    {
        x=r,y=id-c;
        dx=-1,dy=0;
    }
    else if(id<=2*c+r)//west
    {
        x=id-2*c,y=1;
        dx=0,dy=1;
    }
    else if(id<=2*c+2*r)//east
    {
        x=id-2*c-r,y=c;
        dx=0,dy=-1;
    }
}
inline int get_exit(int x,int y)
{
    if(x==0)//north
    {
        return y;
    }
    if(x==r+1)//south
    {
        return c+y;
    }
    if(y==0)//west
    {
        return 2*c+x;
    }
    if(y==c+1)//east
    {
        return 2*c+r+x;
    }
}
inline void get_ans(int id)
{
    ans.cnt++;
    if(id<=c)//north
    {
        ans.side[ans.cnt]='N';
        ans.num[ans.cnt]=id;
    }
    else if(id<=2*c)//south
    {
        ans.side[ans.cnt]='S';
        ans.num[ans.cnt]=id-c;
    }
    else if(id<=2*c+r)//west
    {
        ans.side[ans.cnt]='W';
        ans.num[ans.cnt]=id-2*c;
    }
    else if(id<=2*c+2*r)//east
    {
        ans.side[ans.cnt]='E';
        ans.num[ans.cnt]=id-2*c-r;
    }
}
inline void light_path(int st)
{
    int x,y,dx,dy;
    get_start(st,x,y,dx,dy);
    int cnt=0;
    timee++;
    while(x>=1&&y>=1&&x<=r&&y<=c)
    {
        if(s[x][y]!='.'&&vis[x][y]!=timee)
        {
            vis[x][y]=timee;
            cnt++;
        }
        if(s[x][y]=='/')
        {
            dx=-dx,dy=-dy;
            swap(dx,dy);
        }
        if(s[x][y]!='/'&&s[x][y]!='.')
        {
            swap(dx,dy);
        }
        x+=dx,y+=dy;
    }
    int t=get_exit(x,y);
    vis1[st]=1;
    vis1[t]=1;
    if(cnt==tot)
    {
        get_ans(st);
        get_ans(t);
    }
}
inline void print()
{
    printf("%lld\n",ans.cnt);
    if(ans.cnt>0)
    {
        printf("%c%lld",ans.side[1],ans.num[1]);
        for(int i=2;i<=ans.cnt;i++)
        {
            printf(" %c%lld",ans.side[i],ans.num[i]);
        }
        printf("\n");
    }
}
signed main()
{
    scanf("%lld%lld",&r,&c);
    int n=(r+c)*2;
    for(int i=1;i<=r;i++)
    {
        scanf("%s",s[i]+1);
        for(int j=1;j<=c;j++)
        {
            if(s[i][j]!='.')
            {
                tot++;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(vis1[i]==0)
        {
            light_path(i);
        }
    }
    print();
    return 0;
}