中易网

谁会用MFC编数独游戏,帮帮我 ,急需高手!

答案:1  悬赏:0  
解决时间 2021-11-08 05:46
  • 提问者网友:别再叽里呱啦
  • 2021-11-08 02:30
谁会用MFC编数独游戏,帮帮我 ,急需高手!
最佳答案
  • 二级知识专家网友:人间朝暮
  • 2021-11-08 03:31
#include <stdio.h> #include <stdlib.h> #include <memory.h> typedef unsigned int UINT ; typedef struct tag_Sudu { char **rSudu; char **xNumberStatus; //xNumberStatus[i][x]=y 表示数字i+1放在坐标(x,y),如果y=NOLAY,表示第x列没有放置数字i+1 char **yNumberStatus; //同xNumStatus,不过是按照y方向处理 UINT uEmptyPosition; //记录sudu中总的空格数目。 }Sudu; int xNumberPolicy(Sudu *lpSudu); int yNumberPolicy(Sudu *lpSudu); //可以在这里不断的添加想到的策略. //如果函数的返回值是-1,说明Sudu的当前状态是错误的,就是所Sudu无解. //如果函数的返回值是0,说明Sudu暂时是合法的,就是说可能有解. typedef int(*pFunPolicy)(Sudu *lpSudu); pFunPolicy pfnPolicy[] = { xNumberPolicy, yNumberPolicy }; #define NOLAY -1 #define XVALID(lpSudu, index, x) ((lpSudu)->xNumberStatus[index][x] == NOLAY) #define YVALID(lpSudu, index, y) ((lpSudu)->yNumberStatus[index][y] == NOLAY) #define EMPTYGRID(lpSudu, x, y) ((lpSudu)->rSudu[y][x] == 0) enum LINEDETECT { MOREPOSITION = -2, NOPOSITION }; int gRow,gXCell, gYCell; int Init(Sudu *lpSudu) { int x,y; char data; FILE *fp = fopen("sudu.txt","r+"); if (fp == NULL) { return -1; } fscanf(fp,"%d\n%d\n", &gXCell, &gYCell); gRow = gXCell * gYCell; lpSudu->uEmptyPosition = 0; lpSudu->rSudu = (char**)malloc(sizeof(char*) * gRow + sizeof(char) * gRow * gRow); lpSudu->xNumberStatus = (char**)malloc(sizeof(char*) * gRow + sizeof(char) * gRow * gRow); lpSudu->yNumberStatus = (char**)malloc(sizeof(char*) * gRow + sizeof(char) * gRow * gRow); for (x=0; x<gRow; ++x) { lpSudu->rSudu[x] = (char*)((char*)lpSudu->rSudu + sizeof(char*) * gRow + sizeof(char) * gRow * x); lpSudu->xNumberStatus[x] = (char*)((char*)lpSudu->xNumberStatus + sizeof(char*) * gRow + sizeof(char) * gRow * x); lpSudu->yNumberStatus[x] = (char*)((char*)lpSudu->yNumberStatus + sizeof(char*) * gRow + sizeof(char) * gRow * x); } memset((char*)lpSudu->xNumberStatus + sizeof(char*) * gRow, NOLAY, sizeof(char) * gRow * gRow); memset((char*)lpSudu->yNumberStatus + sizeof(char*) * gRow, NOLAY, sizeof(char) * gRow * gRow); //转换为内部表示 for (y=0; y<gRow; ++y) { for (x=0; x<gRow; ++x) { fscanf(fp,"%c ", &data); if (data>='0' && data<='9') { data = data - '0'; } else if (data>='a' && data<='z') { data = data - 'a' + 10; } else if (data>='A' && data<='Z') { data = data - 'A' + 10; } else { printf("File Data Error.\n"); fclose(fp); return -1; } if (data == 0) { ++lpSudu->uEmptyPosition; } else { lpSudu->xNumberStatus[data-1][x] = y; lpSudu->yNumberStatus[data-1][y] = x; } lpSudu->rSudu[y][x] = data; } fscanf(fp,"\n"); } fclose(fp); return 0; } //本函数假设srcSudu有内容,dstSudu没有分配空间. void copySudu(Sudu *srcSudu, Sudu *dstSudu) { int x,size; dstSudu->rSudu = (char**)malloc(sizeof(char*) * gRow + sizeof(char) * gRow * gRow); dstSudu->xNumberStatus = (char**)malloc(sizeof(char*) * gRow + sizeof(char) * gRow * gRow); dstSudu->yNumberStatus = (char**)malloc(sizeof(char*) * gRow + sizeof(char) * gRow * gRow); for (x=0; x<gRow; ++x) { dstSudu->rSudu[x] = (char*)((char*)dstSudu->rSudu + sizeof(char*) * gRow + sizeof(char) * gRow * x); dstSudu->xNumberStatus[x] = (char*)((char*)dstSudu->xNumberStatus + sizeof(char*) * gRow + sizeof(char) * gRow * x); dstSudu->yNumberStatus[x] = (char*)((char*)dstSudu->yNumberStatus + sizeof(char*) * gRow + sizeof(char) * gRow * x); } size = gRow * gRow ; dstSudu->uEmptyPosition = srcSudu->uEmptyPosition; memcpy(dstSudu->rSudu[0], srcSudu->rSudu[0], size); memcpy(dstSudu->xNumberStatus[0], srcSudu->xNumberStatus[0], size); memcpy(dstSudu->yNumberStatus[0], srcSudu->yNumberStatus[0], size); } //释放Sudu成员所申请的空间 void freeSudu(Sudu *lpSudu) { free(lpSudu->rSudu); free(lpSudu->xNumberStatus); free(lpSudu->yNumberStatus); } void print_xNumberStatus(Sudu *lpSudu) { int index,x; printf("\n"); for (index=0; index<gRow; ++index) { for (x=0; x<gRow; ++x) { printf("%2d ",lpSudu->xNumberStatus[index][x]); } printf("\n"); } } void print_yNumberStatus(Sudu *lpSudu) { int index,y; printf("\n"); for (index=0; index<gRow; ++index) { for (y=0; y<gRow; ++y) { printf("%2d ",lpSudu->yNumberStatus[index][y]); } printf("\n"); } } void print_rSudu(Sudu *lpSudu) { int x,y; printf("\niEmptyPos:%d\n",lpSudu->uEmptyPosition); for (y=0; y<gRow; ++y) { if (y % gYCell == 0) { for (x=0; x<gRow; ++x) { printf("----"); } printf("\n"); } for (x=0; x<gRow; ++x) { if (x % gXCell == 0) printf("|"); printf("%2d ",lpSudu->rSudu[y][x]); } printf("\n"); } } //返回值 // 0:在一个CELL中没有相同的number // 1:在一个CELL中有相同的number int inSameCell(Sudu *lpSudu,int x, int y, int number) { int top,left; int i,j; left = x / gXCell * gXCell; top = y / gYCell * gYCell; for (i=top; i<top+gYCell; ++i) { for (j=left; j<left+gXCell; ++j) { if (lpSudu->rSudu[i][j] == number) { return 1; } } } return 0; } void putNumber(Sudu *lpSudu, int _x, int _y, int _number) { lpSudu->rSudu[_y][_x] = _number; lpSudu->xNumberStatus[_number-1][_x] = _y; lpSudu->yNumberStatus[_number-1][_y] = _x; --lpSudu->uEmptyPosition; } //返回-1说明rSudu是矛盾的。 //返回0说明rSudu不矛盾. int xNumberPolicy(Sudu *lpSudu) { int index; int x,y; int putPosition; for (index=0; index < gRow; ++index) { for (x=0; x<gRow; ++x) { if (XVALID(lpSudu, index, x)) { putPosition = NOPOSITION; for (y=0; y<gRow; ++y) { if (EMPTYGRID(lpSudu, x, y) && YVALID(lpSudu, index, y)) { if (!inSameCell(lpSudu, x, y, index+1)) { if (putPosition == NOPOSITION) { putPosition = y; } else { putPosition = MOREPOSITION; break; } } else { y = (y / gYCell +1)*gYCell-1; } } } if (putPosition == NOPOSITION) { return -1; } if (putPosition != MOREPOSITION) { putNumber(lpSudu, x, putPosition, index+1); } } } } return 0; } int yNumberPolicy(Sudu *lpSudu) { int index; int x,y; int putPosition; for (index=0; index < gRow; ++index) { for (y=0; y<gRow; ++y) { if (YVALID(lpSudu, index, y)) { putPosition = NOPOSITION; for (x=0; x<gRow; ++x) { if (EMPTYGRID(lpSudu, x, y) && XVALID(lpSudu, index, x)) { if (!inSameCell(lpSudu, x, y, index+1)) { if (putPosition == NOPOSITION) { putPosition = x; } else { putPosition = MOREPOSITION; break; } } else { x = (x / gXCell +1)*gXCell-1; } } } if (putPosition == NOPOSITION) { return -1; } if (putPosition != MOREPOSITION) { putNumber(lpSudu, putPosition, y, index+1); } } } } return 0; } void findOptimum(Sudu *lpSudu,int *_index, int *_x, int *_min, char *_yPos) { int x,y; int index; int iCount; char *yPos; int i; i=0; yPos = (char*)malloc(sizeof(char) * gRow); *_min = gRow+1; for (index=0; index<gRow; ++index) { for (x=0; x<gRow; ++x) { if (XVALID(lpSudu,index,x)) { iCount =0; for (y=0; y<gRow; ++y) { if (EMPTYGRID(lpSudu,x,y) && YVALID(lpSudu,index,y)) { if (!inSameCell(lpSudu,x, y, index+1)) { i++; yPos[iCount++] = y; if (iCount >= *_min) { break; } }else{ y = (y/gYCell + 1) * gYCell - 1; } } } if (*_min > iCount) { *_min = iCount; *_index = index; *_x = x; memcpy(_yPos, yPos, iCount); } } } } delete yPos; } void sloveSudu(Sudu *lpSudu) { UINT temp; Sudu imgSudu; char *yPosition; int minCount; int index; int xPos; int i; int cntPcy; temp = lpSudu->uEmptyPosition; while (lpSudu->uEmptyPosition > 0) { for (cntPcy=0; cntPcy<sizeof(pfnPolicy)/sizeof(pFunPolicy); cntPcy++) { if (pfnPolicy[cntPcy](lpSudu) == -1) return; if (lpSudu->uEmptyPosition == 0 ) { print_rSudu(lpSudu); } } if ( temp == lpSudu->uEmptyPosition ) { //break; yPosition = (char*)(malloc)(sizeof(char) * gRow); findOptimum(lpSudu, &index, &xPos, &minCount, yPosition); //number = index + 1 for (i=0; i<minCount; ++i) { copySudu(lpSudu, &imgSudu); putNumber(&imgSudu, xPos, yPosition[i], index+1); sloveSudu(&imgSudu); freeSudu(&imgSudu); } delete yPosition; return; //是return 还是break呢? } temp = lpSudu->uEmptyPosition; } } int main() { Sudu stSudu; if (Init(&stSudu) == -1) { printf("Init failed.\n"); return -1; } print_rSudu(&stSudu); sloveSudu(&stSudu); freeSudu(&stSudu); return 0; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息