马踏棋盘 贪心

    xiaoxiao2021-11-24  35

    #include<iostream>; using namespace std; struct ChessStep { int x; int y; int haveVisiteNum;//已经访问过的次数 ChessStep* next; }; class ChessBoard { private: int isVisit[8][8]; int haveVisit; ChessStep* topStep; int Htry1[8] = { -2,-1,1,2,2,1,-1,-2 }; int Htry2[8] = { 1,2,2,1,-1,-2,-2,-1 }; int FuncNum; public: ChessBoard() { topStep = new ChessStep(); topStep->x = 0; topStep->y = 0; topStep->haveVisiteNum = 0; topStep->next = NULL; FuncNum = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { isVisit[i][j] = 0; } } haveVisit = 1; isVisit[0][0] = 1; } ChessBoard(int x, int y) { topStep = new ChessStep(); topStep->x = x; topStep->y = y; topStep->haveVisiteNum = 0; topStep->next = NULL; FuncNum = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { isVisit[i][j] = 0; } } haveVisit = 1; isVisit[x][y] = 1; } void addStep(int x, int y); void unStep();//栈顶删除 int checkVisit(int x, int y); void Run(); void autoRun(); void printRoad(); }; void ChessBoard::printRoad() { int i, j; // nCount++; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { printf("%-4d", isVisit[i][j]); } printf("\n"); } printf("\n\n"); } int ChessBoard::checkVisit(int x, int y) { if (isVisit[x][y] == 0) { int shaobin = 0; for (int i = 0; i < 8; i++) { if ((x + Htry1[i]) < 8 && (y + Htry2[i]) < 8 && (x + Htry1[i]) >= 0 && (y + Htry2[i]) >= 0) { if (!isVisit[x + Htry1[i]][y + Htry2[i]]) { shaobin++; } } } return shaobin; } else { return -1; } } void ChessBoard::addStep(int x, int y) { ChessStep* newStep = new ChessStep(); newStep->next = topStep; newStep->x = x; newStep->y = y; newStep->haveVisiteNum = 0; isVisit[x][y] = ++haveVisit; topStep = newStep; } void ChessBoard::unStep() { isVisit[topStep->x][topStep->y] = 0; haveVisit--; ChessStep* temp = topStep; topStep = topStep->next; delete temp; } void ChessBoard::Run() { int toCheck, checkList[8], min = 8, minId = -1, checkNum = 0; for (int i = 0; i < 8; i++) { int rX = topStep->x + Htry1[i], rY = topStep->y + Htry2[i]; if (isVisit[rX][rY] == 0 && rX < 8 && rY < 8 && rX >= 0 && rY >= 0) { int check = checkVisit(topStep->x + Htry1[i], topStep->y + Htry2[i]); checkList[i] = check; checkNum++; } else { checkList[i] = 8; } } if (checkNum-topStep->haveVisiteNum != 0) { for (int i = 0; i < topStep->haveVisiteNum; i++) {//这里是为了删掉已经走过的路 min = -1; minId = 0; for (int j = 0; j < 8; j++) { if (checkList[i] < min) { min = checkList[i]; minId = i; } } checkList[minId] = 8; } minId = -1; min = 8; for (int i = 0; i < 8; i++) { if (checkList[i] < min) { min = checkList[i]; minId = i; } } } toCheck = minId; //cout << "to Check " << toCheck << endl; if (toCheck == -1) { unStep(); } else { topStep->haveVisiteNum++; addStep(topStep->x + Htry1[toCheck], topStep->y + Htry2[toCheck]); if (haveVisit == 64) { FuncNum++; printRoad(); unStep(); } } } void ChessBoard::autoRun() { while (FuncNum < 500) { Run(); } } int main() { ChessBoard b; b.autoRun(); return 0; }

    //大概2秒钟500次的速度,比不用贪心还是快很多

    //改进了一点,之前判断是否访问的方法有问题,不能找到所有结果,现在已完善

    转载请注明原文地址: https://ju.6miu.com/read-678490.html

    最新回复(0)