作者ast9869 (甘愿为你~)
看板Programming
标题Re: 请问老鼠走迷宫
时间Sun Aug 10 17:56:08 2008
※ 引述《[email protected] (小伟)》之铭言:
: 我的堆叠之老鼠走迷宫程式 老鼠是一次跑完 有没有办法让画面的老鼠是一步一步走完
: 程式大概:
: 会动的老鼠怎麽显示 我不想画面一下就印出牠跑完 至少要慢慢走=.=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