Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

解题思路:和上题一样《JAVA语言程序设计》中exerice6-22已经给出了计算count的代码,直接拿来用即可,JAVA实现如下:

static public int totalNQueens(int n) {
if(n==1)
return 1;
int count = 0;
int[] queens = new int[n]; // queens are placed at (i, queens[i])
for (int i = 0; i < n; i++)
queens[i] = -1;
queens[0] = 0;
int k = 1;
while (k >=0) {
int j = findPosition(k, queens,n);
if (j ==-1) {
queens[k] = -1;
k--; // back track to the previous row
} else {
queens[k] = j;
if (k == n-1)
count++;
else {
k++;
}
}
}
return count;
}
public static int findPosition(int k, int[] queens,int n) {
int start = queens[k] == -1 ? 0 : queens[k] + 1;
for (int j = start; j < n; j++) {
if (isValid(k, j, queens,n))
return j;
}
return -1;
} public static boolean isValid(int k, int j, int queens[],int n) {
for (int i = 0; i < k; i++)
if (queens[i] == j)
return false;
for (int row = k - 1, column = j - 1; row >= 0 && column >= 0; row--, column--)
if (queens[row] == column)
return false;
for (int row = k - 1, column = j + 1; row >= 0 && column <= n-1; row--, column++)
if (queens[row] == column)
return false;
return true;
}

Java for LeetCode 052 N-Queens II的更多相关文章

  1. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  2. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  3. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  4. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  5. Java实现 LeetCode 63 不同路径 II(二)

    63. 不同路径 II 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在 ...

  6. Java实现 LeetCode 40 组合总和 II(二)

    40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...

  7. Java for LeetCode 229 Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  8. Java for LeetCode 219 Contains Duplicate II

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...

  9. Java for LeetCode 213 House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

随机推荐

  1. MVC的URL路由规则

    MVC的URL路由规则 Routing的作用:它首先是获取到View传过来的请求,并解析Url请求中Controller和Action以及数据,其次他将识别出来的数据传递给Controller的Act ...

  2. sprintf、strcpy和memcpy的区别

    做某题用到了sprintf把一个字符数组(字符串)写到二维字符数组里,然后耗时挺长的,想了想strcpy好像也可以,事实证明strcpy效率果然更高,然后想了想觉得memcpy好像也可以.实践了一下的 ...

  3. 关于MyEclipse对Struts2配置文件较检异常 Invalid result location value/parameter

    有时候Struts.xml配置没有错误,完全可以顺利运行,而MyEclipse9以上版本却经常出现一大坨错误标识,错误信息是 Invalid result location value/paramet ...

  4. FFTW中文参考

    据说FFTW(Fastest Fourier Transform in the West)是世界上最快的FFT.为了详细了解FFTW以及为编程方便,特将用户手册看了一下,并结合手册制作了以下FFTW中 ...

  5. jsp学习(三)

    <%@page contentType="text/html;charset=gbk"%> <html> <body> <font siz ...

  6. Ten Tips for Writing CS Papers, Part 1

    Ten Tips for Writing CS Papers, Part 1 As a non-native English speaker I can relate to the challenge ...

  7. 51nod 1170 1770 数数字(动态规划)

    解题思路:看到题后,直接想到分成两种情况: ①:a*b >9 这里又分成两种 1. n==1 a*b 直接是一个两位数 求得十位和个位(这里十位和个位不可能相等) 然后如果等于d 则结果=1 2 ...

  8. Metropolis Light Transport学习与实现

    这段时间一直在看Metropolis Light Transport(简称mlt),现利用这篇博文把之前看资料已经coding上的一些体会记录下来. 1.Before MLT 在MLT算法被提出之前, ...

  9. php实现文件安全下载

    public function downloads($name){ $name_tmp = explode("_",$name); $type = $name_tmp[0]; $f ...

  10. c++中try catch的用法

    c++中try catch的用法 标签: c++exception数据库sqlc 2011-10-24 21:49 45622人阅读 评论(3) 收藏 举报  分类: 一点小结(267)  版权声明: ...