N皇后问题(很快,保存下)

myhalfsea posted @ 2010年3月17日 18:48 in JAVA , 661 阅读

   1. import java.util.Calendar; 
   2.  
   3. public class Queen { 
   4.  
   5. public static int sum = 0, upperlimit = 1; 
   6.  
   7. public static void compute(int row, int ld, int rd); { 
   8.  
   9. if (row != upperlimit); { 
  10. int pos = upperlimit & ~(row | ld | rd);
  11. while (pos != 0); { 
  12.  
  13. int p = pos & -pos; 
  14. pos -= p; 
  15. compute(row + p, (ld + p); << 1, (rd + p); >> 1); 
  16. } 
  17.  
  18. } else 
  19. sum++; 
  20. } 
  21.  
  22. public static void main(String[] args); { 
  23. Calendar start; 
  24. int n = 8; 
  25.  
  26. if (args.length > 0); 
  27. n = Integer.parseInt(args[0]);
  28. start = Calendar.getInstance();
  29. if ((n < 1); || (n > 32);); { 
  30. System.out.println(" 只能计算1-32之间\n");
  31. return; 
  32. } 
  33. System.out.println(n + " 皇后\n");
  34. upperlimit = (upperlimit << n); - 1; 
  35. compute(0, 0, 0);; 
  36. System.out.println("共有" 
  37. + sum 
  38. + "种排列, 计算时间" 
  39. + (Calendar.getInstance();.getTimeInMillis(); - start 
  40. .getTimeInMillis();); / 1000 + "秒 \n");
  41. } 
  42.  
  43. } 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta
Butterfly Theme | Design: HRS Hersteller of mobile Hundeschule.