<p>c++ 实验课的作业,话说这次算是我花的时间最长的实验课作业了,不过的确感觉自己对搜索的理解和编码能力提高了,作为一名初学者,我要学的东西还很多。。。</p><p>这次的实验是推箱子,自己YY了一个自动生成推箱子地图的算法,两重BFS,这大概也是我第一次把ACM的算法放到了实际应用中吧。下面上代码。。。。</p>
class Box
{
public :
Box();
void RandomMaze(int n);
public :
int x,y;
};
class game
{
public:
int WaitKey( double sec );
void SetPos( int x, int y );
};
typedef int Status;
typedef int ElemType;
//using namespace std;
class map
{
public:
Status RandomMaze(int n,int m);
Status PrintMaze(int n) ;
public:
int mazemap[20][20];
};
//using namespace std;
class Vehicle
{
public:
Vehicle();
void show();
void LoadMap(const char *a);
public:
int x,y;
};
#include "Windows.h"
#include "game.h"
#include "ctime"
#include "conio.h"
using namespace std;
int game::WaitKey( double sec )
{
clock_t tickStart = clock();
clock_t tickEnd = tickStart + (clock_t)(sec * CLOCKS_PER_SEC);
while(!kbhit())
{
// time out
if( clock() >= tickEnd)
return 0;
Sleep(sec / 100.0); // wait
}
return getch();
}
void game::SetPos( int x, int y )
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD cursorPos = {x, y};
SetConsoleCursorPosition(hConsole, cursorPos);
}
#include "box.h"
#include "iostream"
#include "map.h"
#include "vehicle.h"
#include "ctime"
#include "Windows.h"
typedef int Status;
typedef int ElemType;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
Status map::RandomMaze(int n,int m)
{
int i,j,k;
srand(time(NULL));
for(i=0;i<n;i++)
mazemap[0][i]=mazemap[n-1][i]=1;
for(j=0;j<n;j++)
mazemap[j][0]=mazemap[j][n-1]=1;
for(i=1;i<n-1;i++)
for(j=1;j<n-1;j++)
{
k=rand()%6; //随机生成整张的地图
if(k==0)
mazemap[i][j]=1;
else
mazemap[i][j]=0;
}
/*
for(i=1;i<=m;i++)
{
int rand_x=rand()%19;
int rand_y=rand()%19;
if(rand_x>=1&&rand_y>=1)
{
mazemap[rand_x][rand_y]=-1; //随机生成箱子的位置
}
else
i--;
}*/
/*
for(i=1;i<=m;i++)
{
int rand_x=rand()%19;
int rand_y=rand()%19;
if(rand_x>=1&&rand_y>=1)
{
mazemap[rand_x][rand_y]=-3; //随机生成箱子的目标位置
}
else
i--;
}*/
mazemap[1][0]=mazemap[1][1]=0;//标记入口出口
return OK;
}
Status map::PrintMaze(int n) //打印最后的足迹
{
//printf("路径如下:\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{