程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> hdu 1518(dfs),hdu1518dfs

hdu 1518(dfs),hdu1518dfs

編輯:C#入門知識

hdu 1518(dfs),hdu1518dfs


題目鏈接 :http://acm.hdu.edu.cn/showproblem.php?pid=1518

 

 

Square

  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         題意 :給你n條邊,讓你用這些邊組成正方形(不多不少僅n條)。   分析 :主要是超時問題,注意優化。   1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int a[22],vis[22]; 7 int n,m,length; //length表示要組成的正方形的邊長 8 int ans,flag; 9 10 void dfs(int cnt,int sum, int k) //cnt記錄邊數,sum記錄當前邊長,k記錄位置 11 { 12 if (cnt == 3) //如果有三條邊滿足要求,那麼第四條邊一定滿足要求 13 { 14 flag = 1; 15 return ; 16 } 17 if (sum == length) //找到滿足要求的邊,邊數加一,初始化 18 { 19 cnt++; 20 k = 0; 21 sum = 0; 22 } 23 for (int i=k; i<m; i++) 24 { 25 if (!vis[i] && sum + a[i] <= length) 26 { 27 vis[i] = 1; 28 dfs(cnt, sum+a[i], i+1); 29 if (flag) //優化時間,(當找到所有邊之後就一直返回,不需要再把之後的代碼運行一遍) 30 { 31 return ; 32 } 33 vis[i] = 0; 34 } 35 } 36 } 37 38 int main () 39 { 40 scanf ("%d",&n); 41 while (n--) 42 { 43 ans = 0; 44 scanf ("%d",&m); 45 for (int i=0; i<m; i++) 46 { 47 scanf ("%d",&a[i]); 48 ans += a[i]; 49 } 50 if (ans % 4) //如果所有邊長和不是4的倍數,怎樣都不能組成正方形 51 { 52 printf ("no\n"); 53 continue ; 54 } 55 memset(vis, 0, sizeof(vis)); 56 flag = 0; 57 length = ans / 4; //記錄正方形邊長 58 dfs(0, 0, 0); 59 if (flag) 60 printf ("yes\n"); 61 else 62 printf ("no\n"); 63 } 64 return 0; 65 } View Code

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved