Libya action (wide search, maze)

Since March 16, 2011, escalating unrest in Libya, has seriously endanger the safety of the general public and national staff working in Libya. In order to rescue our compatriots in Libya as soon as possible, as the situation in Libya, the Chinese government told every citizen in Libya, what action can the fastest to reach a safe place, and then by the country sent planes, ships, cars and then return home. Suppose Libyan map can be described as a rectangular n rows and m columns, to be saved in a small A fellow row at a secure location of the target at the n rows by m columns. A small can only to an adjacent, lower, move left, and right directions, i.e., if the present position is small A row i and column j, the next step will reach the position of the small A row i-1 and j columns, i + 1 row j-th column, row i column j-1, one of these four positions i + j row columns. 1, of course, can not be moved a small rectangular n rows and m columns. Libya is a multi-desert terrain and complex country, some places are very dangerous, people can not go. Libya given map, please come to the end Tell A small (n, m) from the starting point (1,1) how many steps need it the fastest?

Input formats:

The first line has two positive integers n, m (1≤n≤2000,1≤m≤2000), with a space between them, topographically divided Libyan represents n rows and m columns. Next n lines of m characters indicate the location information in a map. Wherein: the character "* " indicates that this position is buildings, rivers, mines and other people can not come to the location (to ensure that the starting point is not the end, "* "); decimal point one can come to that position. "."

Output formats:

Only a row that only a positive integer. A small representation from start to finish, how many steps the fastest needs.

Sample input:

3 5
.*...
...*.
*..*.

Sample output:

8

 

 

 

I used to write maze class of topics often have to debug a variety of BUG, ​​and the method each time with a bit of difference, not uniform, often doing doing, found that in this way there will be some of the more troublesome areas. Seems to be thought about before because the code that makes up directly started, causing all sorts of headaches (fear), this time almost directly error-free, once-through, and the efficiency and code is also ideal, so save this code as a labyrinth class stencil

Ideas,

1. First, construct a coordinate structure, there are the coordinates x, y points and the number of steps of the arrival coordinates, and the constructor, assignment to later easily.

2. Write the breadth of the search frame, including definitions, visited flag array, an array of horizontal and vertical offsets

3. Write a check function checks whether the coordinates of the points added to the queue (check it out of bounds, whether it is an obstacle, and whether or visited a)

#include<iostream>
#include<queue>
using namespace std;
struct pos {
	int x,y,cunt;
	pos(int a,int b,int c) {   //构造函数
		x=a;
		y=b;
		cunt=c;
	}
};
char a[2002][2002];
int n,m,z[4]= {0,0,1,-1},h[4]= {1,-1,0,0};
bool flag[2002][2002]= {false};
bool check(int x,int y) { //检查其是否越界,是否为障碍物,以及是否访问过了
	if(x>=0&&x<n&&y>=0&&y<m) {
		if(flag[x][y]==false) {
			if(a[x][y]=='.')return true;
		}
	}
	return false;
}
int bfs() {
	queue<pos> q;
	q.push(pos(0,0,0));
	flag[0][0]=true;
	while(!q.empty()) {
		pos top =q.front();
		q.pop();
		if((top.x==n-1)&&(top.y)==m-1)return top.cunt;
		for(int i=0; i<4; i++) {
			int x=top.x+z[i];
			int y=top.y+h[i];
			int cunt=top.cunt+1; //定义局部变量x,y,cunt,优于宏定义,可以增加运行速度
			if(check(x,y)) {     //因为x,y需要用到多次
				q.push(pos(x,y,cunt));
				flag[x][y]=true;
			}
		}
	}
	return -1; //无法到达终点
}
int main() {
	cin>>n>>m;
	for(int i=0; i<n; i++)cin>>a[i];
	cout<<bfs();
	return 0;
}

 

Published 42 original articles · won praise 16 · views 3409

Guess you like

Origin blog.csdn.net/qq_41542638/article/details/96346900