Returns the and of the largest interconnected subarray in a two-dimensional integer array
Last Update:2015-06-22
Source: Internet
Author: User
<span id="Label3"></p><p><p>Requirements:</p></p><p><p>Enter a two-dimensional shaping array with positive numbers in the array and a negative number.</p></p><p><p>A two-dimensional array is connected to the end of the line, like a Belt.</p></p><p><p>One or more consecutive integers in an array make up a sub-array, each of which has a and.</p></p><p><p>The maximum value for the and of all Sub-arrays.</p></p><p><p>"turn" Design Ideas:</p></p><p><p>The two-dimensional array of n*m is decomposed, decomposed into N one-dimensional arrays, and then the largest subarray of n one-dimensional arrays and note the subscript of the largest one-dimensional sub-array of each row, such as 2-5, which is divided into two cases the first is the largest sub-array between rows is connected, such as the first row is 2-5, the second row is 3-6, This is directly added to the LINE. The second is not connected, such as the first row is 2-5, the second row is 6-7, this time the largest sub-array of each row as a whole, and then make each of the largest array of blocks connected, so that the minimum cost of its connection. finally, we can find out the maximum number of interconnected sub-arrays.</p></p><p><p></p></p><p><p>"go" Source Code:</p></p><pre>#include <iostream><span style="color: #0000ff;"><span style="color: #0000ff;">using</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">namespace</span></span><span style="color: #000000;"><span style="color: #000000;">std;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Zuida (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>N<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>a[],<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>*sm,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>*<span style="color: #000000;"><span style="color: #000000;">mm);</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">main () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">m,n,i,j,sm,mm,t2; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">sum,max; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>up[<span style="color: #800080;"><span style="color: #800080;"></span> -</span>],down[<span style="color: #800080;"><span style="color: #800080;"></span> -</span>],t[<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>a[<span style="color: #800080;"><span style="color: #800080;"></span> -</span>][<span style="color: #800080;"><span style="color: #800080;"></span> -</span>],b[<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">]; cout</span></span><<<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">enter rows for two-dimensional arrays</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><<<span style="color: #000000;"><span style="color: #000000;">endl; CIN</span></span>>><span style="color: #000000;"><span style="color: #000000;">m; cout</span></span><<<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">enter rows for two-dimensional arrays</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><<<span style="color: #000000;"><span style="color: #000000;">endl; CIN</span></span>>><span style="color: #000000;"><span style="color: #000000;">n; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(i=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; i<m;i++<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(j=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; j<n;j++<span style="color: #000000;"><span style="color: #000000;">) {cin</span></span>>><span style="color: #000000;"><span style="color: #000000;">a[i][j]; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(i=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; i<m;i++<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(j=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; j<n;j++<span style="color: #000000;"><span style="color: #000000;">) {b[j]</span></span>=<span style="color: #000000;"><span style="color: #000000;">a[i][j]; } Sum</span></span>=zuida (n,b,&sm,&<span style="color: #000000;"><span style="color: #000000;">mm); up[i]</span></span>=<span style="color: #000000;"><span style="color: #000000;">sma; down[i]</span></span>=<span style="color: #000000;"><span style="color: #000000;">mm; t[i]</span></span>=<span style="color: #000000;"><span style="color: #000000;">sum; } T2</span></span>=t[<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(i=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; i+<span style="color: #800080;"><span style="color: #800080;">1</span></span><m;i++<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(up[i]<=down[i+<span style="color: #800080;"><span style="color: #800080;">1</span></span>] && down[i]>=up[i+<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">]) {t2</span></span>+=t[i+<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(j=up[i];j<up[i+<span style="color: #800080;"><span style="color: #800080;">1</span></span>];j++<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(a[i+<span style="color: #800080;"><span style="color: #800080;">1</span></span>][j]><span style="color: #800080;"><span style="color: #800080;">0</span></span>) t2+=a[i+<span style="color: #800080;"><span style="color: #800080;">1</span></span>][j];<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">distinguishing independent positive numbers</span></span><span style="color: #000000;"><span style="color: #000000;">}} cout</span></span><<t2<<<span style="color: #000000;"><span style="color: #000000;">endl;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Zuida (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>N<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>a[],<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>*sm,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>*<span style="color: #000000;"><span style="color: #000000;">Mm) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>b[<span style="color: #800080;"><span style="color: #800080;"></span> -</span>]={<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">}; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i,sum1=<span style="color: #800080;"><span style="color: #800080;">0</span></span>, max1=<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(i=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; i<n;i++<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(sum1<<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">) {sum1</span></span>=<span style="color: #000000;"><span style="color: #000000;">a[i]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{sum1</span></span>=sum1+<span style="color: #000000;"><span style="color: #000000;">a[i]; } b[i]</span></span>=<span style="color: #000000;"><span style="color: #000000;">sum1; } max1</span></span>=b[<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(i=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; i<n;i++<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(max1<<span style="color: #000000;"><span style="color: #000000;">b[i]) {max1</span></span>=<span style="color: #000000;"><span style="color: #000000;">b[i]; </span></span>*MM =<span style="color: #000000;"><span style="color: #000000;">i; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(i = *mm;i >=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; i--<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(b[i] = =<span style="color: #000000;"><span style="color: #000000;">a[i]) { </span></span>*sm=<span style="color: #000000;"><span style="color: #000000;">i; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">max1;}</span></span></pre><p><p></p></p><p><p>Returns the and of the largest interconnected subarray in a two-dimensional integer array</p></p></span>