[JZOJ] 1341. Water (water)

Subject to the effect 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 place, 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, a total number of pumps to be?

This question can be used to do a search, why?

We can see the town as a map view * m the n-** map, I know the depth of each grid, and I'm looking for that one of the deepest, can only use up and down to find

We can be transformed into a map the size of a piece of n * m (consisting of squares), find the largest of the grid.

So we can think of using dfs, but not entirely dfs

We can use an array to hold the state, s [i] [j], if this is not a grid marked (that is, not passed), and this grid within my being a map of the range, we will be able to perform dfs function of the final output ans

In dfs we first use the function s [i] [j] to save the current state, and then further searches, we have to move up and down four directions, save the new cycle x, Y; then, after determining a new x and whether y again within the scope of our s array is labeled, and then determines whether the current position is left in situ (very important to have to leave in situ), then after that we have to get the new x and y, call , do mark (remember to clear, mark put before the cycle)

In fact, this dfs like recursion, but like a dfs. . . .

This question is so completed;

The bar code

 1 include<iostream>
 2 include<cstdio>
 3 include<cmath>
 4 include<algorithm>
 5 include<cstring>
 6 using namespace std;
 7 
 8 const int maxn= 53;
 9 
10 int N, M;
11 
12 char maze[maxn][maxn];
13 
14 char s[maxn][maxn];
15 
16 int xi[] = {0, 0, -1, 1};
17 
18 int yi[] = {1, -1, 0, 0};
19 
20 void dfs(int x, int y) { s[x][y]=1; for(int i=0; i<4; i++) {
21 
22     int nx=x+xi[i]; 
23 
24     int ny=y+yi[i]; 
25 
26     if(nx>=0&&nx<N&& ny>=0&&ny<M&&s[nx][ny]==0&&maze[nx[ny]>=maze[x][y]) {
27          DFS (NX, NY);
 28  
29      }
 30  
31 is  }
 32 } // a very simple search 
33 is  
34 is  int main () {
 35  
36  int T = 0 ;
 37 [  
38 is CIN >> T;
 39  
40  the while (T- - ) {
 41 is  
42 is      CIN >> N >> M;
 43 is  
44 is      for ( int I = 0 ; I <N; I ++ ) {
 45  
46 is          CIN >> Maze [I];
 47  
48      }
49 
50     memset(s,0,sizeof(s));
51 
52     int ans=0;
53 
54     for(char c='a';c<='z';c++) {
55 
56         for(int i = 0; i < N; i++) {
57 
58             for(int j = 0; j < M; j++) {
59 
60                 if(s[i][j] == 0 && maze[i][j] == c){
61 
62                     dfs(i, j);
63 
64                     ans++;
65 
66                 }
67 
68             }
69 
70         }
71 
72     }
73 
74     cout<<ans<<endl;
75     return 0;
76 }

 

Guess you like

Origin www.cnblogs.com/WestJackson/p/11343622.html