Fzu 2150 Fire Game Technique BFS

Source: Internet
Author: User
Tags cas

Title: Fzu 2150 Fire Game


Give a m*n figure, ' # ' means lawn, '. ' Show the open space, and then you can choose to fire in any of the two lawn lattices, the fire every 1 s will spread to the surrounding four lattice, ask those two points to make burning all the lawn spend the least time.


Analysis: This topic is a bit difficult to think about, but given the small data range, we can violently enumerate the points where any lawn is located, and then press the two points into the queue to BFS to a minimum that satisfies the condition.

By the way, Fzu 2141 Sub-bipartite Graph's thinking, the game did not come out.

This topic to think of the complex, and then found others with greedy binary staining, each point of greed to choose with its point of color more than the opposite color, so it can be. It seems to be complicated at the time.


AC Code:

#include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <queue
> Using namespace std;
typedef long Long LL;
const int N = 15;
int dx[5] = {0,0,1,-1};
int dy[5] = {1,-1,0,0};
    struct Node {int x,y;
int cnt;
};
Vector<node> v;
Char Mp[n][n];
int vis[n][n];
int n,m;
    int BFS (Node a,node b) {memset (vis,0,sizeof (VIS));
    Queue<node> Q;
    VIS[A.X][A.Y] = vis[b.x][b.y] = 1;
    a.cnt = 0,b.cnt = 0;
    Q.push (a), Q.push (b);
    int ans = 0x3f3f3f3f;
    int cas = 1;
        while (!q.empty ()) {a = Q.front ();
        Q.pop ();
        printf ("%d%d%d\n", a.x,a.y,a.cnt);
        ans = a.cnt;
            for (int i = 0;i<4;i++) {b.x = a.x + dx[i];
            B.Y = A.y + dy[i];
            b.cnt = a.cnt + 1;
            printf ("b:%d%d%d\n", b.x,b.y,b.cnt); if (b.x>0 && b.y>0 && b.x<=n && b.y<=m && vis[b.x][b.y]==0 && mp[b.x][b.y]== ' # ') {vis[b.x][b.y] = 1;
            Q.push (b);
}} return ans; void print () {for (int i=1;i<=n;i++) {for (int j = 1;j<=m;j++) {printf ("%c")
        MP[I][J]);
    } puts ("");
    int main () {//freopen ("Input.txt", "R", stdin);
    int T;
    scanf ("%d", &t);
        for (int cas=1;cas<=t;cas++) {v.clear ();
        scanf ("%d%d", &n,&m);
            for (int i=1;i<=n;i++) {getchar ();
                for (int j=1;j<=m;j++) {scanf ("%c", &mp[i][j]);
            if (mp[i][j]== ' # ') V.push_back ((Node) {i,j,0});
        int ans = 0x3f3f3f3f; for (int i=0;i<v.size (), i++) {for (int j=i;j<v.size (), j + +) {int tmp
                = BFS (V[i],v[j]);
                bool OK = true;
  for (int k = 1;k<=n;k++)              {for (int f = 1;f<=m;f++) {if (vis[k][f]
                            = = 0 && mp[k][f]== ' # ') {OK = false;
                        Break
                } if (ok==false) break;
                if (ok) {ans = min (ans,tmp);
        } printf ("Case%d:", CAs);
        if (ans = = 0x3f3f3f3f) puts ("-1");
    else printf ("%d\n", 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.