FZU-2150 Fire game__ Search

Source: Internet
Author: User

Exercises

Two children play in the grass in the n*m, two people choose a square with a grass to put a fire, fire can burn his top and bottom position, ask all the grass burned out for at least how long. If the burn is not finished, output "-1".

Place the first selected two points into the queue, set the two point time to 0, the other time all to INF, and use the array to save the smallest time of each point. The biggest time in the array is the time to burn all the grass. Finally, each of the two points is selected for enumeration, and the minimum value of all possible solutions is finally obtained.

Fat brother and Maze are playing a kind of special (hentai) game on a n*m board (N rows, M columns). At the beginning, each grid of this board are consisting of grass or just empty and then all the They. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing in time t, the grid which are adjacent to this grid would fire at time T+1 which to the Grid (x+1, y), (x-1, y), (x, Y+1), (x, y-1). This process ends as no new grid get fire. If then all the grid which are consisting of grass are get fired, Fat brother and Maze would stand in the middle of the the grid And playing a more special (hentai) game. (Maybe it ' s the Ooxx game which decrypted in the last problem, who knows.)

You can assume this grass in the board would never burn out and the empty grid would to get never.

The two grids they choose can be the same. Input

Then T cases Follow, each case contains two integers N and M indicate the size of the board. Then goes N line, and each line with M character shows the board. "#" indicates the grass. You can assume this there is at least one grid which is consisting to grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10 Output

For each case, output the case number I, if they can play the more special (Hentai) game (fire all grass), output The minimal time they need to wait after they set fire, otherwise just. The sample input and output for more details. Sample Input

4
3 3
.
###
.#.
3 3
. #.
#.#
.#.
3 3
...
#.#
...
3 3
###
. #
#.#
Sample Output
Case 1:1 case
2:-1 case
3:0 case
4:2
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #define INF 0x3f3f3f
using namespace Std;
int n,m;
Char map[15][15];
int time1[15][15];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
struct node{int x,y;
queue<node>q;
    int BFS (int x,int y,int Xx,int yy) {while (!q.empty ()) Q.pop ();
    memset (time1,inf,sizeof (time1));
    Node tt1,tt2;
    Tt1.x=x;
    Tt1.y=y;
    Tt2.x=xx;
    Tt2.y=yy;
    time1[tt1.x][tt1.y]=0;
    time1[tt2.x][tt2.y]=0;
    Q.push (TT1);
    Q.push (TT2);
        while (!q.empty ()) {node Temp=q.front ();
        Q.pop ();
        Node Temp1;
            for (int i=0;i<4;i++) {temp1.x=temp.x+dx[i];
            Temp1.y=temp.y+dy[i]; if (temp1.x>=0&&temp1.x<n&&temp1.y>=0&&temp1.y<m&&map[temp1.x][ temp1.y]== ' # ' &&time1[temp1.x][temp1.y]>time1[temp.x][temp.y]+1) {Time1[temp1.x][te Mp1.y]=time1[temp.x][temp.y]+1;
            Q.push (TEMP1);
    {}} int ans=0;
                for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (map[i][j]== ' # ') {
                Ans=max (Ans,time1[i][j]);
            cout<<ans<<endl;
}} return ans;
    int main () {int t;
    int time=1;
    cin>>t;
        while (t--) {cin>>n>>m;
            for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {cin>>map[i][j];
        } int Min1=inf,m1;
                    for (int i=0;i<n;i++) for (int j=0;j<m;j++) if (map[i][j]== ' # ') { for (int k=0;k<n;k++) for (int z=0;z<m;z++) if (map[k][z
              ]== ' # ') {M1=BFS (i,j,k,z);                  cout<<m1<<endl;
                            Min1=min (m1,min1);
        } cout<< "Case" <<time++<< ":";
        if (min1==inf) cout<< "-1" <<endl;
    else cout<<min1<<endl;
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.