JZ junior OJ 1341. [2009 Nanhai junior] water

Time limit: 1000 ms space constraints: 131072 KB specific restrictions  

Title Description

Global warming, A small town facing floods. So you have to buy some pump the water pumped. Pumping capacity of the pump can be considered infinite, but you have to pump in the right position to make all of the water flow in the pump. The town can be considered as N * M matrix. Each cell in the matrix is ​​a 'a'-' z 'lowercase letters, small letters indicate the height of the lattice, large letters indicate the cell is relatively high, on the contrary, represents the height of the grid is relatively low. Current cell water can flow up, down, left, and right grid, but must meet these lattices are highly height of less than or equal to the current cell. Now, to give you some N * M matrix, the number of pump you buy at least, to put all the lattice water can be pumped?

 

Entry

 Multiple sets of test data.

 First line: K, K represents the set of test data there. 1 <= K <= 5.

 Then there are K sets of test data, each set of test data in the following format:

     The first line: two positive numbers, N, M 1 <= N, M <= 50, represents the size of the town.

     Then there are N lines, each line has M lowercase letters, represent the town map.

Export

A total of K rows, each row corresponds to a set of data. How much to buy at least one pump to the water grid can all take away.

 

Sample input

 

Sample Output

 
 

Data range limit

 
 

prompt

 















Entry  

2

 5 5

 ccccc

 cbbbc

 cbabc

 cbbbc

 ccccc

 4 9

 cbabcbabc

 cbabcbabc

 cbabcbabc

 cbabcbabc

1

 11   11

 ccccccccccc

 Chaaaaac

 Chaaaaac

 caazpppzaac

 caapdddpaac

 caapdddpaac

 caapdddpaac

 caazpppzaac

 Chaaaaac

 Chaaaaac

 ccccccccccc

Export

1

2

 2
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n,m;
 4 char a[53][53];
 5 bool bj[53][53];
 6 int xi[]={0,0,-1,1};
 7 int yi[]={1,-1,0,0};
 8 void dfs(int x,int y)
 9 {
10     bj[x][y]=true;
11     for(int i=0;i<4;i++)
12     {
13         int x1=x+xi[i];
14         int y1=y+yi[i];
15         if(x1<=n && x1>0 && y1<=m && y1>0 && bj[x1][y1]==false && a[x1][y1]>=a[x][y]){
16         dfs(x1,y1);
17     }
18 }}
19 int main()
20 {
21     int k=0;
22     cin>>k;
23     while(k--)
24     {
25         memset(bj,false,sizeof bj);
26         cin>>n>>m;
27         for(int i=1;i<=n;i++)
28             {
29                 for(int j=1;j<=m;j++)
30                 cin>>a[i][j];
31             }
32         int ans=0;
33         for(char c='a';c<='z';c++){
34             for(int i=1;i<=n;i++){
35                 for(int j=1;j<=m;j++){
36                     if(bj[i][j]==false && a[i][j]==c)
37                         {
38                             dfs(i,j);
39                             ans++;
40                         }
41                     }
42                 }
43             }
44         cout<<ans<<endl;
45     }
46 }

Guess you like

Origin www.cnblogs.com/anbujingying/p/11305646.html