zoukankan      html  css  js  c++  java
  • bfs模版

    //用队列
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    int x1,x2,y2,x3,y3,y1;
    int dir[8][2]= {0,1,0,-1,-1,0,1,0,1,1,1,-1,-1,1,-1,-1};
    int mark[10][10];
    struct nodes
    {
            int x,y,m;
    };
    nodes node1,node2;
    void bfs()
    {
        queue<nodes> q;
        while(!q.empty())
            q.pop();
        node1.x=x1;
        node1.y=y1;
        node1.m=0;
        mark[x1][y1]=1;
        q.push(node1);
        while(!q.empty())
        {
            node1=q.front();
            q.pop();
            if(node1.x==x2&&node1.y==y2)
            printf("%d
    ",node1.m);
            for(int i=0; i<8; i++)
            {
                node2.x=node1.x+dir[i][0];
                node2.y=node1.y+dir[i][1];
                if(node2.x>=1&&node2.x<=8&&node2.y>=1&&node2.y<=8&&!mark[node2.x][node2.y])
                {
                    mark[node2.x][node2.y]=1;
                    node2.m=node1.m+1;
                    q.push(node2);
                }
            }
        }
    
    
    }
    int main()
    {
        int k=0;
        while(~scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3))
        {
            k++;
            memset(mark,0,sizeof(mark));
            mark[x3][y3]=1;
            printf("Case %d: ",k);
            bfs();
        }
    }
  • 相关阅读:
    函数的内置方法
    函数
    文件操作
    三元运算
    流程控制
    集合
    div容器内文本对齐--神奇的css
    Struts2的零配置和rest插件
    MIME类型
    ganymed-ssh2使用
  • 原文地址:https://www.cnblogs.com/dshn/p/4750465.html
Copyright © 2011-2022 走看看