zoukankan      html  css  js  c++  java
  • BFS(当模板用)——[Usaco2007 Dec]穿越泥地

    好久没写搜索的题目了
    500*500深搜肯定不行
    求最小步长,BFS,坐标有(-500,500),+500搞定
    ps:把状态量写成 i, j一类的,容易不混淆坐标。。。以前用X,Y表示一直要与数学里的坐标搞错的,囧
    写完由于用c提交CE了一次,cpp提交,一次AC...
    View Code
    #include<stdio.h>
    #include
    <iostream>
    #include
    <queue>
    #include
    <string.h>
    using namespace std;

    bool hash[1009][1009];
    int diri[4]={0,-1,0,1};
    int dirj[4]={-1,0,1,0};

    struct data
    {
    int ni;
    int nj;
    int step;
    };

    void bfs(int ei,int ej)
    {
    queue
    <data>q;
    data f,s,t;
    f.ni
    =500;
    f.nj
    =500;
    f.step
    =0;
    q.push(f);

    int i,j;
    while(!q.empty())
    {
    s
    =q.front();
    q.pop();
    for(i=0;i<=3;i++)
    {
    t.ni
    =s.ni+diri[i];
    t.nj
    =s.nj+dirj[i];
    if(hash[t.ni][t.nj]==1)continue;
    t.step
    =s.step+1;

    if(t.ni==ei&&t.nj==ej)
    {
    printf(
    "%d\n",t.step);
    return ;
    }
    q.push(t);
    hash[t.ni][t.nj]
    =1;
    }
    }
    }

    int main()
    {
    int ei,ej,n;
    while(scanf("%d%d%d",&ei,&ej,&n)!=EOF)
    {
    ei
    +=500;
    ej
    +=500;
    int i;
    memset(hash,
    0,sizeof(hash));
    for(i=0;i<n;i++)
    {
    int a,b;
    scanf(
    "%d%d",&a,&b);
    a
    +=500;
    b
    +=500;
    hash[a][b]
    =1;
    }
    bfs(ei,ej);
    }
    }

      

  • 相关阅读:
    jsonp
    web系统中上下移动功能的实现
    重载的目的是什么
    重写和重载
    final和static
    static的应用
    shiro认证
    做阉割版Salesforce难成伟大的TOB企业
    Go语言在国产CPU平台上应用前景的探索与思考
    101 More Security Best Practices for Kubernetes
  • 原文地址:https://www.cnblogs.com/huhuuu/p/2119556.html
Copyright © 2011-2022 走看看