Big nails riding the lake "BFs" _ Other OJ

Source: Internet
Author: User

Original title URL: http://nanti.jisuanke.com/t/11064

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


Exercises

It is not difficult to think of BFS brute force search solution, chess "horse War plus", if not consider stand eaves legs and cross-border, at least to enumerate eight positions, and this question stand eaves horse leg is pruning conditions, special attention to the corresponding relationship of constant array


/* http://blog.csdn.net/liuke19950717/#include <cstdio> #include <queue> #include <cstring>
Include<algorithm> using namespace std;
const int maxn=105;
Char MAP[MAXN][MAXN];
int n,m;
int dh[8][2]={{-1,-2},{-1,2},{1,2},{1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
int dht[8][2]={{0,-1},{0,1},{0,1},{0,-1},{1,0},{1,0},{-1,0},{-1,0}};
struct node {int x,y,time;};
	int BFS (int bx,int by,int ex,int ey) {int vis[maxn][maxn]={0};
	Queue<node> Q;
	Node st={bx,by,0};
	Vis[bx][by]=1;
	Q.push (ST);
		while (!q.empty ()) {St=q.front (); Q.pop ();
		if (St.x==ex&&st.y==ey) {return st.time;
			for (int i=0;i<8;++i) {int tx=st.x+dh[i][0],ty=st.y+dh[i][1]; if (tx<0| | tx>=n| | ty<0| | ty>=m| |
			map[tx][ty]== ' # ') {continue;
			int sx=st.x+dht[i][0],sy=st.y+dht[i][1];
			if (map[sx][sy]== ' # ') {continue;
				} if (!vis[tx][ty]) {vis[tx][ty]=1;
				Node tp={tx,ty,st.time+1};
			Q.push (TP);
}} return-1; int main ({while (~scanf ("%d%d", &n,&m)) {for (int i=0;i<n;++i) {scanf ("%s", Map[i]);
		int Bx,by,ex,ey;
				for (int i=0;i<n;++i) {for (int j=0;j<m;++j) {if (map[i][j]== ' s ') {bx=i;by=j;
				else if (map[i][j]== ' E ') {ex=i;ey=j;
	} printf ("%d\n", BFS (Bx,by,ex,ey));
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.