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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章