Hdu 4101 Ali and Baba "bfs good Problem"

Source: Internet
Author: User

Ali and Baba Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2172 Accepted Submission (s): 460


Problem Descriptionthere is a rectangle area (with N rows and M columns) in front of Ali and Baba, each grid might be one of the following:
1. Empty area, represented by an integer 0.
2. A Stone, represented by an integer x (x > 0) which denote the HP of this Stone.
3. Treasure, represented by an integer-1.
Now, Ali and Baba get the map of this mysterious area, and play the following game:
Ali and Baba play alternately, with Ali starting. In each turn, the player would choose a stone that he can touch and hit it. After this operation, the HP of the stone that been hits would decrease by 1. If some stone ' s HP is decreased to 0, it'll become an empty area. Here, a player can touch a stone means
There is path consist's empty area from the outside to the stone. Note that the grids is adjacent if and only if the they share an edge.
The player who hits the treasure first wins the game.
Inputthe input consists several testcases.
The first line contains both integer N and M (0 < N,m <=), the size of the maze.
The following N lines each contains M integers (less than), describes the maze, where a positive integer represents th e HP of a stone, 0 reperents an empty area, and-1 reperents the treasure.
There is a grid contains the treasure in the maze.
Output "Ali win" or "Baba win" indicates the winner of the game.
Sample INPUT3 31 1 11-1 11 1 1
Sample Outputbaba Win
Source2011 Alibaba-cup Campus Contest

The main topic: give you a n*m figure, the figure has three kinds of values, respectively, represent:

1, 0 stands for the open space

2, 1 represents treasure

3, 1-100 represents the blood value of the stone.

The rules for the game are as follows: Who gets the treasure first? Who wins, Ali initiator, for Ali and Baba, can only go up and down in four directions, each round must attack a stone and let it reduce a blood volume, that is, if there is a way from the edge arbitrarily (x==1| | x==n| | y==1| | Y==M), if you can get to-1, then it is the equivalent of winning.


Ideas:

(This problem is really need to multi-faceted multiple perspectives to consider a problem, really is very good very good very good very good a problem.) )


1, first, if there is a way to start from the edge, there is a direct path can be guaranteed to go directly to the treasure without encountering stones, because Ali initiator, so Ali in this case won, do not need to continue to judge. This problem we can directly from the treasure to start wide search, if go to the edge, is to meet the situation, this part is better to achieve.

2.

If we don't meet the situation we just told you, we need to keep thinking.

Our purpose is very simple, the goal is very clear, that is to find some stones, so that after the stone to kill the rest of the stone inside to kill any one will make a case of Victory . In detail, for the example, the points (stones) that need to be found (1,3) (3,1) (3,3), and then the parity of these points to determine. If it is an even number, then Baba will win, or Ali will win.

Then we will continue to think about this goal.

We need to find these points of interest, first of all to define a boundary, which is the closest stone circle to the treasure.

For example, this example:

7 71 1 1 1 1 1 11 1 0 0 0 0 11 0 1 1 1 0 11 0 1-1 1 0 11 0 1 1 1 0 11 0 0 0 0 0 11 1 1 1 1 1 1
There are two stone rings in the picture, and the stone circle we are looking for is the most inner circle, and the most out-of-the-place ones are the target points.
Spicy is not difficult to understand: These target points are outside the bounds of the point, so if there are stones within the boundaries, these stones are irrelevant stones

As in this example:

7 71 1 1 1 1 1 11 1 0 0 0 0 11 0 0 1 1 0 11 0 1 1 3 0 11 0 0 1 0 0 11 0 0-1 0 0 11 1 1 1 1 1 1

The six points on the top of the obvious treasure are irrelevant points that have just been said.

So we can identify three kinds of stone points and a boundary.

Three kinds of stone points:

One is the point of the stone on the boundary (some of the boundaries of the stone also belong to the target point), one is the boundary outside the point of the stone, which is what we call the target point, another kind of stone point, is the boundary of irrelevant points.

Determined the general content, we also clear the general direction of the problem: to find the boundaries, throw away irrelevant points, plus and target points.

Target point = outside the bounds of the stone point of blood value plus and + bounds on the stone point greater than 1 of the section,


Speaking here may everyone's ideas are relatively clear, here again comprehensive write:
First to see if the Ali can directly find the treasure, if you can, direct exit, if not, continue to judge, continue to judge the goal: to find the target stone points, so that the last remaining stone circle will be the treasure of the whole good surrounding, and this time the point on the boundary circle of the stone blood value is 1. So target point = outside the bounds of the stone point blood value Plus and + bounds on the stone point greater than 1 part


Then we split the code to write:

1, determine the boundary circle, and determine whether the initiator of Ali can directly find the treasure:

