1000ms 32768K
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
#include <iostream> #include <algorithm> #include <string.h> #include <queue> #define PP 102 Char map[pp][pp];//map int a[pp][pp];
The first person to the number of steps int DX[8]={1,1,-1,-1,2,2,-2,-2};//8 a search direction int dy[8]={2,-2,2,-2,-1,1,-1,1}; int yx,yy,ex,ey;//record the position of the spike and the end point
int m,n;
int i,j;
using namespace Std;
struct node {int x,y;};
void Getmap () {for (i=0;i<n;i++) for (j=0;j<m;j++) {cin>>map[i][j];
if (map[i][j]== ' s ')//record position {yx=i;
Yy=j;
} if (map[i][j]== ' E ') {ex=i;
Ey=j;
}} void BFs (int c,int d,int s[pp][pp]) {queue<node>q;
Node AA,BB;
Aa.x=c;
Aa.y=d;
s[c][d]=0; Q.push (AA); Team while (!q.empty ()) {Bb=q.front (); Visit the team first element to copy the value to BB q.pop ();
Eject team first element for (i=0;i<4;i++) {aa.x=bb.x+dx[i];
Aa.y=bb.y+dy[i]; if (aa.x>=0&&aa.x<n&&aa.y>=0&&aa.y<m&&map[aa.x][aa.y]!= ' # ' & &s[aa.x][aa.y]==0&&map[aa.x-dx[i]][aa.y-dy[i]/2]!= ' # ') {s[aa.x][aa.y]=s[bb.x][bb.
y]+1; Q.push (AA);
Join}} for (i=4;i<8;i++) {aa.x=bb.x+dx[i];
Aa.y=bb.y+dy[i]; if (aa.x>=0&&aa.x<n&&aa.y>=0&&aa.y<m&&map[aa.x][aa.y]!= ' # ' & &s[aa.x][aa.y]==0&&map[aa.x-dx[i]/2][aa.y-dy[i]]!= ' # ') {s[aa.x][aa.y]=s[bb.x][bb.
y]+1; Q.push (AA);
Join}}} int main () {while (cin>>n>>m) {int aa=0;
memset (A,0,sizeof (a));
Getmap ();
int minn=1000000;
BFS (Yx,yy,a); for (i=0;i<n;i++) for (j=0;j<m;j++) {if map[i][j]== 'E ' &&a[i][j]!=0//To determine that the point has been visited {minn=min (minn,a[i][j]);
Aa=1;
} if (aa==1) cout<<minn<<endl;
Else cout<< "-1" <<endl;
return 0;
}