搜索专题 POJ 1159:Maze

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xj13821328013/article/details/77885793

代码越写越长,样例过了,提交却WA,找不出原因,先贴在这里吧

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
int n,m;
int a1,b1,c1,d1,e1;
int sx,sy,ex,ey;
char ma[20][20];
bool mark[20][20];
int go[4][2]={0,1,0,-1,-1,0,1,0};
struct node
{
    int x,y;
    int a,b,c,d,e;
};
queue <node> Q;
int BFS()
{
    node tmp;
    tmp.x=sx,tmp.y=sy;
    tmp.a=tmp.b=tmp.c=tmp.d=tmp.e=0;
    mark[sx][sy]=true;
    Q.push(tmp);
    while(!Q.empty())
    {
        node temp;
        temp=Q.front();
        Q.pop();
        if(temp.x==ex&&temp.y==ey)
        {
            return true;
        }
        for(int i=0; i<4; i++)
        {
            int tx=temp.x+go[i][0];
            int ty=temp.y+go[i][1];
            int a2=0,b2=0,c2=0,d2=0,e2=0;
            if(ma[tx][ty]=='A'&&temp.a<a1||ma[tx][ty]=='B'&&temp.b<b1||ma[tx][ty]=='C'&&temp.c<c1||ma[tx][ty]=='D'&&temp.d<d1||ma[tx][ty]=='E'&&temp.e<e1)
                continue;
            if(mark[tx][ty]==false&&tx>=0&&tx<m&&ty>=0&&ty<n&&ma[tx][ty]!='X')
            {
                node tp;
                if(ma[tx][ty]=='A'&&temp.a==a1)
                {
                    ma[tx][ty]='.';
                }
                else if(ma[tx][ty]=='B'&&temp.b==b1)
                {
                    ma[tx][ty]='.';
                }
                else if(ma[tx][ty]=='C'&&temp.c==c1)
                {
                    ma[tx][ty]='.';
                }
                else if(ma[tx][ty]=='D'&&temp.d==d1)
                {
                    ma[tx][ty]='.';
                }
                else if(ma[tx][ty]=='E'&&temp.e==e1)
                {
                    ma[tx][ty]='.';
                }
                else if(ma[tx][ty]=='a')
                {
                    ma[tx][ty]='.';
                    a2=temp.a+1;
                    b2=temp.b;c2=temp.c;d2=temp.d;e2=temp.e;
                }
                else if(ma[tx][ty]=='b')
                {
                    ma[tx][ty]='.';
                    b2=temp.b+1;
                    a2=temp.a;c2=temp.c;d2=temp.d;e2=temp.e;
                }
                else if(ma[tx][ty]=='c')
                {
                    ma[tx][ty]='.';
                    c2=temp.c+1;
                    b2=temp.b;a2=temp.a;d2=temp.d;e2=temp.e;
                }
                else if(ma[tx][ty]=='d')
                {
                    ma[tx][ty]='.';
                    d2=temp.d+1;
                    b2=temp.b;c2=temp.c;a2=temp.a;e2=temp.e;
                }
                else if(ma[tx][ty]=='e')
                {
                    ma[tx][ty]='.';
                    e2=temp.e+1;
                    b2=temp.b;c2=temp.c;d2=temp.d;a2=temp.a;
                }
                //printf("%d %d\n",tp.x,tp.y);
                tp.x=tx;tp.y=ty;
                tp.a=a2;tp.b=b2;tp.c=c2;tp.d=d2;tp.e=e2;
                printf("%d %d %d %d %d %d %d\n",tp.x,tp.y,tp.a,tp.b,tp.c,tp.d,tp.e);
                mark[tx][ty]=true;
                Q.push(tp);
            }
        }
    }
    return false;
}

int main()
{
    while(~scanf("%d %d",&m,&n)&&(m||n))
    {
        while(!Q.empty())
        {
            Q.pop();
        }
        memset(mark,false,sizeof(mark));
        getchar();
        a1=b1=c1=d1=e1=0;
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                //scanf("%c",&ma[i][j]);
                cin>>ma[i][j];
                if(ma[i][j]=='S')
                {
                    sx=i;
                    sy=j;
                }
                else if(ma[i][j]=='a')
                {
                    a1++;
                }
                else if(ma[i][j]=='b')
                {
                    b1++;
                }
                else if(ma[i][j]=='c')
                {
                    c1++;
                }
                else if(ma[i][j]=='d')
                {
                    d1++;
                }
                else if(ma[i][j]=='e')
                {
                    e1++;
                }
                else if(ma[i][j]=='G')
                {
                    ex=i;
                    ey=j;
                }
            }
        }
        if(BFS())
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/xj13821328013/article/details/77885793
今日推荐