Lightoj1238——Power Puff Girls(bfs)

The city of Townsville! This nice city is the home for the power puff girls - Blossom, Bubbles and Buttercup. To introduce their personality we can sing a song:

Blossom, commander and the leader;
Bubbles, she is the joy and the laughter;
Buttercup, she is the toughest fighter.
这里写图片描述
These super girls defend Townsville from monsters and super villains using their super powers, intelligence. They live in a home in Townsville with the professor who is like their father.

The girls are young now and they don’t like to fight the monsters anymore. So, if they are outside their home, they are found shopping. And when they get back to home, they simply watch the TV serials. It’s such a horrible fact that the super intelligent girls are wasting their time watching serials that consist of the rivalries between Wives and their Mother in Laws. And when they use computers they are usually found using the facenote (a dangerous1 social networking site).

So, such wonderful girls just became lazy and useless. Often they are seen fighting each other, the comments that can be heard, are like, ‘Tulsi is the best.’, ‘Gopi is better than Rashee.’ The professor is quite upset with the girls, and he can’t even watch any science show or sports because of these irritating serials.

So, the professor made a plan and asked the girls to go for shopping such that he can watch an important science show in the TV. The girls became very excited and they went out for shopping. But soon they realize that one of their favorite serials will start soon, and they need to get back home for that serial.

To be more specific let’s consider the city as a 2D grid. In the grid there are some symbols, the meaning of them are:

  1. ‘.’ means an empty place
  2. ‘a’ denotes the position of Blossom
  3. ‘b’ denotes the position of Bubbles
  4. ‘c’ denotes the position of Buttercup
  5. ‘m’ denotes that there is a monster
  6. ‘h’ denotes their home
  7. ‘#’ denotes a wall and the girls can’t pass through it
    The three girls move simultaneously. And in each minute, from their current cells, they can move to any four adjacent cells (North, East, West, South) if the destination cell is neither a wall nor the cell contains a monster. Because they want to get home as soon as possible, they want to avoid the monsters. You can assume that they can move to a common cell if necessary.

Now you are given the information of the city and their positions. You have to find the minimum time when they all are in home.

Input
Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing two integers: m and n (4 ≤ m, n ≤ 20), m denotes the number of rows and n denotes the number of columns of the modeled grid. Each of the next m lines contains n characters representing the city.

You can assume that ‘a’, ‘b’, ‘c’, ‘h’ will occur exactly once in the grid. The outer boundaries will always be marked by ‘#’.

Output
For each case, print the case number and the minimum time needed when they all are in their home. You can assume that a solution will always exist.

Sample Input
Output for Sample Input
2
6 8
########
#…c..#
#……#
#.a.h.b#
#……#
########
5 9
#########
#mmm…c#
#ma.h####
#m….b.#
#########
Case 1: 2
Case 2: 4

三个小女警a,b,c要回到家,求至少需要多久才能全部到达,m和#都不能经过

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <map>
#define INF 0x3f3f3f3f
#define MAXN 105
#define Mod 20007
using namespace std;
int n,m;
char mp[30][30];
int dx[]= {1,-1,0,0};
int dy[]= {0,0,1,-1};
int vis[30][30];
struct Node
{
    int x,y,step;
};
int bfs(int x,int y)
{
    memset(vis,0,sizeof(vis));
    Node start;
    start.x=x,start.y=y;
    start.step=0;
    queue<Node> q;
    q.push(start);
    while(!q.empty())
    {
        Node tmp=q.front(),tmp1;
        q.pop();
        if(mp[tmp.x][tmp.y]=='h')
            return tmp.step;
        for(int i=0; i<4; ++i)
        {
            tmp1=tmp;
            tmp1.x+=dx[i];
            tmp1.y+=dy[i];
            if(!vis[tmp1.x][tmp1.y]&&mp[tmp1.x][tmp1.y]!='#'&&mp[tmp1.x][tmp1.y]!='m')
            {
                tmp1.step++;
                vis[tmp1.x][tmp1.y]=1;
                q.push(tmp1);
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int cas=1; cas<=t; ++cas)
    {
        scanf("%d%d",&n,&m);
        int ax,ay,bx,by,cx,cy;
        for(int i=1; i<=n; ++i)
            for(int j=1; j<=m; ++j)
            {
                cin>>mp[i][j];
                if(mp[i][j]=='a')
                {
                    ax=i;
                    ay=j;
                }
                else if(mp[i][j]=='b')
                {
                    bx=i;
                    by=j;
                }
                else if(mp[i][j]=='c')
                {
                    cx=i;
                    cy=j;
                }
            }
        int ans;
        ans=max(bfs(ax,ay),max(bfs(bx,by),bfs(cx,cy)));
        printf("Case %d: %d\n",cas,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值