week2 homework 1-maze

Title

Dongdong has a map and wants to find the sister paper through the map. The map shows that 0 means you can walk, 1 means you can't walk, the upper left corner is the entrance, and the lower right corner is the sister paper. These two positions are guaranteed to be 0. Now that you know the map, it is not difficult for Dongdong to find sister paper. Please write a program to write the shortest route for Dongdong to find sister paper. The input is a 5 × 5 two-dimensional array, consisting of only 0 and 1 digits, representing a normal array map. Several lines are output, representing the coordinates of the shortest path from the upper left corner to the lower right corner, in the format shown in the sample. The data is guaranteed to have a unique solution.

Ideas

I used the dfs method to find the path, using two constant arrays to represent the four directions of movement up, down, left, and right, and using a vector and stack variable to store the path.
In the dfs function, determine whether the tail element is the end point, otherwise continue to look down according to the index order of the constant array. If the next point can be reached, that is, tong () returns true, then mark the point and add The stack continues to recurse. When you reach a dead end, that is, there is no way to reach in all four directions, you will release the point from the stack and continue recursion.

to sum up

This question just took a lot of detours. The storage path is also smart. I use an array to store the constant array index of each operation. I ’m stupid enough to forget that the vector has been saved, and I have almost forgotten about the knowledge of the graph. I made it up.

Code

#include<stdio.h>
#include<iostream>
using namespace std;

int sx=1,sy=1,ex=5,ey=5,step=0;
const int dx[4]={1,0,-1,0};	//表示移动方向
const int dy[4]={0,1,0,-1};
int mp[7][7]=
{
    {1,1,1,1,1,1,1},
    {1,0,0,0,0,0,1},
    {1,0,0,0,0,0,1},
    {1,0,0,0,0,0,1},
    {1,0,0,0,0,0,1},
    {1,0,0,0,0,0,1},
    {1,1,1,1,1,1,1}
};
int arrive[7][7]=
{
    {0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0}
};
struct point
{
	int x,y;
	point(int _x=1,int _y=1):x(_x),y(_y){};
};
vector<point> lu;
int pos[25];//存储路径 
static int count=0;

bool tong(int i,int nx,int ny)
{
	nx += dx[i];
    ny += dy[i];
    return (nx >= 1 && nx <= 5 && ny >= 1 && ny <= 5 && mp[nx][ny] != 1 && !arrive[nx][ny]);
}
void dfs()
{
	point t=lu.back();

	if(t.x==ex&&t.y==ey)return; //到达终点
	int nx=t.x,ny=t.y;

    if (tong(0,nx,ny)) {
    	nx += dx[0];
        ny += dy[0];
    	arrive[nx][ny]=1;
    	pos[step++]=0;
    	point t(nx,ny);
        lu.push_back(t);
        dfs();
    }
    else  if (tong(1,nx,ny)) {
   		nx += dx[1];
        ny += dy[1];
    	arrive[nx][ny]=1;
    	pos[step++]=1;
    	point t(nx,ny);
        lu.push_back(t);
        dfs();
    }
    else  if (tong(2,nx,ny)) {
    	nx += dx[2];
        ny += dy[2];
    	arrive[nx][ny]=1;
    	pos[step++]=2;
    	point t(nx,ny);
        lu.push_back(t);
        dfs();
    }
    else if (tong(3,nx,ny)) {
    	nx += dx[3];
        ny += dy[3];
    	arrive[nx][ny]=1;
    	pos[step++]=3;
    	point t(nx,ny);
        lu.push_back(t);
        dfs();
    }
    else 
	{
		if(step) //返回上一层 
		step--;
		lu.pop_back();
		if(lu.size()){
			point t=lu.back();
			dfs();
		}
	   else {
		cout<<"no way"<<endl;
		}
	}   
}
void showPath()
{
	int xx=0,yy=0;
	cout << "(" << xx << ", " << yy << ")" << endl;	
	for(int i=0;i<step;i++)
	{
		xx = xx + dx[pos[i]];
    	yy = yy + dy[pos[i]];
        cout << "(" << xx << ", " << yy << ")" << endl;
	}
}
int main()
{
	point beg(1,1);
	lu.push_back(beg);
	for(int i=1;i<=5;i++)
		for(int j=1;j<=5;j++)
			cin>>mp[i][j];				
	dfs();
	showPath();
	return 0;
}



Published 20 original articles · praised 3 · visits 464

Guess you like

Origin blog.csdn.net/qq_44893580/article/details/104621584