搜索题

17 篇文章 0 订阅

OJ题目 : 点~~

const int Max_N = 25;
const int inf = 100000000;

int N , M;
char table[Max_N][Max_N];
int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int minstep[Max_N][Max_N][Max_N][Max_N];

struct Point//表示Ball的结构体
{
    int x;
    int y;
    int OK;//1表示已搞定 , 0表示还没搞定
    Point(){}
    Point(int i , int j , int k):x(i),y(j),OK(k){}
};

struct Node//表示状态的结构体
{
    Point A;
    Point B;
    int step;//本状态的步数
    Node(){}
    Node(Point i , Point j , int s):A(i),B(j),step(s){}
};

vector<Point> List_Ball;

int bfs(Node s)
{
    int i , j , k , t;
    for(i = 1;i <= N;i++)
        for(j = 1;j <= M;j++)
            for(k = 1;k <= N;k++)
                for(t = 1;t <= M;t++)
                    minstep[i][j][k][t] = inf;
    queue<Node> que;
    que.push(s);
    Node now ;
    Point newA , newB;
    while(!que.empty())//bfs
    {
        now  = que.front();
        que.pop();
        if(now.A.OK&& now.B.OK)//如果两个Ball都已经进Hole , 成功 , 返回步数
            return now.step;
        int nextAx , nextBx , nextAy , nextBy;
        for(i = 0;i < 4;i++)
        {
            nextAx = now.A.x + dir[i][0];
            nextAy = now.A.y + dir[i][1];
            nextBx = now.B.x + dir[i][0];
            nextBy = now.B.y + dir[i][1];
            if(table[nextAx][nextAy] == '*' && table[nextBx][nextBy] == '*')//如果下面两个位置都是墙,则跳过
                continue;
            if(now.A.OK||table[nextAx][nextAy] == '*')//如果A已搞定,或者下个位置是墙,A的位置不变
            {
                nextAx = now.A.x;
                nextAy = now.A.y;
            }
            if(now.B.OK ||table[nextBx][nextBy] == '*')//如果B已搞定,或者下个位置是墙,B的位置不变
            {
                nextBx = now.B.x;
                nextBy = now.B.y;
            }
            if(!now.A.OK && !now.B.OK && nextAx == nextBx && nextAy == nextBy)//如果AB都在,且下个位置重合,则跳过
                continue;
            newA.x = nextAx;
            newA.y = nextAy;
            newB.x = nextBx;
            newB.y = nextBy;
            if(now.A.OK)//如果A已经搞定,下面考虑B
            {
                newA.OK = 1;//A的状态要赋值!!
                if(table[nextBx][nextBy] == 'H'&& !(nextAx == nextBx && nextAy == nextBy))//如果B的新位置刚好是Hole,且不是A的那个Hole,则B搞定
                    newB.OK = 1;
                else
                    newB.OK = 0;
            }
            else if(now.B.OK)//如果B已经搞定,下面考虑A
                {
                    newB.OK = 1;//B的状态要赋值!!
                    if(table[nextAx][nextAy] == 'H' && !(nextBx == nextAx && nextBy == nextAy))//如果A的新位置刚好是Hole,且不是B的那个Hole,则A搞定
                        newA.OK = 1;
                    else
                        newA.OK = 0;
                }
                else//如果A ,B都未搞定
                {
                    if(table[nextAx][nextAy] == 'H')//考虑A
                    {
                        newA.OK = 1;
                    }
                    else
                        newA.OK = 0;
                    if(table[nextBx][nextBy] == 'H')//考虑B
                    {
                        newB.OK = 1;
                    }
                    else
                        newB.OK = 0;
                }
            if(minstep[newA.x][newA.y][newB.x][newB.y] > now.step + 1)//如果新状态步数比之前的少,则更新
            {
                minstep[newA.x][newA.y][newB.x][newB.y] = now.step + 1;
                que.push(Node(newA , newB , now.step + 1));
            }
        }
    }
    return -1;
}

int main()
{
    int Case;
    cin >> Case;
    while(Case--)
    {
        scanf("%d%d" , &N ,&M);
        List_Ball.clear();
        int i , j;
        for(i = 1;i <= N;i++)
            scanf("%s",table[i]+1);
        for(i = 1;i <= N;i++)
            for(j = 1;j <= M;j++)
            if(table[i][j] == 'B')
                List_Ball.push_back(Point(i , j , 0));//List_Ball保存两个Ball

        int ans = bfs(Node(List_Ball[0] , List_Ball[1] , 0));//广搜
        if(ans == -1) cout << "Sorry , sir , my poor program fails to get an answer." << endl ;
        else
            cout << ans << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值