分享
 
 
 

Space and time trade-off in n queen problem

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

The backtracking algorithm of n queen problem can be improved by space and time trade-off further. The consideration of positions occupancy is attained by duplicate_array and two integrals. Note that these utilities can’t tell all illegal moves because it is performing its job on the basis of chess_board definition. That is, power of utilities contained by chess_board are not enough to telling all illegal moves. It needs a data structure to record positions occupied by chesses. Naturally, it needs three arrays to recording row, column, diagonal1 (upper left to lower right) and diagonal2 (upper right to lower left). Column is already represented by duplicate_array. So it needs two other array to represent diagonal1,2. Consequently, the job is how to express the meaning of one diagonal is occupied. That is, for a move: (cur_i, j), how to assign value to the corresponding element in diagonal1,2. Assuming the one of occupied diagonal1 is diagonal1[x] and diagonal2 is diagonal2[y], then assign diagonal1[x] and diagonal2[y] to 1 to denoting these diagonals are occupied. An n queen problem has 2n-1 diagonal1s and 2n-1 diagonal2s. The rules are:

Diagonal1[j-cur_i]=1 if j >= cur_i

Diagonal1[j-cur_i+2*n-1]=1 if j < cur_i

Diagonal2[j+cur_i]=1

The codes:

#include <iostream>

#include <vector>

#include <algorithm>

#include <utility>

#include <functional>

#include <ctime>

using namespace std;

template<class T>

struct output_functor : public unary_function<T,void>

{

output_functor():n(0){}

void operator()(const T& p)

{

cout<<"("<<n<<","<<p<<")"<<" ";

n++;

}

int n;

};

class chess_board

{

vector<int> array;

vector<bool> duplicate_array;

vector<bool> diagonal1, diagonal2;

public:

int n;

int cur_i;

int number_solution;

chess_board(int dim):n(dim),cur_i(0),number_solution(0)

{

array.resize(n,-30000);

duplicate_array.resize(n,0);

diagonal1.resize(2*n-1,0);

diagonal2.resize(2*n-1,0);

}

void move(int j)

{

array[cur_i]=j;

if(j>=cur_i)

{

diagonal1[j-cur_i]=1;

}

else

{

diagonal1[j-cur_i+2*n-1]=1;

}

diagonal2[j+cur_i]=1;

duplicate_array[j]=1;

}

void un_move(int j)

{

array[cur_i]=-30000;

if(j>=cur_i)

{

diagonal1[j-cur_i]=0;

}

else

{

diagonal1[j-cur_i+2*n-1]=0;

}

diagonal2[j+cur_i]=0;

duplicate_array[j]=0;

}

bool legal_pos(int j)

{

if(duplicate_array[j] == 0 && diagonal2[j+cur_i] == 0)

{

if(j<cur_i)

{

if(diagonal1[j-cur_i+2*n-1]==0)

return true;

}

else

{

if(diagonal1[j-cur_i]==0)

return true;

}

return false; //tricky. if lack this, then function has nothing to return(it will return true in this situation)

}

else

return false;

}

void write()

{

number_solution++;

for_each(array.begin(), array.end(), output_functor<int>());

cout<<endl;

}

};

void BackTracking(chess_board_delegation& c)

{

if(c.get_cur_i() == c.get_n())

{

c.write();

c.set_cur_i(c.get_cur_i()-1);

return;

}

else

{

for(int j=0; j<c.get_n(); j++)

{

if(c.legal_pos(j) == false)

continue;

c.move(j);

c.set_cur_i(c.get_cur_i()+1);

BackTracking(c);

c.un_move(j);

}

c.set_cur_i(c.get_cur_i()-1);

}

}

//////////////////////// omitted chess_board_delegation and main ////////////////////////////

For the convenient of test, it should use a template argument to indicate with or without output.

template<bool with_output>

class chess_board

{

......

void write()

{

if(with_output == true)

{

number_solution++;

for_each(array.begin(), array.end(), output_functor<int>());

cout<<endl;

}

else

{

number_solution++;

}

}

......

};

template<bool T>

class chess_board_delegation

{

chess_board<T>* cbp;

public:

chess_board_delegation(chess_board<T>* p):cbp(p){}

......

};

template<bool T>

void BackTracking(chess_board_delegation<T>& c)

{

......

}

int main()

{

......

chess_board<false> a(n);

chess_board_delegation<false> c(&a);

......

}

Using specialization can avoid the equality comparison in write function in order to improve the performance a bit. The codes of definition of chess_board is: template<bool with_output> class chess_board{…with two last sentences of write function…}; template<>class chess_board<false>{…without two last sentences of write function…}; But this method needs double codes so I have not write it at here.

There are a lot of works has done, the practice data shows that: for n=13, the first version needs: 10.956 seconds. The best (last) version needs 1.963 seconds. (Number of solutions are 73712) of course, the larger n is, the improvement has. It still has room to improve performance of this algorithm because symmetry. Utilize this characteristic of n queen problem can lead to double performance improvement. I leave this in next article.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有