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

📄 labyrinth.cpp

📁 labyrinth: you need to find the doable way to go to the destinaton.And then print out the path.
💻 CPP
字号:
#include<iostream>

using namespace std;

int maze(int,int);

struct ma
{
	int data;
	int make;
};
ma m[6][6];

void main()
{
    int i,j;
    
	for(i=0;i<=5;i++)
	{
	    for(j=0;j<=5;j++)
		{
		    m[i][j].make=0;
		}
        if((i==0)||(i==5)||(j==0)||(j==5))
		{
			m[i][j].data=1;
		}
	}

    cout<<"please enter a maze"<<endl;

    for(i=1;i<=4;i++)
	{
	    for(j=1;j<=4;j++)
		{
		    cout<<'m'<<'['<<i<<']'<<'['<<j<<']'<<'=';
		    cin>>m[i][j].data;
		}
	}
	cout<<endl;
    
	if(maze(1,1))
	{
		cout<<endl<<"yes"<<endl;
		for(i=1;i<=4;i++)
		{
			for(j=1;j<=4;j++)
				cout<<m[i][j].make<<' ';
			cout<<endl;
		}
	}
	else
		cout<<endl<<"no"<<endl;
}

int maze(int a,int b)
{
	int x=a,y=b;
    int fb[4][2];
 	
    fb[0][0]=0;  fb[0][1]=1;  fb[1][0]=1;  fb[1][1]=0;  
    fb[2][0]=0;  fb[2][1]=-1;  fb[3][0]=-1;  fb[3][1]=0;

	if((x==4)&&(y==4)&&(m[x][y].data==0))
	{
		m[x][y].make=1;
		cout<<x<<' '<<y<<endl;
		return (1);
	}

	else if((x<4)||(y<4))
	{
        if(m[x][y].data==1)
		{
			m[x][y].make=0;
			return (0);
		}

		else if(m[x][y].data==0)
		{
			int vb=0;
			while(vb<=3)
			{
				int g,h;
				g=x+fb[vb][0];
				h=y+fb[vb][1];

				if(g<=4&&h<=4&&maze(g,h))
				{
					cout<<x<<' '<<y<<endl;
	                m[x][y].make=1;
					m[g][h].make=1;
					return (1);
				}
				vb++;

                if(vb>3)
				{
					m[x][y].make=0;
			     	return (0);
				}
			}
		}
		
	}
}

⌨️ 快捷键说明

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