Returns the and of the largest interconnected subarray in a two-dimensional integer array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.