Gregory the Grasshopper BFS搜索

Description
Gregory is a grasshopper. His favourite food are clover leafs ¡ª he can simply never have enough
of them. Whenever he spots such a leaf, he wants to eat it as quickly as possible. Gregory is
also lazy, so he wants to move to the leaf with minimal effort. Your task is to help him to find
the shortest way to a clover leaf.
For simplicity, we will assume that Gregory lives on a rectangular grid consisting of unit squares.
As a grasshopper, he prefers to move by jumping (or, more exactly, hopping) from one square
to the other. Each hop takes him to a square that is in the adjacent row or column in one
direction, and two columns or rows away in the other direction. So, his hops resemble the moves
of a knight on a chessboard.

Gregory the Grasshopper BFS搜索_第1张图片


Input
Gregory is a grasshopper. His favourite food are clover leafs ¡ª he can simply never have enough
of them. Whenever he spots such a leaf, he wants to eat it as quickly as possible. Gregory is
also lazy, so he wants to move to the leaf with minimal effort. Your task is to help him to find
the shortest way to a clover leaf.
For simplicity, we will assume that Gregory lives on a rectangular grid consisting of unit squares.
As a grasshopper, he prefers to move by jumping (or, more exactly, hopping) from one square
to the other. Each hop takes him to a square that is in the adjacent row or column in one
direction, and two columns or rows away in the other direction. So, his hops resemble the moves
of a knight on a chessboard.
Input Specification
Output
For each test case, print one integer number ¡ª the minimal number of hops that Gregory needs
to reach the square with his beloved delicacy. If it is not possible to reach that square at all,
print the word ¡°impossible¡± instead.
Sample Input
10 10 10 10 1 1
2 2 1 1 1 2
8 8 1 1 1 2
Sample Output
6
impossible
3


题目很简单 就是类似马走日字格,然后给你了 棋盘的长和宽,起始坐标和终止坐标,然后求从起始目标到达终点的最小步数,若不存在 输出 impossible

就是BFS的模板

bfs(){ 
    node st[N]    
    int l=0,r=1,vis[110][110] ,f[110][110] ;  // vis 表示队列中是否存在的点 st表示队列 存的是坐标  f存的是权值
    vis[s.x][s.y]=1; 
    f[s.x][s.y]=0;     //初始的权值为0 然后一开始的节点是访问过的
    st[r]=s;
    while(l!=r){
         if(++l>max)l=1; 
         node u=st[l];               //用循环队列取出节点
         vis[u.x][u.y]=1;
         node tmp;
         for(int i=0;i<8;i++){         // 进行跳的操作
            tmp.x=u.x+dx[i];
            tmp.y=u.y+dy[i]; 
            if(f[u.x][u.y]+1>f[tmp.x][tmp.y]){  //更新权值
                f[tmp.x][tmp.y]=f[u.x][u.y]+1;
                if(vis[tmp.x][tmp.y]==0){      //若权值更新了 看队列中是否含有已经跳到的点,若有不加入队列
                   vis[tmp.x][tmp.y]=1;
                   if(++r>max)r=1;
                   st[r]=tmp;
                }
            } 
         }
    }
    if(f[e.x][e.y]==MAX)return -1;
    else return f[e.x][e.y];
}


源代码

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
struct node{
     int x,y;       
}s,e,u;
const int MAX=20000000,edge=9000;
int f[110][110];
int wid,len;
bool vis[110][110];
int dx[10]={-2,-1,1,2,2,1,-1,-2};
int dy[10]={1,2,2,1,-1,-2,-2,-1};
node st[10000],tmp;   //¿ªÉèÒ»¸ö¶ÓÁÐ 
int bfs(){
     memset(vis,0,sizeof(vis));
     for(int i=0;i<110;i++)
        for(int j=0;j<110;j++)
           f[i][j]=MAX;
     f[s.x][s.y]=0;
     vis[s.x][s.y]=1; 
     int l=0,r=1;    
     st[r]=s;
     while(l!=r){
        if(++l>edge){
           l=1;
        }
        u=st[l];
        vis[u.x][u.y]=0;
        for(int i=0;i<8;i++){
           tmp.x=u.x+dx[i];
           tmp.y=u.y+dy[i];        
           if(tmp.x<1||tmp.x>wid||tmp.y<1||tmp.y>len)continue;
           if(f[u.x][u.y]+1<f[tmp.x][tmp.y]){
                f[tmp.x][tmp.y]=f[u.x][u.y]+1;
                if(vis[tmp.x][tmp.y]==0){
                   if(++r>edge) {
                   r=1;
                   }   
                st[r]=tmp;
                vis[tmp.x][tmp.y]=1;                 
                }       
           }
  
        }           
     }
     if(f[e.x][e.y]==MAX)return -1;
     else return f[e.x][e.y];

}
int main()
{
    
    while(scanf("%d%d%d%d%d%d",&wid,&len,&s.x,&s.y,&e.x,&e.y)!=EOF){
          int ans=bfs();  
          if(ans==-1)cout<<"impossible"<<endl;
          else cout<<ans<<endl;                                                                                                              
    }    
    return 0;    
} 














你可能感兴趣的:(算法)