Hdu1838:chessboard (linear DP)

Source: Internet
Author: User

Title: http://acm.hdu.edu.cn/showproblem.php?pid=1838

That's a pretty good question, too. First of all, the bottom right corner of the chessboard must be ' 1 ', and the board contains at least a 1, so the minimum value is 1, then initialize, just start thinking wrong, think only tu[1][j]== ' 1 ' | | tu[j][1]== ' 1 ',

Dp[1][i] and dp[i][1] only initialized to 1, this is wrong, specific see Code Red Part, do not explain, direct sense inexpressible ah. Then fill it again.

Transfer equation:

if (tu[i][j]!=tu[i-1][j]&&tu[i][j]!=tu[i][j-1]&&tu[i][j]==tu[i-1][j-1])         Dp[i][j]=min (DP [I-1] [J-1],min (Dp[i-1][j],dp[i][j-1])) +1; else dp[i][j]=1;  


#include <iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#defineINF 0x3f3f3f3ftypedef __int64 LL;using namespacestd;intn,dp[ .][ .];Chartu[ .][ .];intMain () {intT,cnt,maxx; scanf ("%d",&T);  while(t--) {scanf ("%d",&N);  for(intI=1; i<=n; i++) {scanf ("%s", tu[i]+1); } memset (DP,0,sizeof(DP));  for(intI=1; i<=n; i++) {dp[1][i]=1; dp[i][1]=1; } Maxx=1;  for(intI=2; i<=n; i++)        {             for(intj=2; j<=n; J + +)            {                if(tu[i][j]!=tu[i-1][j]&&tu[i][j]!=tu[i][j-1]&&tu[i][j]==tu[i-1][j-1]) Dp[i][j]=min (dp[i-1][j-1],min (dp[i-1][j],dp[i][j-1]))+1; Elsedp[i][j]=1; if (tu[i][j]== ' 1 ') {maxx= Max (maxx,dp[i][j]);  }}} CNT=0;  for(intI=1; i<=n;i++)        {             for(intj=1; j<=n;j++)                if(dp[i][j]==maxx&&tu[i][j]=='1') CNT++; } printf ("%d%d\n", maxx,cnt); }    return 0;}

Hdu1838:chessboard (linear DP)

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.