void Bfs (int x,int y) {    memset (vis,0,sizeof (Vis));    now.x=x;    now.y=y;    Vis[x][y]=1;    Queue<zuobiao >s;    S.push (now);    while (!s.empty ())    {        now=s.front ();        if (now.x==1| | now.x==n| | now.y==1| | NOW.Y==M)//If the initiator Ali can directly        find            the treasure {ok=1;//mark on            return;        }        S.pop ();        for (int i=0;i<4;i++)        {            nex.x=now.x+fx[i];            Nex.y=now.y+fy[i];            if (nex.x>=1&&nex.x<=n&&nex.y>=1&&nex.y<=m&&vis[nex.x][nex.y]==0)// If within the range of the walk, and do not repeat            {                vis[nex.x][nex.y]=1;//marks on these points are traversed, that is, these points belong to the boundary circle, including the boundary Circle                if (a[nex.x][nex.y]==0)// If you go to the point of 0, you can continue to go.                {                    s.push (NEX);    }}}} return;}

In this way, the =1 point of the Vis "x" "Y" is the point in the boundary circle, and also determines the boundary circle,

2, the target point of statistical value Plus and:

Note Here one point: do not add more than once to the points on the boundary. Points outside the bounds must be marked completely.

void Bfs2 (int x,int y)//<span style= "font-family:arial, Helvetica, Sans-serif;"    > An implicit figure </span>{memset (vis2,0,sizeof (VIS2)) with a circle value of 0 on the outside of the diagram;    Now.x=x;    Now.y=y;    Queue<zuobiao >s;    S.push (now);    Vis2[x][y]=1;        while (!s.empty ()) {Now=s.front ();        S.pop ();            for (int i=0;i<4;i++) {nex.x=now.x+fx[i];            Nex.y=now.y+fy[i]; if (nex.x>=0&&nex.x<=n+1&&nex.y>=0&&nex.y<=m+1&&vis2[nex.x][nex.y]= =0&&vis2[nex.x][nex.y]!=2)//can walk in the range, and do not repeat a point, also note that one point is not to repeat the point on the boundary circle {if (vis[nex.x][ne                    x.y]==0)//The point outside the boundary ring {vis2[nex.x][nex.y]=1;                S.push (NEX);                    } if (vis[nex.x][nex.y]==1)//The point on the boundary circle {vis2[nex.x][nex.y]=2; sum+=a[nex.x][nex.y]-1;//this piece of special treatment, so that the point of the stone on the threshold of the amount of blood to 1,}}}} for (int i=0;i<=n+1;i++) {for (int j=0;j<=m+1;j++) {if (vis2[i][j]==1) sum+=a[i][j];//points outside the bounds are added and }    }}

Finally, the complete AC code:

#include <stdio.h> #include <queue> #include <string.h>using namespace std;struct zuobiao{int x, y;} Now,nex;int a[505][505];int vis[505][505];int vis2[505][505];int fx[4]={0,0,1,-1};int fy[4]={1,-1,0,0};int N,m,ok,    Sum;void Bfs (int x,int y) {memset (vis,0,sizeof (VIS));    Now.x=x;    Now.y=y;    Vis[x][y]=1;    Queue<zuobiao >s;    S.push (now);        while (!s.empty ()) {Now=s.front (); if (now.x==1| | now.x==n| | now.y==1| |            now.y==m) {ok=1;        return;        } s.pop ();            for (int i=0;i<4;i++) {nex.x=now.x+fx[i];            Nex.y=now.y+fy[i];                if (nex.x>=1&&nex.x<=n&&nex.y>=1&&nex.y<=m&&vis[nex.x][nex.y]==0) {                Vis[nex.x][nex.y]=1;                if (a[nex.x][nex.y]==0) {s.push (NEX); }}}} return;} void Bfs2 (int x,int y) {memset (vis2,0,sizeof (vIS2));    Now.x=x;    Now.y=y;    Queue<zuobiao >s;    S.push (now);    Vis2[x][y]=1;        while (!s.empty ()) {Now=s.front ();        S.pop ();            for (int i=0;i<4;i++) {nex.x=now.x+fx[i];            Nex.y=now.y+fy[i]; if (nex.x>=0&&nex.x<=n+1&&nex.y>=0&&nex.y<=m+1&&vis2[nex.x][nex.y]=                    =0&&vis2[nex.x][nex.y]!=2) {if (vis[nex.x][nex.y]==0) {                    Vis2[nex.x][nex.y]=1;                S.push (NEX);                    } if (vis[nex.x][nex.y]==1) {vis2[nex.x][nex.y]=2;                Sum+=a[nex.x][nex.y]-1; }}}} for (int i=0;i<=n+1;i++) {for (int j=0;j<=m+1;j++) {if (V        is2[i][j]==1) Sum+=a[i][j];        }}}int Main () {while (~scanf ("%d%d", &n,&m)) {int sx,sy;       memset (A,0,sizeof (a)); for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {scanf ("%d", &a[i][j]                );                    if (a[i][j]==-1) {sx=i;                Sy=j;        }}} ok=0;        sum=0;        Bfs (Sx,sy);            if (ok==1) {printf ("Ali win\n");        Continue        } BFS2 (0,0);        if (sum%2==0) {printf ("Baba win\n");    } else printf ("Ali win\n"); }}











Hdu 4101 Ali and Baba "bfs good Problem"

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.