(DFS深度优先遍历+回溯)给出一堆长度各异的木棍,这些木棍能否头尾相连形成一个正方形

Square8

题目描述:

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

  大意:给出一堆长度各异的木棍,这些木棍能否头尾相连形成一个正方形

输入:

  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 10000.

输出:

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

样例输入:

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

样例输出:

yes
no
yes

题目分析:

1、如果所有木棍总长度len不能被4整除,则结果为no

2、最终围成的正方形边长side = len / 4;(木棍长度都是整型)

3、如果所有木棍中最长的木棍比side长,则结果为no(可以用排序事先排好序)

4、DFS搜索,搜索的载体为木棍数组(由每个数组元素作为定点的完全图)

5、构造搜索状态三元组(sum, number,position)

6、sum表示当前已经凑出的边长度; number表示已经凑出的边数; position表示当前遍历的顶点(数组下标)

以7 6 5 4 4 3 2 1为例,对应的完全图如下:

从7开始BFS,一共有n!条路径(从一个顶点出发有(n-1)!条路径,一共n个顶点),回归点处需要重置访问标记

遍历时:①下一个顶点已访问;

          或②sum + 下一个顶点的值(木棍长度) > side;

              则跳过改点

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int sticks[20];  //木棍数组
int side;        //正方形边长
int m;           //木棍个数
bool visited[20];
bool DFS(int sum, int number, int position){
    if(number == 3){       //能凑出三根就一定能围成正方形(剩下的一定能组合成一条边)
        return true;
    }
    for(int i = position; i < m; i ++){     //一共有n!条路线
        if(visited[i] || sum + sticks[i] > side){
            continue;
        }
        visited[i] = true;
        if(sum + sticks[i] == side){
            if(DFS(0, number + 1, 0)){    //重新开始凑边
                return true;
            }
        }else{    //sum + sticks[i] < side
            if(DFS(sum + sticks[i], number, i + 1)){   //继续DFS
                return true;
            }
        }
        visited[i] = false;     //回归点处,重置访问标志
    }
    return false;
}
bool Compare(int x, int y){
    return x > y;
}
int main(){
    int n;
    while(scanf("%d", &n) != EOF){
        while(n --){
            scanf("%d", &m);
            int len = 0;
            for(int i = 0; i < m; i ++){
                scanf("%d", &sticks[i]);
                len += sticks[i];
            }
            memset(visited, false, sizeof(visited));  //初始化访问标记数组
            sort(sticks, sticks + m, Compare);   //将所有木棍从大到小排序
            if(len % 4 != 0){     //所有木棍的总长度必须可以整分为速快
                cout << "no" << endl;
                continue;
            }else{
                side = len / 4;
                if(sticks[0] > side){   //木棍的最大值必须小于边长
                    cout << "no" << endl;
                    continue;
                }
            }
            if(DFS(0, 0, 0)){
                cout << "yes" << endl;
            }else{
                cout << "no" << endl;
            }
        }
    }
    return 0;
}

 

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值