八皇后问题
#include <math.h> #include <stdio.h> #define MAX 8 //棋盘大小MAX*MAX int board[MAX]; int count = 0; void show_result() //输出结果 { int i; for(i = 0; i < MAX; i++) printf("(%d,%d),", i+1, board[i]+1); //生活中从1开始计数 printf("\n"); } int check_cross(int n) //检查是否与前面已经存在的皇后冲突 { int i; for(i = 0; i < n; i++) if(board[i]==board[n]||(n-i)==abs(board[i]-board[n])) return 1; //若冲突返回1 return 0; } void put_chess(int n) // 在第n行放棋子 { int i; for(i = 0; i < MAX; i++) { //依次对第0~7列试探有无冲突 board[n] = i; // board[n]存储列号 (行号是n) if(!check_cross(n)) { //不冲突 if(n == MAX-1) { //若已经到第八行,则找到一个解 count++; printf(“%3d: ”, count); //输出一个解的序号 show_result(); //输出�Y果 if(count%24 == 0) { //每24行一屏暂停 getch(); clrscr(); } } else put_chess(n+1); //若未到第八行,则递归调用,进入下一行 } } } int main() { clrscr(); puts("The possible placements are:"); put_chess(0); puts("\n Press any key to quit..."); getch(); return 0; }
#define TRUE 1 #define FALSE 0 int nQueens_nonrecursive(int *a, int n) { int top, i, j, conflict; if(n <= 0) return FALSE; top = -1; i = 0; do { conflict = FALSE; /*判断会不会与前面已经放置的发生冲突*/ for(j = 0; j < top+1; j++) if(i==a[j]||top+1-j==i-a[j]||top+1-j==a[j]-i) conflict = TRUE; if(conflict == FALSE) { //如果不冲突 a[++top] = i; //把当前皇后放到第i列 if(top == n-1) return TRUE; //问题已成功解决 i = 0; //从下一行的第0列开始,继续试探 } else { //如果冲突 while(i == n-1 && top >= 0) i = a[top--]; i++; } } while(i < n); return FALSE; }
#include <stdio.h> #include <stdlib.h> #define M 8 // maze数组的行数 #define N 11 // maze数组的列数 typedef struct { int x,y,d; }DataType; struct SeqStack { //顺序栈类型定义 int MAXNUM; int t; // t<MAXNUM,指示栈顶位置,而不是元素个数 DataType *s; }; typedef struct SeqStack *PSeqStack; //顺序栈类型的指针类型 PSeqStack CreateEmptyStack_seq(int n); void push_seq( PSeqStack pastack, DataType x ); void pop_seq( PSeqStack pastack ); int isEmptyStack_seq(PSeqStack pastack); DataType top_seq( PSeqStack pastack ); /*下面函数求从入口maze[x1][y1]到出口maze[x2][y2]的一条路径,其中 1<=x1, x2<=M-2 , 1<=y1, y2<=N-2 */ void mazePath(int** maze, int direction[4][2],int x1,int y1,int x2,int y2,int m,int n) { int i,j,k; int g,h; PSeqStack st; DataType element; st = CreateEmptyStack_seq(m*n); if(st == NULL) return; maze[x1][y1] = 2; //从入口开始进入, 作标记 element.x = x1; element.y = y1; element.d = -1; push_seq(st,element); //入口点进栈 while(!isEmptyStack_seq(st)) { //走不通时, 一步步回退 element = top_seq(st); pop_seq(st); i = element.x; j = element.y; k = element.d + 1; while(k <= 3) { //依次试探每个方向 g = i + direction[k][0]; h = j + direction[k][1]; if(g==x2 && h==y2 && maze[g][h]==0) { //走到出口点 printf("The revers path is:\n"); //打印路径上的每一点 printf("the node is: %d %d \n",g,h); printf("the node is: %d %d \n",i,j); while(!isEmptyStack_seq(st)) { element = top_seq(st); pop_seq(st); printf("the node is: %d %d \n",element.x,element.y); } free(st->s); free(st); return; } if(maze[g][h] == 0) { //走到没走过的点 maze[g][h] = 2; //作标记 element.x = i; element.y = j; element.d = k; push_seq(st,element); //进栈 i = g; //下一点转换成当前点 j = h; k = -1; } k = k + 1; } } printf("The path has not been found.\n"); free(st->s); free(st); } int main() { int maze[M][N] ={ {1,1,1,1,1,1,1,1,1,1,1}, {1,0,1,0,0,1,1,1,0,0,1}, {1,0,0,0,0,0,1,0,0,1,1}, {1,0,1,1,1,0,0,0,1,1,1}, {1,0,0,0,1,0,1,1,0,1,1}, {1,1,0,0,1,0,1,1,0,0,1}, {1,1,1,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1}, }; int direction[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; mazePath(maze,direction,1,1,6,9,M,N); return 0; }
#include <stdio.h> #include <malloc.h> #define DataType char #define TRUE 1 #define FALSE 0 #define MAX_SEQSTACK 100 struct SeqStack { //顺序栈类型定义 int MAXNUM; int t; // t<MAXNUM,指示栈顶位置,而不是元素个数 DataType *s; }; typedef struct SeqStack *PSeqStack; //顺序栈类型的指针类型 /以下5个函数的实现见本章实例13 PSeqStack CreateEmptyStack_seq(); void push_seq(PSeqStack pastack, DataType x); void pop_seq(PSeqStack pastack); int isEmptyStack_seq(PSeqStack pastack); DataType top_seq(PSeqStack pastack); //下面代码将中缀表达式转换为后缀表达式,如成功返回TRUE int infixtoSuffix(char *infix, char *suffix) { int state_int = FALSE; /*记录状态,TRUE表示刚读入的是数字,FALSE表示刚读入的不是数字。 设置这个变量的目的是每输出一个整数后输出一个空格,以免连续输出的两个整数混在一起。*/ char c, c2; int i, j = 0; PSeqStack ps = CreateEmptyStack_seq(); if(ps == NULL) return FALSE; if(infix[0] == '\0') { free(ps->s); free(ps); return FALSE; } for(i = 0; infix[i] != '\0'; i++) { c = infix[i]; switch(c){ case ' ': case '\t': case '\n': if(state_int == TRUE) //从TRUE到FALSE时,输出空格 suffix[j++] = ' '; state_int = FALSE; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': state_int = TRUE; suffix[j++] = c; //遇到数字,输出 break; case '(': if(state_int == TRUE) suffix[j++] = ' '; state_int = FALSE; push_seq(ps, c); break; case ')': if(state_int == TRUE) suffix[j++] = ' '; state_int = FALSE; c2 = ')'; while(!isEmptyStack_seq(ps)) { c2 = top_seq(ps); pop_seq(ps); if(c2 == '(') break; suffix[j++] = c2; } //读入右括号,弹出输出,直到遇到左括号,弹出不输出 if(c2 != '(') { free(ps->s); free(ps); suffix[j++] = '\0'; return FALSE; //找不到左括号,非法 } break; case '+': case '-': if(state_int == TRUE) suffix[j++] = ' '; state_int = FALSE; while(!isEmptyStack_seq(ps)) { c2 = top_seq(ps); if(c2=='+' || c2=='-' || c2=='*' || c2=='/') { pop_seq(ps); suffix[j++] = c2; } //栈顶为加减乘除时,弹出栈顶元素并输出 else break; } push_seq(ps,c); break; case '*': case '/': if(state_int == TRUE) suffix[j++] = ' '; state_int = FALSE; while(!isEmptyStack_seq(ps)) { c2 = top_seq(ps); if(c2 == '*' || c2 == '/' ) { pop_seq(ps); suffix[j++] = c2; } //栈顶为加减乘除时,弹出栈顶元素并输出 else break; } push_seq(ps,c); break; default: //出现其他非法字符 free(ps->s); free(ps); suffix[j++] = '\0'; return FALSE; } } if(state_int == TRUE) suffix[j++] = ' '; while(!isEmptyStack_seq(ps)) { c2 = top_seq(ps); pop_seq(ps); if(c2 == '(') { free(ps->s); free(ps); suffix[j++] = '\0'; return FALSE; } suffix[j++] = c2; } free(ps->s); free(ps); suffix[j++] = '\0'; return TRUE; } /*下面函数用于计算后缀表达式的值,若非法返回FALSE;否则返回TRUE,且*presult存放计算结果*/ int calculateSuffix(char *suffix, int *presult) { int state_int = FALSE; int num = 0, num1, num2; int i; char c; PSeqStack ps = CreateEmptyStack_seq(); if(ps == NULL) return FALSE; for(i = 0; suffix[i] != '\0'; i++) { c = suffix[i]; switch(c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if(state_int == TRUE) num = num*10 + c - '0'; else num = c -'0'; state_int = TRUE; break; case ' ': case '\t': case '\n': if(state_int == TRUE) { push_seq(ps, num); state_int = FALSE; } break; case '+': case '-': case '*': case '/': if(state_int == TRUE) { push_seq(ps, num); state_int = FALSE; } if(isEmptyStack_seq(ps)) { free(ps->s); free(ps); return FALSE; } num2 = top_seq(ps); pop_seq(ps); if(isEmptyStack_seq(ps)) { free(ps->s); free(ps); return FALSE; } num1 = top_seq(ps); pop_seq(ps); if(c == '+') push_seq(ps, num1+num2); if(c == '-') push_seq(ps, num1-num2); if(c == '*') push_seq(ps, num1*num2); if(c == '/') { if(num2 == 0) { //除数为0返回FALSE free(ps->s); free(ps); return FALSE; } push_seq(ps, num1/num2); } break; default: //出现其他非法字符 free(ps->s); free(ps); return FALSE; } } *presult = top_seq(ps); pop_seq(ps); if(!isEmptyStack_seq(ps)) //栈中还有其他字符,非法 { free(ps->s); free(ps); return FALSE; } free(ps->s); free(ps); return TRUE; } int main() { char infix[80] = "20+(3*(4+1)-5)/2-3"; char suffix[80]; int result; if(infixtoSuffix(infix,suffix) == TRUE) { if(calculateSuffix(suffix,&result) == TRUE) printf("The Reuslt is: %3d\n", result); else printf("Error!\n"); } else printf("Input Error!\n"); return 0; }