================================main.cpp===========================================
/*
*Copyright(c)2010, xingwangshiyi
*All rights reserved.
*
*文件名: main.cpp
*摘 要:猫抓老鼠
*作 者:许文发
*时 间:2010-4-23
*/
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "queue.h"
#include "main.h"
using namespace std;
int SearchWay(pQueue list, char **maze, int xlen, int ylen, int xcat, int ycat);
int PaintWay(pQueue list, char **maze, int xlen, int ylen);
int main()
{
int xlen = 0;
int ylen = 0;
int xcat = 0;
int ycat = 0;
int xmouse = 0;
int ymouse = 0;
int num = 0;
pQueue que = NULL;
int i = 0;
char **maze = NULL;
cout<<"请输入迷宫的长度和宽度:";
cin>>xlen;
cin>>ylen;
cout<<endl<<"请输入障碍物数目(小于迷宫的长*宽):";
cin>>num;
while (num > xlen*ylen)
{
cout<<endl<<"超出!请输入障碍物数目(小于迷宫的长*宽):";
cin>>num;
}
cout<<endl<<"请输入猫的位置:";
cin>>xcat;
cin>>ycat;
while (xcat < 0 || ycat >= ylen || ycat < 0 || xcat >= xlen)
{
cout<<endl<<"出界!请输入猫的位置:";
cin>>xcat;
cin>>ycat;
}
cout<<endl<<"请输入老鼠的位置:";
cin>>xmouse;
cin>>ymouse;
while (xmouse < 0 || ymouse >= ylen || ymouse < 0 || xmouse >= xlen)
{
cout<<endl<<"出界!请输入老鼠的位置:";
cin>>xmouse;
cin>>ymouse;
}
que = QueueCreate(xlen*ylen); //创建队列
if (que == NULL)
{
return 1;
}
QueueInit(que); //初始化队列
maze = CreateMaze(xlen, ylen, num, xcat, ycat, xmouse, ymouse); //创建迷宫
PrintMaze(maze, xlen, ylen); //打印迷宫
cout<<"猫在寻找中……"<<endl;
if (SearchWay(que, maze, xlen, ylen, xcat, ycat)) //搜索
{
PaintWay(que, maze, xlen, ylen); //打印路径
PrintMaze(maze, xlen, ylen);
cout<<"猫:哈哈!抓到你了吧,小样"<<endl;
}
else
{
cout<<"老鼠:哈哈,抓不到我!"<<endl;
}
return 0;
}
/*
*函数名:CreateMaze
*输 入:int xlen, int ylen, int num, int xcat, int ycat, int xmouse, int ymouse
*输 出:
*返 回:char **
*功 能:创建迷宫,并放置猫和老鼠
*/
char **CreateMaze(int xlen, int ylen, int num, int xcat, int ycat, int xmouse, int ymouse)
{
char **maze = NULL;
int i = 0;
int j = 0;
int k = 0;
maze = new char *[xlen];
for (i = 0; i < xlen; i++)
{
maze[i] = new char[ylen];
}
srand((unsigned)time(NULL));
for (i = 0; i < xlen; i++)
{
for (j = 0; j < ylen; j++)
{
maze[i][j] = '0';
}
}
maze[xcat][ycat] = 'C'; //放置猫
maze[xmouse][ymouse] = 'M'; //放置老鼠
while (k < num)
{
i = rand()%xlen;
j = rand()%ylen;
if (maze[i][j] == '0')
{
maze[i][j] = '*';
k++;
}
}
return maze;
}
/*
*函数名:PrintMaze
*输 入:char **maze, int xlen, int ylen
*输 出:
*返 回:int
*功 能:打印迷宫
*/
int PrintMaze(char **maze, int xlen, int ylen)
{
int i = 0;
int j = 0;
for (i = 0; i < xlen; i++)
{
for (j = 0; j < ylen; j++)
{
cout<<maze[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
/*
*函数名:SearchWay
*输 入:pQueue list, char **maze, int xlen, int ylen, int xcat, int ycat
*输 出:
*返 回:int
*功 能:搜索路径
*/
int SearchWay(pQueue list, char **maze, int xlen, int ylen, int xcat, int ycat)
{
NODE cat = {-1, 0, xcat, ycat};
int des[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};
int i = 0;
int j = 0;
int k = 0;
QueueAdd(list, cat);
while(!QueueEmpty(list))
{
for (k = 0; k < 4; k++)
{
i = list->buf[list->front].mPos.x + des[k][0];
j = list->buf[list->front].mPos.y + des[k][1];
if (i >= 0 && i < xlen && j >= 0 && j < ylen && (maze[i][j] == '0' || maze[i][j] == 'M'))
{
cat.mIndex = list->rear;
cat.pIndex = list->front;
cat.mPos.x = i;
cat.mPos.y = j;
QueueAdd(list, cat); //入队
if (maze[i][j] == 'M')
{
return 1; //如果是老鼠,返回1
}
maze[i][j] = '1'; //标记已搜索的位置
}
else
{
continue;
}
}
QueueDel(list);
}
return 0;
}
/*
*函数名:PaintWay
*输 入:pQueue list, char **maze, int xlen, int ylen
*输 出:
*返 回:int
*功 能:标记路径
*/
int PaintWay(pQueue list, char **maze, int xlen, int ylen)
{
int i = 0;
int j = 0;
//先还原数组
for (i = 0; i < xlen; i++)
{
for (j = 0; j < ylen; j++)
{
if (maze[i][j] == '1')
{
maze[i][j] = '0';
}
}
}
int k = list->buf[list->rear - 1].pIndex;
//标记路径
while (k != 0)
{
i = list->buf[k].mPos.x;
j = list->buf[k].mPos.y;
maze[i][j] = '.';
k = list->buf[k].pIndex;
}
return 0;
}
================================main.h===========================================
/*
*Copyright(c)2010, xingwangshiyi
*All rights reserved.
*
*文件名:main.h
*摘 要:
*作 者:许文发
*时 间:2010-4-23
*/
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
typedef struct pos
{
int x;
int y;
}POS;
typedef struct node
{
int pIndex;
int mIndex;
POS mPos;
}NODE, DataType;
int PrintMaze(char **maze,int xlen, int ylen);
char **CreateMaze(int xlen, int ylen, int num, int xcat, int ycat, int xmouse, int ymouse);
#endif
================================queue.cpp===========================================
/*
*Copyright(c)2010, xingwangshiyi
*All rights reserved.
*
*文件名:queue.cpp
*摘 要:队列操作
*作 者:许文发
*时 间:2010-4-23
*/
#include "queue.h"
#include <stdio.h>
#include <malloc.h>
/*
*函数名:QueueCreate
*输 入:int len
*输 出:
*返 回:pQueue
*功 能:创建队列
*/
pQueue QueueCreate(int len)
{
pQueue list = NULL;
list = (pQueue)malloc(sizeof(Queue));
if (NULL != list)
{
list->buf = (DataType *)malloc(sizeof(DataType)*len);
if (NULL != list->buf)
{
list->len = len;
return list;
}
else
{
free(list);
}
}
printf("创建队列失败/n");
return NULL;
}
/*
*函数名:QueueInit
*输 入:pQueue list
*输 出:
*返 回:int
*功 能:初始化队列
*/
int QueueInit(pQueue list)
{
int i = 0;
list->front = 0;
list->rear = 0;
for (i = 0; i < list->len; i++)
{
list->buf[i].mPos.x = 0;
list->buf[i].mPos.y = 0;
}
return 0;
}
/*
*函数名:QueueEmpty
*输 入:pQueue list
*输 出:
*返 回:int
*功 能:判断队列是否为空
*/
int QueueEmpty(pQueue list)
{
if (list->front == list->rear)
{
return 1;
}
else
{
return 0;
}
}
/*
*函数名:QueueAdd
*输 入:pQueue list, DataType mypos
*输 出:
*返 回:int
*功 能:入队
*/
int QueueAdd(pQueue list, DataType mypos)
{
if ((list->rear + 1)%list->len == list->front)
{
printf("上溢/n");
}
else
{
list->buf[list->rear] = mypos;
list->rear = (list->rear + 1)%list->len;
}
return 0;
}
/*
*函数名:QueueDel
*输 入:pQueue list
*输 出:
*返 回:DataType
*功 能:出队
*/
DataType QueueDel(pQueue list)
{
DataType temp = {-1, -1};
if (1 == QueueEmpty(list))
{
printf("下溢/n");
}
else
{
temp = list->buf[list->front];
list->front = (list->front + 1)%list->len;
}
return temp;
}
================================queue.h===========================================
/*
*Copyright(c)2010, xingwangshiyi
*All rights reserved.
*
*文件名:queue.h
*摘 要:队列
*作 者:许文发
*时 间:2010-4-23
*/
#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED
#include "main.h"
typedef struct queue
{
int front;
int rear;
int len;
DataType *buf;
}Queue;
typedef Queue * pQueue;
pQueue QueueCreate(int len);
int QueueInit(pQueue list);
int QueueEmpty(pQueue list);
int QueueAdd(pQueue list, DataType mypos);
DataType QueueDel(pQueue list);
#endif // QUEUE_H_INCLUDED