由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 晕了,有人用iteration解n queens么
相关主题
生成一个有重复数的全排列,怎么做比较好如何写内存速度最优化的string permutation?有重复字符
对自己DFS能力彻底的绝望了。Permutation leetcode-
关于排列组合的题目的算法T家电面面经并且不解为何被秒拒
Exposed上一道string permutation的题iterative string permutation
问一个题(求推荐)recursion以及把recursion转变为iteration的资料
一个容易记忆的permutation算法How to handle the return type of container.size() in C++
抽空简单说一下昨天的Google Phone Interview问个白痴问题,DP到底算不算递归?
用了递归以后,怎么计算空间复杂度?请教为什么这段程序运行不work?(doubly linked list) (转载
相关话题的讨论汇总
话题: ncurrcol话题: ppos话题: nqueue话题: int话题: continue
进入JobHunting版参与讨论
1 (共1页)
s**********r
发帖数: 8153
1
糊涂了,不会了。。。。
求解!
l***b
发帖数: 125
2
这种网上搜搜很多吧。曾经有次面试,被要求把递归和非递归的都写出来。。。

【在 s**********r 的大作中提到】
: 糊涂了,不会了。。。。
: 求解!

s**********r
发帖数: 8153
3
N Queens?

【在 l***b 的大作中提到】
: 这种网上搜搜很多吧。曾经有次面试,被要求把递归和非递归的都写出来。。。
r**h
发帖数: 1288
4
iteration有什么好解法吗?
N重循环还是next permutation?
l***b
发帖数: 125
5
嗯,N queue

【在 s**********r 的大作中提到】
: N Queens?
s**********r
发帖数: 8153
6
求解法,刚搜到一个,正在验证,不知道好用不。。。

【在 l***b 的大作中提到】
: 嗯,N queue
s**********r
发帖数: 8153
7
阿?你说的这2个,怎么做。。。

【在 r**h 的大作中提到】
: iteration有什么好解法吗?
: N重循环还是next permutation?

l***b
发帖数: 125
8
随手写的,输出解的个数和wiki上一样,估计是对的吧,没仔细测
int g_nTotalSolutionNum = 0;
void PrintSolution(int *pPos, int nQueue)
{
if (pPos == NULL)
{
return;
}
for ( int i = 0; i < nQueue; ++i )
{
cout << pPos[i] << ", ";
}
cout << endl;
++g_nTotalSolutionNum;
}
void NQueue(int nQueue)
{
if ( nQueue == 0 )
{
return;
}
int *pPos = new int[nQueue];
int nCurrCol = 0;
pPos[0] = 0;

for(;;)
{
if ( pPos[nCurrCol] == nQueue )
{
if ( nCurrCol == 0 )
{
break;
}
--nCurrCol;
++pPos[nCurrCol];
continue;
}
bool bValidPos = true;
if ( nCurrCol > 0 )
{
for ( int i = 0; i < nCurrCol; ++i )
{
if (pPos[nCurrCol] == pPos[i] ||
((nCurrCol - i) == abs(pPos[nCurrCol] - pPos[i])))
{
bValidPos = false;
break;
}
}
}
if ( bValidPos )
{
if ( nCurrCol == nQueue - 1 )
{
PrintSolution(pPos, nQueue);
++pPos[nCurrCol];
continue;
}
else
{
++nCurrCol;
pPos[nCurrCol] = 0;
continue;
}
}
else
{
if ( pPos[nCurrCol] == nQueue - 1 )
{
--nCurrCol;
++pPos[nCurrCol];
continue;
}
else
{
++pPos[nCurrCol];
continue;
}
}
}
delete[] pPos;
}
s**********r
发帖数: 8153
9
谢谢,我读读!

【在 l***b 的大作中提到】
: 随手写的,输出解的个数和wiki上一样,估计是对的吧,没仔细测
: int g_nTotalSolutionNum = 0;
: void PrintSolution(int *pPos, int nQueue)
: {
: if (pPos == NULL)
: {
: return;
: }
: for ( int i = 0; i < nQueue; ++i )
: {

d****n
发帖数: 233
10
Mark.
The following part at the end seems not needed:
if ( pPos[nCurrCol] == nQueue - 1 )
{
--nCurrCol;
++pPos[nCurrCol];
continue;
}
else

【在 l***b 的大作中提到】
: 随手写的,输出解的个数和wiki上一样,估计是对的吧,没仔细测
: int g_nTotalSolutionNum = 0;
: void PrintSolution(int *pPos, int nQueue)
: {
: if (pPos == NULL)
: {
: return;
: }
: for ( int i = 0; i < nQueue; ++i )
: {

1 (共1页)
进入JobHunting版参与讨论
相关主题
请教为什么这段程序运行不work?(doubly linked list) (转载问一个题
请教 Iterator 一题一个容易记忆的permutation算法
树中序遍历,要求左子树用递归,右子树用iteration抽空简单说一下昨天的Google Phone Interview
有重复元素的全排列,递归算法用了递归以后,怎么计算空间复杂度?
生成一个有重复数的全排列,怎么做比较好如何写内存速度最优化的string permutation?有重复字符
对自己DFS能力彻底的绝望了。Permutation leetcode-
关于排列组合的题目的算法T家电面面经并且不解为何被秒拒
Exposed上一道string permutation的题iterative string permutation
相关话题的讨论汇总
话题: ncurrcol话题: ppos话题: nqueue话题: int话题: continue