17: The best grass, the best grass

Source: Internet
Author: User

17: The best grass, the best grass
17: The best grass

  • View
  • Submit
  • Statistics
  • Question
Total time limit:
10000 ms
 
Time limit for a single test point:
1000 ms
 
Memory limit:
65536kB
Description

Bessie, the cow, plans to enjoy the soft spring grass. The grass is distributed in the farm of column C in the R row. It wants to calculate the number of grass in the pasture.

In a farm map, each grass is either a single "#" or two adjacent "#" with a public edge. Calculate the number of grass trees in a given farm map.

For example, consider the farm map with 5 rows and 6 Columns

.#....
..#...
..#..#
...##.
.#....

This farm has five grass trees: one in the first row, one in the second column across the second and third rows, one in the third row, and the other in the fourth row across the fourth and fifth columns, the last one is on the fifth line.

 

Input
The first line contains two integers, R and C, separated by a single space.
Next, the R Line describes the farm map with C characters in each line. There are only two types of characters: "#" and. (1 <= R, C <= 100)
Output
Output an integer to indicate the number of grass.
Sample Input
5 6.#......#.....#..#...##..#....
Sample output
5
Source
USACO Open 2008 Bronze

 

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 using namespace std; 5 char a[1001][1001]; 6 int now=1; 7 int m_tot=0; 8 int z_tot=0; 9 int ans=0;10 int main() 11 {12     int n,m;13     cin>>n>>m;14     for(int i=0;i<n;i++)15     {16         for(int j=0;j<m;j++)17         {18             cin>>a[i][j];19         }20     }21     for(int i=0;i<n;i++)22     {23         for(int j=0;j<m;j++)24         {25             if(a[i][j]=='#')26             {27                 if(a[i+1][j]=='#')28                 {29                     a[i][j]='.';30                     a[i+1][j]='.';31                     ans++;32                 }33                 else if(a[i-1][j]=='#')34                 {35                     a[i][j]='.';36                     a[i+1][j]='.';37                     ans++;38                 }39                 else if(a[i][j+1]=='#')40                 {41                     a[i][j]='.';42                     a[i][j+1]='.';43                     ans++;44                 }45                 else if(a[i][j-1]=='#')46                 {47                     a[i][j]='.';48                     a[i+1][j]='.';49                     ans++;50                 }51                 else52                 {53                     a[i][j]='.';54                     ans++;55                 }56             }57         }58     }59     cout<<ans;60     return 0;61 }

 

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.