Beispiel #1
0
void _locQueen(int board[N][N], int row, int max)
{
	int col,i=0;
	int tmp[N][N];
	

	for (col=0; col<max; col++){
		if (board[row][col]==0){
			_copyBoard(board, tmp, max);
			_occupy(board, row, col, max);
                  _locQueen(board, row+1, max);
                  if (row == max-1){
				printf ("\n      ---布局%d---\n", ++n);
				_printBoard(board, max);
				system("pause");
			}
                  _copyBoard(tmp, board, max);//恢复上一次占位前的状态
		}
	}
}
void _createNode(int next, char state[][BOARD_SIZE])
{
  /* function creates a new board and sets the fields */
  node *new_node = malloc(sizeof(node));
  if(new_node == NULL){
    printf("malloc fail: _createNode\n");
    exit(EXIT_FAILURE);
  }
   memset(new_node,0,sizeof(node));
  /* copy state over to head node */
  _copyBoard(new_node,state);
  
  if(test_tree_head == NULL){
    new_node->parent = NULL;
    new_node->next = NULL;
    new_node->child_head = NULL;
    test_tree_head = new_node;
    current_state = test_tree_head;
  }
  else{
    
    /*add as child to parent node */
    node *prev = current_state->child_head;
    if(prev != NULL){
      node *cur = prev->next;
      for(;(cur != NULL);prev = prev->next, cur = cur->next);
      prev->next = new_node;
    }
    else{
      current_state->child_head = new_node;
    }

    /* stupid hack to connect child nodes to the proper parent, and 
       set the parents child_list */
    if(next == 1){
      //reset current_state to next node in parent list
      current_state = current_state->next;
      //set current_state child head to previous (to connect all children among parent list
      current_state->child_head = new_node;
    }

    new_node->parent = current_state;  
    new_node->next = NULL;
    new_node->child_head = NULL;
  }
   
}