//8queen.cpp #include <iostream> const int N = 8; int a[N][N] = {0};
//杈撳嚭鐭╅樀 int main() //鎺㈢儲錛岄亶鍘?鏍稿績閮ㄥ垎) //鍒ゆ柇姝ラ鏄惁鏈夋晥 //杈撳嚭鐭╅樀 cout << "--------------------" <<endl;
//eight queens problem錛屾誨叡鏈?2涓В.
//鍏殗鍚庨棶棰橈細鍦?*8鐨勬柟鏍兼鐩樹笂瀹夌疆8涓瀛愶紙鐨囧悗錛?br>//瑕佹眰錛氫換浣曚袱涓瀛愰兘涓嶈兘鍦ㄦ鐩樼殑鍚屼竴琛岋紝鍚屼竴鍒楁垨鍚屼竴瀵硅綰夸笂銆?/font>
using namespace std;
static int num = 0;
void print(int a[][N],int row); //娉ㄦ剰浜岀淮鏁扮粍濡備綍璋冪敤
//鍒ゆ柇姝ラ鏄惁鏈夋晥
bool is_valid(int i, int j);
//鎺㈢儲錛岄亶鍘?/font>
void traversal(int i);
{
traversal(0);
cout << "There are " << num << " solutions for eight queens problems" <<endl;
return 0;
}
void traversal(int i)
{
if(i >= 8)
{
print(a, N);
}
for(int j = 0; j != N; ++j)
{
a[i][j] = 1;
if(is_valid(i,j)) traversal(i+1);
a[i][j] = 0;
}
}
bool is_valid(int i, int j)
{
bool b = true;
for(int i1 = 0; i1 != i; ++i1)
for(int j1 = 0; j1 != N; ++j1)
{
if(a[i1][j1] == 1)
if((j1 == j) || ( abs(i1 - i) == abs(j1 - j)) ) b = false;
}
return b;
}
void print(int a[][N],int row)
{
++num;
for(int i = 0; i != row; ++i)
{
for(int j = 0; j != N; ++j)
cout << a[i][j] << " ";
cout << endl;
}
}
鏂囩珷鏉ユ簮:http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!272.entry
]]>
鐢ㄨ繖涓▼搴忕敤鎴峰氨鍙互鑷杈撳叆鎯寵鐨勬潹杈変笁瑙掑艦鐨勮鏁頒簡銆?
//YHTriangle.cpp
//杈撳嚭鏉ㄨ緣涓夎褰?br>//綆楁硶鎬濇兂錛氶鍏堝湪寰幆闃熷垪涓瓨鏀劇涓夎鐨?1錛?錛?鍜岀鍥涜鐨?.
//鑻ュ驚鐜槦鍒楅槦澶村厓绱犲拰闃熷ご絎簩涓厓绱犲潎涓?錛屽垯浠庨槦澶村垹闄や竴涓?錛屽湪闃熷熬鎻掑叆涓や釜1.
//鑻ヤ笉鐒訛紝灝嗛槦瀵瑰ご鍏冪礌鍜岄槦澶寸浜屼釜鍏冪礌鐩稿姞錛屽皢鍜屽兼彃鍏ュ埌闃熷熬錛屽垹闄ゅ澶翠竴涓厓绱犮?br>//鑻ユ兂杈撳嚭鏉ㄨ緣涓夎褰琛岋紝灝嗗驚鐜槦鍒楅暱搴﹁緗垚 n+2銆?br>//杈撳嚭錛氬墠涓よ鐩存帴杈撳嚭銆傚畾涔変簡print鍑芥暟鎺у埗鍚庨潰姣忚杈撳嚭鐨勫厓绱犱釜鏁?br>#include "Queue.h"
#include "CycQueue.h"
#include <iostream>
using namespace std;
//杈撳嚭n涓┖鏍?/font> int main() CycQueue<int> YHTri(n+2);
YHTri.push(1); //杈撳嚭寰幆闃熷垪涓暀瀛樼殑鍏冪礌 system("pause"); void print_space(int n) //杈撳嚭n涓┖鏍?br>{ int i = 1, j = 0; //寰幆闃熷垪鐨勬ā鏉跨被澹版槑
#ifndef CYCQUEUE_H #include <iostream> template<class T> void clear() {rear = front = 0;}; #include "CycQueue.cpp"
寰幆闃熷垪涓昏娉ㄦ剰涓嶈蹇樿%len錛屼笉鐒跺氨閮芥槸鑾悕閿欒鍟︼紒
void print_space(int n);
//n琛屾潹杈変笁瑙掑艦鐨勮緭鍑烘牸寮?/font>
void print(int k, int n);
{
int n;
cout << "Please enter the YangHui Triangle row number n:";
cin >> n;
YHTri.push(2);
YHTri.push(1);
YHTri.push(1);
//杈撳嚭鍓嶄袱琛?/font>
print(1,n);
print(1,n);
print(1,n);
while( !YHTri.full())
{
int a,b;
//鑻ラ槦澶村厓绱犲拰闃熷ご絎簩涓厓绱犲潎涓?
if( (YHTri.top() == 1) && (YHTri.second() == 1))
{
a = YHTri.pop_top();
YHTri.push(1);
YHTri.push(1);
print(a,n);
}
//鑻ヤ笉鐒?/font>
else
{
a = YHTri.pop_top();
b = YHTri.top();
YHTri.push(a + b);
print(a,n);
}
}
while( !YHTri.empty())
{
print(YHTri.pop_top(),n);
}
return 0;
}
while(n--) cout << " ";
}
void print(int k, int n) //n琛屾潹杈変笁瑙掑艦鐨勮緭鍑烘牸寮?br>{
if( i==1 ) print_space(n);
if(j++ != i) ;
else
{
cout << endl;
print_space(n-i);
++i;
j = 1;
}
cout << k <<" ";
}
#define CYCQUEUE_H
using namespace std;
class CycQueue:public Queue<T>
{
public:
CycQueue(int maxsz = 100):len(maxsz)
{
elems = new T[maxsz];
front = rear = 0;
};
~CycQueue(){delete[] elems; };
int size()const
bool full()const
bool empty()const
bool push(const T& item);
bool pop();
T top()const;
T pop_top();
T second()const;
protected:
private:
int front;
int rear;
int len;
T* elems;
};
#endif
鏂囩珷鏉ユ簮:http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!271.entry
]]>
緙虹偣錛氬洜涓烘暟緇勯暱搴︿笉鑳藉湪榪愯鏃剁‘瀹氾紝鎵浠ユ潹杈変笁瑙掑艦鐨勯暱搴︿笉鑳介氳繃鐢ㄦ埛鑷杈撳叆紜畾銆?
#include<iostream>
using namespace std;
const int N = 10;
int main()
{
int a[N]={0};
a[0]=1;
for(int i=1; i!=N;++i)
{
int m = N - i;
//杈撳嚭絀烘牸
while(m != 0 )
{
cout << " ";
--m;
}
//杈撳嚭
for(int j=0;j != i;++j)
cout << a[j] << " ";
cout << endl;
//璧嬪?/font>
a[i]=1; //鏈鍚庝竴涓厓绱犺祴鍊?
for(int j=i-1;j!=0;--j)
a[j]=a[j]+a[j-1];
}
return 0;
}
鏂囩珷鏉ユ簮:http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!270.entry