CSP-homework Week2 Problem A sign BFS-- find sister

Original title description

Topic narrative

Stuff have a map, sister paper to find the map. Map display, 0 can go, can not go 1 represents the upper left corner is the entrance to the lower right corner is the sister paper, these two positions to ensure zero. Now that we know the map, then stuff it is not difficult to find the sister paper, you compile a program, write stuff to find the shortest route sister paper.

INPUT

Input is a 5 × 5 two-dimensional array, just the two numbers 0,1, representation positions in FIG.

OUTPUT

A plurality of output lines, represents the shortest path from upper left to lower right corner coordinates sequentially passes, formatted as shown in the sample. Ensure data has a unique solution.

SAMPLE INPUT

0 1 0 0 0
0 1 0 1 0
0 1 0 1 0
0 0 0 1 0
0 1 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(3, 0)
(3, 1)
(3, 2)
(2, 2)
(1, 2)
(0, 2)
(0, 3)
(0, 4)
(1, 4)
(2, 4)
(3, 4)
(4, 4)

Reiteration

Is a simple maze, find a shortest path, record the path laid down, because it is the shortest path requires that the most convenient is the BFS.

Problem-solving ideas

Overview ideas

BFS BFS board part is naked, no far-reaching treatment process, a two-dimensional array with VIS [] [] survive, can not access the local standard -2 accessible place superscript -1, n represents the number after accessing distance from the starting point on it.
Note that selecting the output section of the program, problem-solving when I was more inclined to write a recursive output, but may be thought to myself drift off, the output is always a mess, simply opened a the Vector <struct Point>
, with two-dimensional array of struct dis [] [] to hold a point of origin, then print () function in reverse under treatment is good, although much space fee, but without thinking when solving recursive border is also fairly feasible.
Recursive version of the print () function below

point dis[maxn][maxn];
vector<point> v;
void print(point &p)
{
	point target=p;
	v.push_back(p);
	while(target.x!=0 | target.y!=0)
	{
		target=dis[target.x][target.y];
		v.push_back(target);
	}
	for(int i=v.size()-1;i>=0;i--)
	{
		printf("(%d, %d)\n",v[i].x,v[i].y);
	}
}

Later he found himself recursive array wrong (stupid cry .jpg), recursive writing more US programming, the brain awake, then it is more feasible to writing
a recursive version of the print () as follows

point dis[maxn][maxn];
vector<point> v;
void print(point &p)
{
    point target=p;
    if(target.x==0 && target.y==0)
    {
        printf("(%d, %d)\n",target.x,target.y);
        return;
    }
    print(dis[target.x][target.y]);
    printf("(%d, %d)\n",target.x,target.y);
}

data storage

The title used in data storage and

Type name and variable name Practical significance
int dx[],dy[] Offset Array
force int [] [] Distance from a point of origin
struct point dis[][] A point of origin in the course of BFS

to sum up

Comparison of water an attendance problem, pay attention to good PE format does not appear, basically can not go wrong.

Improved point

This question is for No.

Source title

#include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
using namespace std;
struct point
{
    /* data */
    int x;
    int y; 
};
const int maxn=10;
int dx[]={+1,-1,0,0};
int dy[]={0,0,+1,-1};
int vis[maxn][maxn];
point dis[maxn][maxn];
vector<point> v;
void print(point &p)
{
	point target=p;
	v.push_back(p);
	while(target.x!=0 | target.y!=0)
	{
		target=dis[target.x][target.y];
		v.push_back(target);
	}
	for(int i=v.size()-1;i>=0;i--)
	{
		printf("(%d, %d)\n",v[i].x,v[i].y);
	}
}
void bfs(int sx,int sy,int tx,int ty)
{
	point target={tx,ty};
    point the_begin={sx,sy};
    queue<point> Q;
    Q.push(the_begin);
    vis[sx][sy]=0;
    while(!Q.empty()){
        point now=Q.front(); Q.pop();
        if(now.x==tx &&now.y==ty)
        print(target);
        for(int i=0;i<4;i++){
            int newx=now.x+dx[i],
                newy=now.y+dy[i];
                point start={now.x,now.y};
                point finish={newx,newy};
                if(newx>=0 && newx<=4 && newy>=0 && newy<=4 && vis[newx][newy]==-1){
                	vis[newx][newy]=vis[now.x][now.y]+1;
                    Q.push(finish);
                    dis[newx][newy]=start;
            }
        }
    }
}
int main()
{
    memset(vis,-1,sizeof(vis));
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
            int number=0;
            cin>>number;
            if(number==1)
            {
                vis[i][j]=-2;
            }
        }
    }
	bfs(0,0,4,4);
    return 0;
}
Published 17 original articles · won praise 2 · Views 1665

Guess you like

Origin blog.csdn.net/qq_43942251/article/details/104674247