P1123 fetch games (Los Valley)

Title Description

Of N × M a digital matrix of a non-negative integers, you need to remove a number of digits therein, such that any two non-adjacent numbers removed (if a number adjacent another digit i.e. a grid of eight think these two figures adjacent), seek out the largest digital and how much.

Input Format

Line T 1 has a positive integer, T represents the set of data there.

For each set of data, the first row there are two positive integers N and M, represents a digital matrix of N rows M columns.

Next N lines of M non-negative integer, describes this digital matrix.

Output Format

T lines, each a non-negative integer, the output obtained answers.

Sample input and output

Input # 1
3
4 4
67 75 63 10
29 29 92 14
21 68 71 56
8 67 91 25
2 3
87 70 85
10 3 17
3 3
1 1 1
1 99 1
1 1 1

Output # 1

271
172
99

Problem-solving ideas:

  Directly dfs will TLE's (because I only ran directly through the 4 test sample), usually dfs we will use to access the for loop to change up and down around the four elements of position, but in this question, dfs will be put in for TLE. Thus there is a need for more efficient movement method: Here, we are each move default y + 1; then when y> m exceeds the number of the current column, we set y = 1 (first column) and x + 1 . Seen in this light, this movement is fixed, and we can know the specific location of its next step in this process to ensure that all positions are traversed. Mobile termination condition is x> return when n exceeds the number of rows.

  For each position we have taken and no values ​​in both cases (thus covers all cases combined), from the first position of the search: take and not to take before each time dfs. In dfs in an element is reached, he has two states: can take and not take, can be taken for: then take time, take over flag of 1 indicates, in dfs; 0 then back flag, whichever values ​​do not directly dfs , can not be taken for (around indicates element has been taken off): direct dfs.

  Note the point: There should not be found in a certain position, whichever value is immediately after the elements around it to mark, and this will bring very high complexity. DFS ( X, Y, SUM) represents the state where the access to the xy position but also added at this time whether the case is handled well. So the idea is the following code should have access to an element, first find it to be moved to the next location, go to determine whether the position to take on its value, if the next two dfs, if not there is a next dfs.

 

Code:

. 1 #include <the iostream>
 2 #include <CString>
 . 3  the using  namespace STD;
 . 4  
. 5  int n-, m;
 . 6  int A [ 10 ] [ 10 ] = { 0 };
 . 7  int Map [ 10 ] [ 10 ] = { 0 };
 . 8  
. 9  int Maxx = 0 ;     // used to store the maximum value of 
10  
. 11  
12 is  void DFS ( int X, int Y, int SUM) {         //x represents the row and y represents the column sum of the current value 
13 is   
14      Maxx = max (Maxx, sum);
 15      
16      // Number 1 until the maximum number of columns plus regular mobile columns, the number of columns becomes 1 at this time, the number of rows + + 
. 17      int My = Y + . 1 ;
 18 is      int MX = X;
 . 19      IF (My> m) {
 20 is          My = . 1 ;
 21 is          MX = X + . 1 ;
 22 is      } 
 23 is      IF (MX> n-)     return ;         // recursive termination conditions 
24      
25      
26 is      IF (! Map [MX- . 1 ] [MY- . 1]&&!map[mx][my-1]&&!map[mx+1][my-1]&&!map[mx-1][my]&&!map[mx+1][my]&&!map[mx-1][my+1]&&!map[mx][my+1]&&!map[mx+1][my+1]){
27         map[mx][my]=1;
28         dfs(mx,my,sum+a[mx][my]);
29         map[mx][my]=0;
30     }
31     
32     dfs(mx,my,sum);
33     
34     return;
35 }
36 int main(){
37     int t;
38     cin>>t;
39     
40     for(int i=1;i<=t;i++){
41         n=m=0;
42         cin>>n>>m;
43         memset(a,0,sizeof(a));
44         maxx=0;
45         
46         for(int i=1;i<=n;i++){
47             for(int j=1;j<=m;j++){
48                 cin>>a[i][j];
49             }
50         } 
51 
52         memset(map,0,sizeof(map));
53         dfs(1,1,0);
54         memset(map,0,sizeof(map));
55         map[1][1]=1;
56         dfs(1,1,a[1][1]);
57         cout<<maxx<<endl;
58         
59     }
60     return 0;
61 }

 

 

Guess you like

Origin www.cnblogs.com/xwh-blogs/p/12549799.html