bool值参数传入
函数原型:void fill(bool name[] ,int num , bool tf) 传入参数时候,给tf传入true或者false不对,传入1或者0 也不对,why?tf的传入值是 1'f(类似f的东西)',但是name[i] = tf;之后,name的值一直不变
程序代码:
#include<iostream> using namespace std; #define num_queens 8 #define zero 0 #define seven ( num_queens - 1) #define fif ( 2 * num_queens - 1) bool column_free [num_queens]; bool diag_free [fif]; bool inv_diag_free [fif]; bool check(int i , int j) {return column_free[j] && diag_free[i + j] && inv_diag_free [seven + i + j];} void set_queen(int i , int j) { column_free[j] = diag_free[i + j] = inv_diag_free [seven + i + j]=false;} void remove_queen(int i , int j) { column_free[j] = diag_free[i + j] = inv_diag_free [seven + i + j]=true;} void fill(bool name[] ,int num , bool tf) { for(int i = 0; i < num; ++i) name[i] = tf; } class queenclass{ public: queenclass() {for(int i=0;i<num_queens; ++i)queen[i]=0;} ~queenclass(){;} void queenchange(int i) {queen[0] = i;} void pop() { queen[size()-1] = 0;} void push(int j){queen[size()]= j;} bool empty() { if(0 == size()) return true; else return false; } int size() { int i = 0; while(queen[i] != 0) {++i;} return i; } int low(){return queen[0];} int top(){return queen[size()-1];} void write() { for(int i = size() ; i >=0; --i) cout<<i<<'.'<<queen[i]<<"->"; cout<<'\b'<<endl; } private: int queen[num_queens]; }; int main() { queenclass q; fill(column_free ,num_queens,true) ; fill(diag_free, fif,true); fill(inv_diag_free, fif,true); while(q.size()<= num_queens) { int j; for( j = zero;!check(q.size(), j); ++j) { while(j >= seven) { if(q.empty()) return false; j=q.top(); q.pop(); remove_queen(q.size(), j); } } set_queen(q.size(),j); q.push(j); if(q.size() == num_queens) { cout<<"one of the way is"<<endl; q.write(); fill(column_free ,num_queens,true) ; fill(diag_free, fif,true); fill(inv_diag_free, fif,true); q.queenchange(q.low()+1); } } return 0; }