poj2362(小木棍问题的逆过程)

Square
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 18880   Accepted: 6549

Description

Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?

Input

The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.

Output

For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".

Sample Input

3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5

Sample Output

yes
no
yes

题目描述:

给出M根小木棍,问是否能拼成一个正方形。相当于把小木棍复原成4根等长的大木棍,若能,输出Yes,否则输出No。

解题思路:

回溯法,即深搜,再加一些剪枝,具体看代码注释。

#include
#include 
#include 
using namespace std;

int M, side;
int stick[21];//各个小木棍
bool mark[21];//标记数组,记录小木棍是否用过

int cmp(const void *a,const void *b);
int dfs(int num,int len);

int main()
{
    int i, N;
    cin>>N;
    while(N --)
    {
        int sum = 0;
        memset(mark,0,sizeof(mark));
        cin >> M;
        for(i=0; i> stick[i];
            sum += stick[i];//sum是所有小木棍长度的总和
        }
        side = sum / 4;//side是拼成的正方形的边长

        qsort(stick,M,sizeof(stick[0]),cmp);//把所有小木棍长度排序,降序排

        if(stick[0] > side)//剪枝1,如果最长木棍长度大于正方形边长,则肯定无法拼成正方形
        {
            cout << "No" <


你可能感兴趣的:(模拟题,深搜,剪枝)