【Depth First Search】Counter: Equilateral Triangle

Determine whether a group of numbers can form an equilateral triangle

Input: n numbers

Output: yes/no

Suppose the three sides are divided into a, b, c. To make n choices, each number can have three choices, which can be placed on a, b, or c. You need to set 3 parameters to record these states.

dfs(index, x, y, z): index selections are made, and the lengths of the current edges a, b, and c are x, y, z

if(index==n) return false;
//已经做了n次选择
if(x>sum/3||y>sum/3||z>sum/3)return false;
//有一条边大于sum/3
if(index==n&&x==y&&y==z&&z==sum/3)return true;
//已经做了n次选择当前是等边三角形

return dfs(index+1,x+n[index],y,z)||
dfs(index+1,x,y+n[index],z)||dfs(index+1,x,y,z+n[index]);

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/123910521