第5章 回溯法,符号三角形问题

#include <iostream>
#include <cstdio>

using namespace std;

#define MAXSIZE 21

int n;								//第一行符号个数
int signTriangle[MAXSIZE][MAXSIZE];	//符号三角形矩阵,矩阵最左侧一列不用
int count;							//满足条件的符号三角形个数
int half;							//符号总数的一半
int amount;							//符号总数

int Count(int k, int count)
{
	//printf("sign[%d] %d ", k, signTriangle[0][k]);
	int i, j;
	for (i = 0, j = k; i < k; i++, j--)
	{
		count += signTriangle[i][j];
		if (signTriangle[i][j-1] == signTriangle[i][j])
		{
			signTriangle[i+1][j-1] = 1;
		}
		else
		{
			signTriangle[i+1][j-1] = 0;
		}
	}
	//printf("number of plus signs: %d\n", count);
	return count;
}

void Backtrack(int k, int plusSigns)
{
	if (k > n)
	{
		count++;
		//printf("%d %d\n", plusSigns, count);
	}
	else
	{
		int i;
		for (i = 0; i < 2; i++) //i为0代表减号,为1代表加号
		{
			signTriangle[0][k] = i;
			//统计新增最右侧一条边后,加号的数量
			int temp = Count(k, plusSigns);
			//加号和减号的数量均不能超过总符号数的一半
			if (temp <= half && (k*(k+1))/2 - temp <= half)
			{
				Backtrack(k+1, temp);
			}
		}
	}
}

int main(void)
{
	while (scanf("%d", &n) != EOF)
	{
		count = 0;
		amount = n*(n+1) / 2;
		//printf("amount: %d\n", amount);
		if (amount % 2 == 0)
		{
			half = amount / 2;
			Backtrack(1, 0);
		}
		printf("%d\n", count);
	}
	return 0;
}

    原文作者:回溯法
    原文地址: https://blog.csdn.net/code_pang/article/details/8809557
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