zoukankan      html  css  js  c++  java
  • 全是套路——BFS

    #include <iostream>
    #include <vector>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <stack>
    #include <math.h>
    #include <limits.h>
    #include <set>
    #include <memory>
    #include <queue>
    
    using namespace std;
    
    struct point{
        int x;
        int y;
        point *parent;
        int step;
    };
    
    int map[9][9] = {         1, 1, 1, 1, 1, 1, 1, 1, 1,
                             1, 0, 0, 1, 0, 0, 1, 0, 1,
                             1, 0, 1, 1, 1, 0, 0, 0, 1,
                             1, 0, 1, 0, 0, 0, 0, 1, 1,
                             1, 0, 1, 0, 1, 1, 0, 0, 1,
                             1, 0, 1, 0, 1, 1, 0, 0, 1,
                             1, 0, 0, 0, 1, 1, 0, 0, 1,
                             1, 1, 0, 1, 1, 0, 0, 0, 1,
                             1, 1, 1, 1, 1, 1, 1, 1, 1, };
    
    
    vector<point> p;
    int BFS(point s,point e)
    {
        queue<point> q;
        q.push(s);
        while (!q.empty())
        {
            point start = q.front();
            point *s1 = new point; s1->x = start.x; s1->y = start.y; s1->parent = start.parent;
            q.pop();
            if (start.x == e.x&&start.y == e.y)
            {
                p.push_back(start);
                return start.step;
            }
            if (map[start.x + 1][start.y] != 1 && start.x + 1 <= 8 && start.x + 1 >= 0)
            {
                point tmp = { start.x + 1, start.y, s1, start.step + 1 };
                map[start.x + 1][start.y] = 1;
                q.push(tmp);
            }
            if (map[start.x - 1][start.y] != 1 && start.x - 1 <= 8 && start.x - 1 >= 0)
            {
                point tmp = { start.x - 1, start.y, s1, start.step + 1 };
                map[start.x - 1][start.y] = 1;
                q.push(tmp);
            }
            if (map[start.x][start.y + 1] != 1 && start.y + 1 <= 8 && start.y + 1 >= 0)
            {
                point tmp = { start.x, start.y + 1, s1, start.step + 1 };
                map[start.x][start.y + 1] = 1;
                q.push(tmp);
            }
            if (map[start.x][start.y - 1] != 1 && start.y - 1 <= 8 && start.y - 1 >= 0)
            {
                point tmp = { start.x, start.y - 1, s1, start.step + 1 };
                map[start.x][start.y - 1] = 1;
                q.push(tmp);
            }
            start.step++;
        }
    
    }
    
    int main()
    {
        point s = { 1, 1 }, e = { 7, 7 };
        BFS(s, e);
        char p1[15] = "abcd", *p2 = "ABCD", str[50] = "xyz";
        strcpy(str + 2, strcat(p1 + 2, p2 + 1));
        printf("%s", str);
        point *point = &p[0];
        while (point->parent != NULL)
        {
            cout << point->x << " " << point->y << endl;
            point = point->parent;
        }
        return 0;
    }
  • 相关阅读:
    luogu P1073 最优贸易 |分层图最短路
    luogu P1901 发射站 |单调队列
    luogu P1759 通天之潜水 |背包
    luogu P1801 【黑匣子_NOI导刊2010提高(06)】|堆+分块
    bzoj1642[Usaco2007 Nov]Milking Time 挤奶时间*
    bzoj1616[Usaco2008 Mar]Cow Travelling游荡的奶牛*
    bzoj1623[Usaco2008 Open]Cow Cars 奶牛飞车*
    bzoj1612[Usaco2008 Jan]Cow Contest奶牛的比赛*
    bzoj1639[Usaco2007 Mar]Monthly Expense 月度开支*
    bzoj1601[Usaco2008 Oct]灌水*
  • 原文地址:https://www.cnblogs.com/wyc199288/p/5811531.html
Copyright © 2011-2022 走看看