正在加载图片...
附录八皇后问题并行算法的MP源程序 1源程序 Queens.c #include <stdio h> seed++. i while(seed <=QUE #define MAX Solutions 92 & collides(, seed/ QUEENS seed QUEENS)) typedef const int true =1 if(seed >QUEENS+ QUEENS-1) const int false =0 return 0 else enum msg content ACCOMPLISHED void print se oun NEW TASK int solutionsIlQUEENSD TERMINATE int i,j, k, enum msg tag for(i=0; 1< count; i++) REQUEST TAG, SEED TAG for G=0; j<QUEENS; j++) REPLY TAG NUM SOLUTIONS TAG printf("%d"solutions[0D; SOLUTIONS TAG for(k=0, k< solutions: k++) printf("*"); int solutions [MAX SOLUTIONSJIQUEENS] for(k= QUEENS-1; int solution count =0: k> solutions[Q0); k-) bool collides( int row l, int coll, int row2, int col2) printf("n); return(coll = col2) owI-row2 l(coll +rowl=col2+ row2) 1/ print solutions"/ }/ collides’ bool is safe( int chessboard[, int row, int col) static int seed=013 附录 八皇后问题并行算法的 MPI 源程序 1.源程序 Pqueens.c #include <mpi.h> #include <stdio.h> #define QUEENS 8 #define MAX_SOLUTIONS 92 typedef int bool; const int true = 1; const int false = 0; enum msg_content { READY, ACCOMPLISHED, NEW_TASK, TERMINATE }; enum msg_tag { REQUEST_TAG, SEED_TAG, REPLY_TAG, NUM_SOLUTIONS_TAG, SOLUTIONS_TAG }; int solutions[MAX_SOLUTIONS][QUEENS]; int solution_count = 0; bool collides(int row1, int col1, int row2, int col2) { return (col1 == col2) || (col1 - col2 == row1 - row2) || (col1 + row1 == col2 + row2); } /* collides */ int generate_seed() { static int seed = 0; do { seed++; } while (seed <= QUEENS * QUEENS – 1 && collides(0, seed / QUEENS, 1, seed % QUEENS)); if (seed > QUEENS * QUEENS - 1) return 0; else return seed; } /* generate_seed */ void print_solutions(int count, int solutions[][QUEENS]) { int i, j, k; for (i = 0; i < count; i++) { printf("Solution %d :\n", i + 1); for (j = 0; j < QUEENS; j++) { printf("%d ", solutions[i][j]); for (k = 0; k < solutions[i][j]; k++) printf("- "); printf("* "); for (k = QUEENS - 1; k > solutions[i][j]; k--) printf("- "); printf("\n"); } printf("\n"); } } /* print_solutions */ bool is_safe(int chessboard[], int row, int col) { int i;
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有