赞
踩
马踏棋盘
#undef UNICODE #undef _UNICODE //取消unicode的定义 #include<stdio.h> #include<graphics.h> #include<windows.h> #include<stdlib.h> #define Row 8 #define Col 8 #define maxStep 64 #define White 1 #define Yellow 0 typedef struct QI { int x; int y; int i; int j; }QI; typedef struct { int abscissa; //横坐标 int ordinate; //纵坐标 int direction; //方向 }SqStack; int ChessBoard[Row + 1][Col + 1] = {}; //储存路径的棋盘 //分别有(1 ~ 8)个方向 int HTry1[8] = { 1, -1, -2, 2, 2, 1, -1, -2 }; int HTry2[8] = { 2, -2, 1, 1, -1, -2, 2, -1 }; SqStack PointStack[maxStep]; int top = -1; //栈顶 int flagOperate = 0; //操作标记 int num = 0; //记录结果数 int Q[8][8] = { {1,0,1,0,1,0,1,0}, {0,1,0,1,0,1,0,1}, {1,0,1,0,1,0,1,0}, {0,1,0,1,0,1,0,1}, {1,0,1,0,1,0,1,0}, {0,1,0,1,0,1,0,1}, {1,0,1,0,1,0,1,0}, {0,1,0,1,0,1,0,1}, }; int start_x = 0; int start_y = 0; int Re_i = 0; int Re_j = 0; int Window_Wide = 900; int Window_High = 700; int qipan_start = 30; int qipan_end = 670; int qige_wide = 80; int menu_start = 700; int menu_end = 870; int step = 0; void basic(); void marke(int x, int y); void Show(int x, int y, int flag); void Reback(int x, int y, int flag); int transmit_x_to_j(int x); int transmit_j_to_x(int x); void printChessBoard(); void push(int abscissa, int ordinate); void pop(); void printStack(); void markChessBoard(int abscissa, int ordinate); void runChessBoard(); void InitStartPoint(); void marke(int x, int y); int main() { int x; int y; int i; int j; int flag; int MA = 0; initgraph(Window_Wide, Window_High, SHOWCONSOLE); basic(); MOUSEMSG m; // 定义消息变量 while (!MA) { m = GetMouseMsg(); switch (m.uMsg) { case WM_LBUTTONDOWN: if (m.x >= qipan_start && m.x <= qipan_end && m.y >= qipan_start && m.y <= qipan_end) { j = transmit_x_to_j(m.x); i = transmit_x_to_j(m.y); flag = Q[i][j]; x = transmit_j_to_x(j); y = transmit_j_to_x(i); printf("%d,%d", i + 1, j + 1); Show(x, y, flag); push(j + 1, i + 1); markChessBoard(j + 1, i + 1); runChessBoard(); MA = 1;//标志马的初始位置已经确定 break; } else ; } } getchar(); } void basic() { IMAGE img; int left = qipan_start; int right = qipan_end; int top = qipan_start; int bottom = qipan_end; setlinecolor(RED); int x = 0, y = 0; x = left; y = top; loadimage(&img, _T("棋盘.png"), 640, 640); putimage(30, 30, &img); rectangle(left, top, right, bottom); for (int i = 0; i < 7; i++) { x += qige_wide; y += qige_wide; line(x, top, x, bottom); line(left, y, right, y); } setcolor(WHITE); setfillcolor(RGB(250, 228, 178)); fillrectangle(0, 0, 900, 30); fillrectangle(0, 30, 30, 670); fillrectangle(870, 30, 900, 700);//四周边框设计 fillrectangle(670, 30, 700, 700); fillrectangle(0, 670, 900, 700); setfillcolor(RGB(255, 205, 134)); fillrectangle(700, 30, 870, 670); setcolor(RGB(209, 139, 25)); rectangle(700, 30, 870, 670); } void Show(int x, int y, int flag) { IMAGE img1; IMAGE img2; IMAGE img3; step++; loadimage(&img1, _T("马1.png"), qige_wide - 20, qige_wide - 20); loadimage(&img2, _T("马2.png"), qige_wide - 20, qige_wide - 20); if (flag == Yellow) { putimage(x + 10, y + 10, &img1); Re_i = transmit_x_to_j(y); Re_j = transmit_x_to_j(x); } else if (flag == White) { putimage(x + 10, y + 10, &img2); Re_i = transmit_x_to_j(y); Re_j = transmit_x_to_j(x); } else printf("Show函数出错了!!!\n"); Sleep(100); Reback(x, y, flag); return; } void Reback(int x, int y, int flag) { IMAGE img1, img2; loadimage(&img1, _T("Reback1.png"), qige_wide - 20, qige_wide - 20); loadimage(&img2, _T("Reback2.png"), qige_wide - 20, qige_wide - 20); if (flag == White) putimage(x + 10, y + 10, &img2); else if (flag == Yellow) putimage(x + 10, y + 10, &img1); else printf("Reback出错了!!!\n"); marke(x, y); return; } int transmit_x_to_j(int x) { int j = 0; j = (x - qipan_start) / qige_wide; return j; } int transmit_j_to_x(int j) { int x = 0; x = qipan_start + j * qige_wide; return x; } //int caculate(int x, int y) { //} void marke(int x, int y) { char str[5] = { "0" }; int num_x; int num_y; num_x = x + 1 * qige_wide / 5; num_y = y + qige_wide / 5; setbkmode(TRANSPARENT); settextcolor(BLACK); settextstyle(3 * qige_wide / 5, 0, _T("Consolas")); sprintf_s(str, "%d", step); outtextxy(num_x, num_y, str); } void printChessBoard() { char str[5]; int num_x; int num_y; int flag = 0; setbkmode(TRANSPARENT); settextcolor(BLACK); settextstyle(3 * qige_wide / 4, 0, _T("Consolas")); printf("棋盘路径是:\n"); while(step!=64){ for (int i = 1; i <= Row; i++) { for (int j = 1; j <= Col; j++) { printf("%5d ", ChessBoard[i][j]); if (ChessBoard[i][j] == step+1) { num_x = transmit_j_to_x(j-1); num_y = transmit_j_to_x(i-1); flag = Q[i - 1][j - 1]; Show(num_x, num_y, flag); } } printf("\n"); } } printf("\n\n"); } //入栈 void push(int abscissa, int ordinate) { ++top; PointStack[top].abscissa = abscissa; PointStack[top].ordinate = ordinate; PointStack[top].direction = -1; //初始化方向 } //出栈 void pop() { PointStack[top].abscissa = 0; PointStack[top].ordinate = 0; PointStack[top].direction = -1; //初始化方向 --top; } //标记棋盘 void markChessBoard(int abscissa, int ordinate) { ChessBoard[ordinate][abscissa] = top + 1; } void runChessBoard() { int xNow, yNow; //当前马所在的坐标 while (1) { if (top == maxStep - 1) { printChessBoard(); break; } xNow = PointStack[top].abscissa; yNow = PointStack[top].ordinate; //对下面八个方向重新排序,出口最少的优先 int count[8] = {}; for (int i = 0; i < 8; i++) { int xNext = xNow, yNext = yNow; xNext += HTry1[i]; yNext += HTry2[i]; if (xNext > 0 && xNext <= Col && yNext > 0 && yNext <= Row && ChessBoard[yNext][xNext] == 0) { for (int j = 0; j < 8; j++) { int xNextNext = xNext, yNextNext = yNext; xNextNext += HTry1[j]; yNextNext += HTry2[j]; if (xNextNext > 0 && xNextNext <= Col && yNextNext > 0 && yNextNext <= Row && ChessBoard[yNextNext][xNextNext] == 0) { count[i]++; } } } } //对方向进行排序,实际要走的方向记录在directionNext中 int directionNext[8] = {}; int temp = 9; int k = 0; for (int i = 0; i < 8; i++) { temp = 9; for (int j = 0; j < 8; j++) { if (count[j] < temp) { directionNext[i] = j; temp = count[j]; k = j; } } count[k] = 9; } //走下一步 int direnow = 0; int i = 1; for (direnow = PointStack[top].direction + 1; direnow < 8; direnow++) { int xRealNext = xNow, yRealNext = yNow; xRealNext += HTry1[directionNext[direnow]]; yRealNext += HTry2[directionNext[direnow]]; PointStack[top].direction += 1; if (xRealNext <= Col && xRealNext > 0 && yRealNext <= Row && yRealNext > 0 && ChessBoard[yRealNext][xRealNext] == 0) { push(xRealNext, yRealNext); // 标记在棋盘路径中 markChessBoard(xRealNext, yRealNext); break; } } //判断无路可走出栈 if (PointStack[top].direction >= 7) { int x, y; x = PointStack[top].abscissa; y = PointStack[top].ordinate; ChessBoard[y][x] = 0; //棋盘标记取消 pop(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。