关于皇后问题的核心数据用到的结构体要采用动态内存分配和链表结构
核心数据结构用到的结构体要采用动态内存分配和链表结构,求思路!有数组形式的、、能直接把它转换成标题要求的那样吗
#include <stdio.h> #include <math.h> #define MAX_QUEEN_SIZE (8)// int array[MAX_QUEEN_SIZE]={0};//确定了行号 int counter=0;//计数器 //冲突返回0 否则返回非0 int is_conflict(int val_row, int val_col) { int i; if (0 == val_row) { return -1; } //同列 for (i=0; i<val_row; ++i) { if (array[i] == val_col) { return 0; } } //同斜线 for (i=0; i<val_row; ++i) { if (abs(array[i]-val_col)==abs(i-val_row)) { return 0; } } return -1; } //确定列号 void queen(int val_row) { int i; if (MAX_QUEEN_SIZE == val_row) { ++counter; return; } for (i=0; i<MAX_QUEEN_SIZE; ++i) { if (0 != is_conflict(val_row, i)) {//不冲突 array[val_row] = i;//记录列 queen(val_row+1); } } } int main(void) { queen(0); printf ("%d\n", counter); return 0; }
/*** * @文件名:xxxx.c * @作 者:寒风中的细雨 * @时 间:2012/4/11/20:32 * @概 要:№‰¾√→ */ #include <stdlib.h> #include <assert.h> #include <stdio.h> #include <math.h> unsigned int counter=0;//计数器 typedef unsigned int elem_type; typedef struct _node { elem_type str_col;//列 elem_type str_row;//行 struct _node *next; }node, *ptr_node; //建立对象 ptr_node create_list(ptr_node h_list, elem_type v_code) { assert(NULL == h_list); h_list = (ptr_node) malloc (sizeof(node)); assert(NULL != h_list);//带头结点, 用于存放棋盘大小 h_list->str_col = h_list->str_row = v_code; h_list->next = NULL; return h_list; } //添加 int add_list(ptr_node h_list, elem_type v_row, elem_type v_col) { ptr_node p_tail = h_list; while ((NULL!=p_tail->next) && (p_tail=p_tail->next)); p_tail->next = (ptr_node) malloc (sizeof (node)); assert(NULL != p_tail->next); p_tail = p_tail->next; p_tail->str_col = v_col; p_tail->str_row = v_row; p_tail->next = NULL; return 0;//成功 } //输出 void print_list(ptr_node h_list) { unsigned int i, j; ptr_node p_tmp = h_list->next; for (i=0; i<h_list->str_row; ++i) { for (j=0; j<h_list->str_col; ++j) { if (p_tmp->str_col == j) { printf ("№"); } else { printf ("→"); } } printf ("\n"); p_tmp = p_tmp->next; } printf ("\n"); } //删除某结点 int delete_list(ptr_node h_list, int index) { ptr_node p_tail = h_list->next; ptr_node p_ftail = h_list; while (--index && (NULL!=p_tail->next) && (p_ftail=p_tail)) { p_tail = p_tail->next; } if (0 != index) { //error printf ("error!\n"); return -1; } p_ftail->next = p_tail->next; free(p_tail); return 0; } //释放资源 ptr_node destroy_list(ptr_node h_list) { ptr_node p_tail = h_list; while (p_tail) { ptr_node p_tmp = p_tail->next; free(p_tail); p_tail = p_tmp; } h_list = NULL; return h_list; } // int is_conflict(ptr_node h_list, const int val_row, const int val_col) { ptr_node p_tmp = h_list->next; if (0 == val_row) { return -1; } //同列 for (p_tmp = h_list->next; p_tmp; p_tmp = p_tmp->next) { if (val_col == p_tmp->str_col) { return 0; } } //同斜线 for (p_tmp = h_list->next; p_tmp; p_tmp = p_tmp->next) { if (abs(val_col-p_tmp->str_col) == abs(val_row-p_tmp->str_row)) { return 0; } } return -1; } // void queen(ptr_node h_list, const unsigned int val_row) { unsigned int i; if (val_row == h_list->str_row) { ++counter; print_list(h_list); return; } for (i=0; i<h_list->str_col; ++i) { if (0 != is_conflict(h_list, val_row, i)) { add_list(h_list, val_row, i); queen(h_list, val_row+1); delete_list(h_list, val_row+1); } } } // int main(int argc, char *argv[]) { int i; ptr_node list=NULL; printf (">\\"); scanf_s ("%d", &i); assert(0 < i); list = create_list(list, i); queen(list, 0); printf ("%d\n", counter); list = destroy_list(list); assert(NULL == list); return 0; }