Invasion of tyvj 1030 milk grass (BFS)

Source: Internet
Author: User
Background backgroundusaco oct09 6th describe descriptionfarmer John has been trying to fill his grass with delicious, juicy, healthy pasture. It is a pity that the sky is not from the hope of others, he defeated in the plant war. The evil Beaver has taken a place in the northwest part of his farm.
As usual, the lawn is split into a right-angle grid with a height of Y (1 <= Y <= 100) and a width of x (1 <= x <= 100. () Is the lattice in the lower left corner (that is, the coordinate layout is the same as the normal X and Y coordinates ). At the beginning, ruicao received the lattice (MX, my ). Every week, the milk grass spreads to the grids that have been taken by the milk grass. in all directions, each grid without many stones (including vertical and horizontal adjacent and diagonal adjacent grids ). One week later, the neighborhood can be spread to more cells.
Bessie wants to enjoy all the pasture as much as possible before the grass is completely covered by milk grass. She was curious about how long it would take to pull the entire lawn. If the grass is located at zero point in the lattice (MX, my), then at that point they can completely hack into the entire lawn (it always happens to the given data )?
The lawn is represented by an image. "." Indicates the grass, and "*" indicates the big stone. For example, x = 4, y = 3.

....
..*.
.**.

If the grass is in the lower left corner (1st rows, 1st columns) at the beginning, the map of the grass will develop as follows:

... Mmm. Mmmm
.. *. Mm * m mm * m
M **. m **. m ** m
Number of weeks 0 1 2 3 4

Milk grass will collect the whole land in four weeks. Input Format: inputformat * First line: Four integers separated by spaces: X, Y, MX, my
* Row 2nd to row y + 1: The Y + 1 line of the data is composed of X characters ("." indicates the lawn, "*" indicates the dashstone), describing the lawn
(Y + 2-y. Output Format outputformat * The first line: A separate integer indicates the number of weeks in which the last lattice, not a big rock, is taken by the grass. Sample input sampleinput

4 3 1 1
....
..*.
.**.

 

Sample output sampleoutput 4 the idea is BFs. Pay attention to the input. First input the column and then input the row. The idea at the beginning was to determine whether all the roads were occupied by milk grass. If all the roads were occupied, the results would be output. (Next. Step is output when the node is output for the week, because you have extended the node and determined whether you will be fully occupied in the next week)
 1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 #include<stdlib.h> 5 using namespace std; 6 char a[105][105]; 7 int vis[105][105]; 8 int dir[8][2]={{-1,0},{0,1},{1,0},{0,-1},{-1,1},{1,1},{1,-1},{-1,-1}}; 9 int n,m,sx,sy,day,cnt,sum,num;10 struct node11 {12     int x,y;13     int step;14 }star;15 void BFS()16 {17     memset(vis,0,sizeof(vis));18     star.x=sx;19     star.y=sy;20     star.step=0;21     queue<node> q;22     q.push(star);23     vis[star.x][star.y]=1;24     while(!q.empty())25     {26         node now,next;27         now=q.front();28         q.pop();29         for(int i=0;i<8;i++)30         {31             next.x=now.x+dir[i][0];32             next.y=now.y+dir[i][1];33             if(0<=next.x&&next.x<n&&0<=next.y&&next.y<m&&!vis[next.x][next.y]&&a[next.x][next.y]!=‘*‘)34             {35                  next.step=now.step+1;36                  q.push(next);37                  vis[next.x][next.y]=1;38                  cnt++;39             }40         }41         if(cnt==sum)42         {43             printf("%d\n",next.step);44             return ;45         }46     }47     return ;48 }49 int main()50 {51     day=0,cnt=1,num=0;52     scanf("%d %d %d %d",&m,&n,&sy,&sx);53     for(int i=0;i<n;i++)54         scanf("%s",a[i]);55     star.x=sx--;56     star.y=sy--;57     for(int i=0;i<n;i++)58         for(int j=0;j<m;j++)59             if(a[i][j]==‘*‘)60                 num++;61 62     sum=n*m-num;63     BFS();64     //printf("%d %d\n",sum,num);65    // printf("%d\n",day);66    //printf("%d\n",cnt);67     return 0;68 }
View code

 

Later I found another way to record the week
 1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 #include<stdlib.h> 5 using namespace std; 6 char a[105][105]; 7 int vis[105][105]; 8 int dir[8][2]={{-1,0},{0,1},{1,0},{0,-1},{-1,1},{1,1},{1,-1},{-1,-1}}; 9 int n,m,sx,sy,day;10 struct node11 {12     int x,y;13     int step;14 }star;15 void BFS()16 {17     memset(vis,0,sizeof(vis));18     star.x=sx;19     star.y=sy;20     star.step=0;21     queue<node> q;22     q.push(star);23     vis[star.x][star.y]=1;24     while(!q.empty())25     {26         node now,next;27         now=q.front();28         q.pop();29         for(int i=0;i<8;i++)30         {31             next.x=now.x+dir[i][0];32             next.y=now.y+dir[i][1];33             if(0<=next.x&&next.x<n&&0<=next.y&&next.y<m&&!vis[next.x][next.y]&&a[next.x][next.y]!=‘*‘)34             {35                  next.step=now.step+1;36                  q.push(next);37                  vis[next.x][next.y]=1;38                  if(day<next.step)39                     day=next.step;40             }41         }42     }43     return ;44 }45 int main()46 {47     day=0;48     scanf("%d %d %d %d",&m,&n,&sy,&sx);49     for(int i=0;i<n;i++)50         scanf("%s",a[i]);51     star.x=sx--;52     star.y=sy--;53     BFS();54     printf("%d\n",day);55     return 0;56 }
View code

 

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.