⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mouse.cpp

📁 老鼠走迷宮
💻 CPP
字号:
#include<iostream>
#include<fstream>
using namespace std;

class move
{
public:
	static int rownum[8];
	static int colnum[8];
	int new_x1,new_y1;
	static int ** roadmap;
	move * next_node;
	move * front_node;
	static move * now;
	const static move * head;
	move():next_node(NULL)
	{
		head = now  = this;
	}
	move(int new_xn,int new_yn):new_x1(new_xn),new_y1(new_yn),next_node(NULL){}
	static void add_node(int, int);
	static void chk_node(int *,int*);
	static void choice(int *, int *, int ** );
};
int move::rownum[8] = {-1,0,1,1,1,0,-1,-1};
int move::colnum[8] = {-1,-1,-1,0,1,1,1,0};
int ** move::roadmap;
const move * move::head;
move * move::now;

void move::add_node(int new_x,int new_y)
{
	now->next_node = new move(new_x,new_y);
	now->next_node->front_node = now;
	now = now->next_node;
}

void move::chk_node(int*a,int*b)
{
	int node_num = NULL,nea=NULL,neb=NULL;
	for(int i = 0;i<=7;i++)
		{
			nea=now->new_x1+rownum[i];
			neb=now->new_y1+colnum[i];
 			if(roadmap[nea][neb]==0)
			{
					node_num++;
			}
		}
		if(node_num == 0)
		{
			if(now != head)
			{
			   now = now->front_node;
			   chk_node(a,b);
			}
		}
		else
		{
			*a=now->new_x1;
			*b=now->new_y1;
		}
}

void move::choice(int * x, int * y, int ** roadmap_a)
{
	roadmap = roadmap_a;
	int cout = 0,num=0;
	int nextx=NULL,nexty=NULL,sea=NULL,seb=NULL;
	for(int i = 0;i<=7;i++)
	{
		sea=*x+rownum[i];
		seb=*y+colnum[i];
		if(roadmap[sea][seb]==0)
		{
				cout++;
		}
	}
	if(cout >= 2)
	{
		move::add_node(*x,*y);
	}
	num=0;
	for(int i = 0;i<=7;i++)
	{
		sea=*x+rownum[i];
		seb=*y+colnum[i];
		if(roadmap[sea][seb]==0)
		{
				nextx=*x,nexty=*y;
				*x=sea,*y=seb;
				roadmap[nextx][nexty]=1;
				break;
		}
		else
		{num++;}
	}
	if(num==8)
	{
		roadmap[*x][*y]=1;
	    chk_node(x,y);
	}
}

main()
{
	fstream file;
	file.open("maze.txt",ios::in);
	int m = 0,n = 0,f_cur = 0,f_end = 0;
	char str;
	file.seekp(0,ios::end);
	f_end = file.tellg();
	file.seekp(0,ios::beg);
	f_cur = file.tellg();
	while(f_cur != f_end)
	{
		file.get(str);
		f_cur = file.tellg();
		if(str == '1' || str == '0' )
		{
			if(n == 0)
			{
				m++;
			}
		}
		else
		{
			n++;
		}
	}
    n = n+1;
    file.seekp(0,ios::beg);
	int **a = new int * [n];
	int **b = new int * [n];
	for(int  i = 0; i<n; i++)
	{
		*(a+i) = new int [m];
		*(b+i) = new int [m];
	}
	move mousemove;
	for(int i = 0;i<n;i++)
	{
		for(int j =0;j<m;j++)
		{
			file>>str;
			if(str == '0')
			{
				a[i][j] ='0';
				b[i][j] = 0;
			}
			else if(str == '1')
			{
				a[i][j] = '1';
				b[i][j] = 1;    
			}
		}
	}

	int x = 1,y = 1,final_x,final_y,*ad_x,*ad_y;
	final_x = n - 2;
	final_y = m - 2;
	ad_x = &x,ad_y = &y;
	while(*ad_x != final_x||*ad_y != final_y) 
	{
		cout<<"’----------->沧翴\n\n";   
		for(int i = 0;i<n;i++)
		{
			for(int j =0;j<m;j++)
			{ 
				if(i == *ad_x&&j == *ad_y)
				{cout<<"公";}
				else if(i == final_x&&j == final_y)
				{cout<<"’";}
				else if(a[i][j] == '1' )
				{cout<<"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -