SDNU1241北望村八卦阵

提交的两次重要错误:

1.标记问题,访问完成后没有及时标记,造成TLE

2.输出格式问题,没有输出IMPOSSIBLE 

#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
using namespace std;
const int maxn=1e2+3;
char mapp[maxn][maxn];
bool vis[maxn][maxn];
int pp,qq;
int X,Y;
/**L,W**/
bool flag[2];
struct node
{
    int x;
    int y;
    int st;
} p[maxn*maxn];
int n,m,res;
int step[4][2]= {{0,-1},{0,1},{1,0},{-1,0}};
void bfs()
{
    int xx,yy,st,x,y;
    flag[0]=flag[1];//0-l
    while(pp<qq)
    {
        st=p[pp].st+1;
        xx=p[pp].x;
        yy=p[pp].y;
        for(int i=0; i<4; ++i)
        {
            x=xx+step[i][0];
            y=yy+step[i][1];
            if(x<0||x>=n||y<0||y>=m)
                continue;
            if(mapp[x][y]=='*'||vis[x][y])
                continue;
            if(mapp[x][y]=='L')
            {
                if(flag[1])
                    continue;
                else
                    flag[0]=1;
            }
            if(mapp[x][y]=='W')
            {
                if(flag[0])
                    continue;
                else
                    flag[1]=1;
            }
            if(x==X-1&&y==Y-1)
            {
                res=st;
                return;
            }
            p[qq].x=x;
            p[qq].y=y;
            p[qq].st=st;
            qq++;
            vis[x][y]=1;
        }
        pp++;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        scanf("%d%d",&X,&Y);
        for(int i=0; i<n; ++i)
            scanf("%s",&mapp[i]);
        memset(vis,0,sizeof(vis));
        vis[0][0]=1;
        res=-1;
        pp=qq=0;
        p[qq].st=0;
        p[qq].x=0;
        p[qq].y=0;
        qq++;
        bfs();
        if(res!=-1)
            printf("%d\n",res*2);
        else
            printf("IMPOSSIBLE\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41658955/article/details/85112262