Re: 請問老鼠走迷宮

看板Programming作者 (甘願為妳~)時間17年前 (2008/08/10 17:56), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《grangekl.bbs@moca.csie.chu.edu.tw (小偉)》之銘言: : 我的堆疊之老鼠走迷宮程式 老鼠是一次跑完 有沒有辦法讓畫面的老鼠是一步一步走完 : 程式大概: : 會動的老鼠怎麼顯示 我不想畫面一下就印出牠跑完 至少要慢慢走=.=a // test.cpp : Defines the entry point for the console application. // // http://202.193.64.35/dept7/acm/web/AlgorithmGossip/MouseGoMaze.htm // // Note: 參考上面的網址,所改寫的,你可以Trace下面的Code , // 關鍵應該是UpdateMap()。 // #include "stdafx.h" #include "test.h" #include "stdafx.h" #include "test.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif int visit(int, int); void UpdateMap(); int maze[][12] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 2, 2, 0, 0, 2, 0, 2, 0, 0, 2, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2, 0, 0, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 2, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 2, 2, 0, 0, 2, 0, 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }; int startI = 1, startJ = 1; // 入口 int endI = 10, endJ = 10; // 出口 int success = 0; CWinApp theApp; using namespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { UpdateMap(); visit(startI, startJ); return 0; } void UpdateMap() { HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE ); int i, j; COORD pos = {0, 0}; SetConsoleCursorPosition( hConsole, pos ); Sleep(300); for(i = 0; i < 12; i++) { for(j = 0; j < 12; j++) { if(maze[i][j] == 2) printf("█"); else if(maze[i][j] == 1) printf("◇"); else printf(" "); } printf("\n"); } } int visit(int i, int j) { maze[i][j] = 1; UpdateMap(); if(i == endI && j == endJ) success = 1; if(success != 1 && maze[i][j+1] == 0) visit(i, j+1); if(success != 1 && maze[i+1][j] == 0) visit(i+1, j); if(success != 1 && maze[i][j-1] == 0) visit(i, j-1); if(success != 1 && maze[i-1][j] == 0) visit(i-1, j); if(success != 1) maze[i][j] = 0; UpdateMap(); return success; } -- ████████████████ █ █████████████ ██████████████ █ ██████████████ ██████████████ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.110.225.153
文章代碼(AID): #18dhixuB (Programming)
文章代碼(AID): #18dhixuB (Programming)