First, the topic requirements
1. Returns the number of the largest connected Subarray in a two-dimensional integer array.
Second, design ideas
At the beginning to see this topic, think of it should be reduced dimension, and then a one-dimensional array to solve the problem more convenient, but always think of ways, and then under the guidance of the teacher, think of the discrete and data structures have learned about the map of the Unicom solution, the two-dimensional array as a graph to traverse to find the largest sub-array of
In this experiment, the teacher asked to read from the file and columns and array elements, we use fstream to achieve from the file read, one-time reading the rows and arrays of elements.
In this experiment, we failed to achieve the large number of signed 32-bit integers, and the completion of the experiment did not meet the requirements.
Third, the program code
1 //Pair development member; Zhangxiaofi Jangchul2 //The subject of this experiment is: to find the maximum connected Subarray in a two-dimensional array and3#include <iostream>4#include <ctime>5#include <fstream>6 using namespacestd;7 #defineN 1008 9typedefstructTen { One intArray[n]; A intCol[n][n]; - intCountnum; -}struct;//defining struct-body variables the - voidInput (Struct &num,intXinty) -{//input is used to implement reading rows and columns from a file and outputting them to the screen -Num.countnum = x*y; + inti =1; - intA, B; +Ifstreaminch= Ifstream ("Input.txt"); A inch>>A; at inch>>b; -Num.countnum = A *b; - while(inch>>Num.array[i]) -{//reads a number from an in file into an array -++i; - } in inch. Close ();//finished reading close file in - for(inti =1; I <= num.countnum; i++) to { +cout << Num.array[i] <<" "; - if(I%b = =0) the { *cout <<Endl; $ }Panax Notoginseng}//an array of output file imports to the screen - for(inti =1; I <= num.countnum; i + =y) the { + for(intj = i; J <= i + Y-2; J + +) A { theNum.col[j][j +1] =1; +Num.col[j +1][J] =1; - } $ } $ for(inti =1+ y; i<num.countnum; i + =y) - { - for(intj = i; J <= i + X-1; J + +) the { -NUM.COL[J][J-Y] =1;WuyiNUM.COL[J-Y][J] =1; the } - } Wu } - About voidTraverse (Struct &num,intVintVisit[],int&b,int&max,intx) ${//finding the largest connected subarray through the traversal of an array - intA =0,var=0; -VISIT[V] =1; -Max + =Num.array[v]; A if(Max >=b) + { theb =Max; - } $ for(intW =1; W <= Num.countnum; w++) the { the for(intc =1; C <= Num.countnum; C++) the { the if((visit[w] = =0) && (num.col[c][w] = =1) && (visit[c] = =1)) - { inA =W; the var=1; the Break; About } the } the if(var==1) the Break; + } - for(intW =1; W <= Num.countnum; w++) the {Bayi for(intc =1; C <= Num.countnum; C++) the { the if((visit[w] = =0) && (num.col[c][w] = =1) && (visit[c] = =1)) - { - if(num.array[a]<Num.array[w]) theA =W; the } the } the } - if(b + num.array[a]<0) the { theNum.col[v][a] =0; the }94 Else the Traverse (num, A, visit, B, max, x); the } the 98 About intMain () - {101 intx, y;102FStream FS ("Input.txt");103FS >>x;104FS >>y; thecout << x <<" "<< y <<Endl;106 Struct num;107 input (num, x, y);108 intv =1, b[n] = {0}, H =0;109 for(inti =1; I <= num.countnum; i++) the {111 if(num.array[i]<0) the {113B[i] =Num.array[i]; the } the Else the {117 intVisit[n] = {0 };118 intMax =0;119 Traverse (num, I, visit, B[i], Max, x); - }121 }122 123 intmax = b[1];124 for(inti =2; I <= num.countnum; i++) the {126 if(b[i]>max)127Max =B[i]; - }129cout <<"The number of the largest connected sub-arrays in the array is:"<< Max <<Endl; the}
Iv. Results of experimental operation
(1), input file
Screen display
Five, Time recording log
| Date |
Start time |
End time |
Interrupt Time (min) |
NET time (min) |
Activities |
Note |
March 28 Monday |
14:00 |
15:50 |
10 (recess) |
100 |
Lectures |
Software |
March 29 Tuesday |
20:00 |
22:00 |
0 |
120 |
Programming |
Arithmetic Web Edition |
March 30 Wednesday |
15:00 |
17:00 |
10 (rest) |
110 |
Programming |
Arithmetic Web Edition |
|
20:00 |
22:10 |
10 (rest) |
120 |
Programming |
Arithmetic Web Edition |
March 31 Thursday |
14:00 |
15:50 |
10 (recess) |
100 |
Lectures |
Software |
|
20:30 |
22:00 |
10 (rest) |
80 |
Programming |
Two-dimensional array 3 Maximum connected sub-array and |
April 2 Saturday |
14:00 |
17:00 |
20 |
160 |
Programming |
Arithmetic Web Edition |
April 3 Sunday |
14:30 |
18:00 |
30 |
180 |
Programming |
Arithmetic Web Edition |
April 4 Monday |
19:30 |
22:30 |
0 |
180 |
Programming |
Arithmetic Web Edition |
April 5 Tuesday |
8:00 |
10:00 |
0 |
120 |
Programming |
Two-dimensional array 3 The and of the maximum connected sub-array |
|
19:30 |
23:00 |
0 |
210 |
Programming |
Arithmetic Web Edition |
April 6 Wednesday |
14:20 |
17:10 |
0 |
150 |
Programming |
Arithmetic Web Edition Two-dimensional array 3 Write a blog |
Vi. Summary of the experiment
Through this experiment, I learned a lot of things, in the beginning of the experiment, we think about the group, convert it to a diagram of the method to solve the problem, for the knowledge previously learned is a good use of the process began in the use of the structure of the unfamiliar, the previous programming used less, and then access to the relevant information to achieve, In short, through this knot pair development experiment design, harvest a lot, learned a lot.
Array Exercise 3 to find the maximum number of connected sub-arrays and