Beijing Forestry University "Garlic Guest" Cup Program Design Competition network B-Big nails riding the lake "BFS" _bfs

Source: Internet
Author: User

What is the river, for the big nails in the chess world, the lake is a matrix, his goal is to ride a horse in the rivers and lakes, from his position to go to the end.

Of course, the big nailed horse also obeys the Chinese chess "The horse Goes Day" the rule, and in the matrix, there will be some obstacles, the horse can not jump to the obstacles, if the big nails in front of the horse is a barrier, that is, "horse legs", then he will not be able to jump to the obstruction of the Left front and right in the two directions.

How many steps do I need at least to get the big nails to the end of the horse?

Input format:

There are multiple sets of sample samples.

The first line of each group is entered two numbers nn and mm, representing the number of rows and columns of the matrix, 2 \leq n \leq m < 1002≤n≤m<100.

Next enter the NN line string, where ' s ' stands for the starting point, ' E ' stands for the end, '. ' stands for the Open Space, ' # ' stands for obstacles.

Output format:

For each group of inputs, output the minimum number of steps to the end of the horse, if not jump to the end, output -1−1. Sample 1

Input:

3 3
S..
...
.. E

3 3
s#.
...
#.e

Output:

4
-1

Ideas for solving problems:

Simple search.

#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> using namespace std
;
int n,m;
Char map[500][500];
int vis[500][500];
int Dx[8]={1,-1, 1,-1, 2,-2, 2,-2};
int dy[8]={2, 2,-2,-2, 1, 1,-1,-1};
int sx,sy;
struct node {int x,y,time;};
	int bfs (int x,int y) {int i;
	Queue<node >q;
	Node k,s;
	K.x=x;
	K.y=y;
	k.time=0;
	Vis[x][y]=1;
	Q.push (k);
		while (!q.empty ()) {K=q.front ();
		Q.pop ();
		if (map[k.x][k.y]== ' E ') {return k.time;
			for (i=0;i<4;i++) {s=k;
			S.x=k.x+dx[i];
			S.y=k.y+dy[i];
			S.time+=1; if (s.x>=0&&s.y>=0&&s.x<n&&s.y<m&&!vis[s.x][s.y]&&map[s.x][
				s.y]!= ' # ' &&map[s.x-dx[i]][s.y-dy[i]/2]!= ' # ') {Q.push (s);
			Vis[s.x][s.y]=1;
			} for (i=4;i<8;i++) {s=k;
			S.x=k.x+dx[i];
			S.y=k.y+dy[i];
			S.time+=1; if (s.x>=0&&s.y>=0&&s.x<n&&s.y<m&&!vis[s.x][s.y]&&map[s.x][s.y]!= ' # ' &&map[s.x-dx[i]/2][s.y-dy[i]]!= ' # ') {Q.push (s);
			Vis[s.x][s.y]=1;
}} return-1;
		int main () {while (scanf ("%d%d", &n,&m)!=eof) {int i,j;
			for (i=0;i<n;i++) {scanf ("%s", Map[i]);
				for (j=0;j<m;j++) {if (map[i][j]== ' s ') {sx=i,sy=j;
		}} memset (Vis,0,sizeof (VIS));
	printf ("%d\n", BFS (Sx,sy));
return 0;  }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.