zoukankan      html  css  js  c++  java
  • hdu 1372 ( Knight Moves ) BFS

    /*不停地BFS 我发现BFS的题目泛滥了*/
    Problem : 1372 ( Knight Moves )     Judge Status : Accepted
    RunId : 3710302    Language : C++    Author : zjut11018
    Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
    #include<iostream>
    #include<queue>
    #include<string>
    using namespace std;
    int dir[8][2]={-1,-2,-1,2,-2,1,-2,-1,2,1,2,-1,1,2,1,-2};
    int vis[8][8],ans;
    struct node
    {
        int x,y,dis;
        node(int _x=0,int _y=0,int _dis=0):x(_x),y(_y),dis(_dis){};
    };
    node s,e;
    string a[2];
    void BFS()
    {
        memset(vis,0,sizeof(vis));
        queue <node> q;
        q.push(s);
        vis[s.x][s.y]=1;
        while(!q.empty())
        {
            node t=q.front();
            q.pop();
            if(e.x==t.x&&e.y==t.y){ans=t.dis;return;}
            for(int k=0;k<8;k++)
            {
                int x=t.x+dir[k][0];
                int y=t.y+dir[k][1];
                if(x>=0&&x<8&&y>=0&&y<8&&!vis[x][y])
                {
                    vis[x][y]=1;
                    q.push(node(x,y,t.dis+1));
                }
    
            }
    
        }
    }
    int main()
    {
        while(cin>>a[0]>>a[1])
        {
            s.x=a[0][0]-'a';s.y=a[0][1]-'1';s.dis=0;
            e.x=a[1][0]-'a';e.y=a[1][1]-'1';
            ans=0;
            BFS();
            cout<<"To get from "<<a[0]<<" to "<<a[1]<<" takes "<<ans<<" knight moves."<<endl;
        }
    }
    
  • 相关阅读:
    小程序注册
    Webpack
    npm总结1
    js事件
    js高级程序2
    js高级程序
    索引
    将数据渲染到页面的方法
    前后端分离后,通讯问题 springboot + vue
    axios post 请求后端参数为null解决方案
  • 原文地址:https://www.cnblogs.com/sook/p/1996255.html
Copyright © 2011-2022 走看看