Problem 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
意就是好多棍子,看能不能拼成正方形。 主要注意的有几点: 所有棍子都要用到,不能剩余 输入已经保证大于4根棍子了。所以无需判断可能小于3根棍子的情况 棍长的总数首先要是4的倍数,才能进行。否则直接输出 “no” 当前面前提满足以后,再满足3 根棍子拼好,就完工了。最后一根一定能拼好。 解法就是DFS——->深度优先搜索。DFS的思路就是一个图沿着一条路走下去,当走不下去的时候就回溯到上一结点再去走没有走过的岔路。 换算成数据结构的话,就要有一个“标记”来标记每个结点是否走过。DFS具体的实现方式,常见的一种就是:循环里面嵌套递归,这也算是一个DFS的框架。而剩下的要补充的“题眼”(也就是关键的地方)是要转移的状态。 参考:http://blog.csdn.net/guodongxiaren/article/details/23126997
