八数码问题的暴力求解

来源:互联网 发布:php 发送邮件 编辑:程序博客网 时间:2024/05/17 07:28
#include <stdio.h>#include <conio.h>//kbhit()函数的头文件#include <string.h>#include <time.h>//时间头文件struct Node { char a[10]; int from; };Node Q[181440] = { {"123456780", -1} };//9的全排列除以2可以保存全部状态int num = 0;FILE *fp1, *fp2;void swap(char&, char&);void insert(char*, int);void main(){char b[10], *p;    int i, pos;fp2 = fopen("EightDigit.txt", "wt");//打开txt将所有的状态写入文件insert("123456780",-1);clock_t t1 = clock();//得到当前时间 clock_t 相当于 int     for(i=0;i<num;++i){        for(pos=0;pos<9 && Q[i].a[pos]!='0';++pos); // pos = strchr(Q[i].a, '0') - Q[i].a;switch(pos){case 0:     strcpy(b, Q[i].a);            swap(b[0],b[1]);insert(b, i);     strcpy(b, Q[i].a);swap(b[0],b[3]);insert(b, i);break;case 1:     strcpy(b, Q[i].a);            swap(b[1],b[0]);  // 0 move to leftinsert(b, i);     strcpy(b, Q[i].a);swap(b[1],b[2]);  // 0 move to rightinsert(b, i);     strcpy(b, Q[i].a);            swap(b[1],b[4]);  // 0 move to downinsert(b, i);break;case 2:     strcpy(b, Q[i].a);            swap(b[2],b[1]);  // leftinsert(b, i);     strcpy(b, Q[i].a);swap(b[2],b[5]);  // downinsert(b, i);break;case 3:     strcpy(b, Q[i].a);            swap(b[3],b[4]);  // 0 move to rightinsert(b, i);     strcpy(b, Q[i].a);swap(b[3],b[0]);  // 0 move to upinsert(b, i);     strcpy(b, Q[i].a);            swap(b[3],b[6]);  // 0 move to downinsert(b, i);break;case 4:     strcpy(b, Q[i].a);            swap(b[4],b[3]);  // 0 move to leftinsert(b, i);     strcpy(b, Q[i].a);swap(b[4],b[5]);  // 0 move to rightinsert(b, i);     strcpy(b, Q[i].a);             swap(b[4],b[1]);  // 0 move to upinsert(b, i);     strcpy(b, Q[i].a);             swap(b[4],b[7]);  // 0 move to downinsert(b, i);break;case 5:     strcpy(b, Q[i].a);            swap(b[5],b[4]);  // 0 move to leftinsert(b, i);     strcpy(b, Q[i].a);swap(b[5],b[2]);  // 0 move to upinsert(b, i);     strcpy(b, Q[i].a);             swap(b[5],b[8]);  // 0 move to downinsert(b, i);break;case 6:     strcpy(b, Q[i].a);            swap(b[6],b[7]);  // rightinsert(b, i);     strcpy(b, Q[i].a);swap(b[6],b[3]);  // upinsert(b, i);break;case 7:     strcpy(b, Q[i].a);            swap(b[7],b[4]);  // 0 move to upinsert(b, i);     strcpy(b, Q[i].a);swap(b[7],b[6]);  // 0 move to leftinsert(b, i);     strcpy(b, Q[i].a);             swap(b[7],b[8]);  // 0 move to rightinsert(b, i);break;case 8:     strcpy(b, Q[i].a);            swap(b[8],b[7]);  // leftinsert(b, i);     strcpy(b, Q[i].a);swap(b[8],b[5]);  // upinsert(b, i);break;}}fp1 = fopen("EightDigit.dat", "wb");//二进制文件fwrite(Q, sizeof(Q), 1, fp1);                      //写入二进制文件fclose(fp1);fclose(fp2);clock_t t2 = clock();//得到当前时间printf("%.2lf seconds.\n", (double)(t2-t1)/CLOCKS_PER_SEC);//得到的并不是秒时间单位,需要除以某个脉冲printf("Enter your expected state: ");gets(b);for(i=0; i<num; ++i){if(strcmp(Q[i].a, b) == 0) break;}if(i == num)puts("No solution!");else{while(i!=-1){puts(Q[i].a);i=Q[i].from;}}}void insert(char* b, int f){for(int i=num-1; i>=0 && strcmp(Q[i].a, b) != 0; --i) ;// 判断b是否出现过if(i < 0){strcpy(Q[num].a, b);//进队列Q[num].from = f;fprintf(fp2, "%d: %s %d\n", num, b, f);// 写入文件num++;//num++!!!}if(kbhit())//点击一次屏幕中出现一次{getch();printf("%d: %s %d\n", num-1, Q[num-1].a, Q[num-1].from);}}void swap(char& a, char& b){char t = a; a = b; b = t;}

0 0
原创粉丝点击