围棋棋盘由横纵19*19条线组成,请问这些线共组成多少个正方形?假定只考虑横纵方向,忽略倾斜方向。
采用分治的思想,将问题规模缩小。
<img src="https://img-blog.csdn.net/20161002113351322?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;" alt="" />#include<iostream>
#include<vector>
using namespace std;
int main()
{
int M=19;
int N=19;
vector<vector<int>> chess(M,vector<int>(N)); //申请二维数组
//初值
int i,j;
for (i=0; i<M; i++)
chess[i][0]=0;
for (j=0; j<N; j++)
chess[0][j]=0;
int count = 0;
for (i=1; i<M; i++) {
for (j=1; j<N; j++) {
if (i!=j)
chess[i][j]=max(chess[i-1][j],chess[i][j-1]);
else
chess[i][j]=chess[i-1][j]+1;
count+=chess[i][j];
}
}
cout<<"总数为:"<<count<<endl;
return 0;
}