Fzu 2150 Fire Game BFS Solution

Source: Internet
Author: User

Title Link: http://acm.fzu.edu.cn/problem.php?pid=2150

Test instructions: Given a lawn, two people light a haystack (which can be the same), asking for the minimum time when all the haystack is on fire.

Talk about the problem is also a bit sad, tle several times, read the other person's problem and then write, WA a good many times, tonight to see a good blog to let me inspiration, and then smooth AC.

This blog is: http://m.blog.csdn.net/blog/zzzz40/38122385, I was just about to see the next inspiration, haha.


The so-called inspiration: probably looked at the blog, found that he is also a violent enumeration of two points, why he did not tle it. Look carefully, people violent than I am a little more beautiful, beautiful is to put:

for (int i=0;i< (n*m), i++) for (int j=0;j< (n*m); j + +) instead: for (int i=0;i< (n*m;i++) for (int j=i;j< (n*m); j + +).

In this way, the calculation of the original 1/2, you may also say that the complexity of the time is not O ((n*m) ^2) it. But in fact this is not the case after the tle. The question is why this can be done without tle. See a blog said, this topic pure violence words time complexity will reach 10^8, I am also doubtful, after all, the calculation of time complexity is not very clear. My calculation of the time complexity of this problem is: Obviously the time complexity of the two for Loop is O ((m*n) ^2) =10^4. Then calculate the time complexity of the BFS: obviously each layer has a for loop, the loop volume is 4, the maximum layer is diagonal, 10 layers, so the total complexity of 4^10, in the front of the complexity, obviously timed out. But this is not the case, we can think, by the shape of the rectangle can be inferred, in fact, the upper left and right corner of the calculation should be an order of magnitude, why say so. Because the two are symmetrical, after screening, the amount of computation left should be equal. So the actual number of layers should be 5 layers. But the first layer has been initialized as the queue, the cycle volume of 0, only 4 layers, so the total time complexity is: 10^4* (4^4). About: 2.5*10^6. Therefore, in time into the original 1/2 (500ms+), will be AC, of course, my computer 1s calculation is not very clear, from the problem can be estimated, probably also 10^6 or 10^7 order of magnitude, 10^8 should be tle. In fact, at first I also feel that should not tle ah, unfortunately, I am not violent enough beautiful ....

Also, since they are so violent, the number of connected blocks becomes meaningless.

Code:

#include <iostream> #include <sstream> #include <cstring> #include <cstdio> #include <cmath
> #include <algorithm> #include <queue> #include <set> #include <map> #include <vector>
#include <string> #define LL __int64 #define INF 0x7fffffff using namespace std;

struct node{int x,y,step;};
Char map[11][11];
int vis[11][11];

int n,m;

int d[][2]={1,0,-1,0,0,1,0,-1};
    int BFS (int x,int y,int xx,int yy) {memset (vis,0,sizeof (VIS));
    Vis[x][y]=vis[xx][yy]=1;
    Queue<node> Q;
    Q.push (node) {x,y,0});
    Q.push (node) {xx,yy,0});
    int ans=-1; while (!
        Q.empty ()) {node Tp=q.front ();
        Ans=max (Ans,tp.step);
        Q.pop ();
            for (int p=0;p<4;p++) {int tx=tp.x+d[p][0],ty=tp.y+d[p][1];
            if (tx<0| | ty<0 | | tx>=n | | ty>=m) continue;
            if (map[tx][ty]== '. ' | | vis[tx][ty]) continue;
            Q.push (node) {tx,ty,tp.step+1});
 Vis[tx][ty]=1;       }} for (int i=0;i<n;i++) for (int j=0;j<m;j++) if (map[i][j]== ' # ' && vis[i][j]==0) Retu
    RN INF;
return ans;
    } int main () {//freopen ("D:\\in.txt", "R", stdin);
    int t;cin>>t;
        for (int kase=1;kase<=t;kase++) {cin>>n>>m;
        for (int i=0;i<n;i++) for (int j=0;j<m;j++) cin>>map[i][j];
        int ans=inf; for (int i=0;i<n*m;i++) for (int j=i;j<n*m;j++) if (map[i/m][i%m]== ' # ' && map[j/m][
        j%m]== ' # ') ans=min (BFS (i/m,i%m,j/m,j%m), ans);
    printf ("Case%d:%d\n", Kase,ans==inf -1:ans);
} 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.