Requirements:
Enter a two-dimensional shaping array with positive numbers in the array and a negative number.
The maximum value for the and of all sub-arrays.
Spit Groove:
This algorithm is not particularly good to write, see a lot of seniors learn elder sister's writing found that most of their logic is flawed, the method is also very Earth (...) ), this topic I think the need to use DFS traversal +DP Dynamic planning, design is not detailed explained, more trouble.
1#include <iostream>2#include <cstring>3#include <Set>4 using namespacestd;5 intmap[ the][ the];6 BOOLv[ the][ the];7 Set<Long Long>DP;8 intAns,m,n;9 intXd,yd;Ten intx[4]={1,0,-1,0}; One inty[4]={0,1,0,-1}; A BOOLIsOK (intXinty) { - if(x<1|| y<1)return 0; - if(x>m| | Y>n)return 0; the return 1; - } - Long LongTonum () { - Long LongA=0; + for(intI=1; i<=m;i++){ - for(intj=1; j<=n;j++){ + if(V[i][j]) { A Long Longs=1<< (I-1)*m); ats=s<< (J-1); -a=a|s; - } - } - } - returnA; in } - intC=0; to voidDfsintNowans) { + if(nowans>ans) { -ans=Nowans; the } * for(intIi=1; ii<=m;ii++){ $ for(intjj=1; jj<=n;jj++){Panax Notoginseng if(V[ii][jj]) { - for(intI=0;i<4; i++){ the if(IsOK (Ii+x[i],jj+y[i]) && (v[ii+x[i]][jj+y[i]]==0)){ +v[ii+x[i]][jj+y[i]]=1; A Long Longs=tonum (); the if(Dp.count (s) = =0) C++,dp.insert (s), DFS (nowans+map[ii+x[i]][jj+Y[i]]); +v[ii+x[i]][jj+y[i]]=0; - } $ } $ } - } - } the } - intMain () {WuyiCin>>m>>N; the for(intI=1; i<=m;i++){ - for(intj=1; j<=n;j++){ WuCin>>Map[i][j]; - } About } $ for(intI=1; i<=m;i++){ - for(intj=1; j<=n;j++){ -Xd=i;yd=J; -v[i][j]=1; A DFS (Map[i][j]); +memset (V,0,sizeof(v)); the } - } $cout<<c<<Endl; thecout<<ans<<Endl; the return 0; the}
Run:
Returns the and of the largest interconnected subarray in a two-dimensional integer array