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)