• POJ 3984 迷宫问题 bfs


    题目链接:迷宫问题 

    天啦撸。最近怎么了。小bug缠身,大bug 不断。然这是我大腿第一次给我dbug。虽然最后的结果是。我............bfs入队列的是now..............

    然后。保存路径的一种用的string 。一种用的数组。大同小异。根据就是我bfs 先搜到的绝壁就是步数最少的、

    附代码:

    pre 数组

     1 /*
     2  很简单的广搜。如果不是+路径输出的话。。
     3  保存路径。
     4  */
     5 
     6 #include <stdio.h>
     7 #include <string.h>
     8 #include <iostream>
     9 #include <queue>
    10 #include <string>
    11 using namespace std;
    12 
    13 struct Node {
    14     int x, y;
    15 }now, temp, ans;
    16 
    17 int mp[10][10];
    18 int vis[10][10];
    19 Node que[210];
    20 Node pre[210];
    21 
    22 int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
    23 
    24 bool check(Node a) {
    25     if (a.x >= 0 && a.x < 5 && a.y >= 0 && a.y < 5 && !vis[a.x][a.y] && mp[a.x][a.y] == 0)
    26         return true;
    27     return false;
    28 }
    29 
    30 int head = 0, tail = 0;
    31 
    32 void bfs() {
    33     while(head < tail) {
    34         now = que[head++];
    35         if (now.x == 4 && now.y == 4) {
    36             return;
    37         }
    38         for (int i=0; i<4; ++i) {
    39             temp.x = now.x + dir[i][0];
    40             temp.y = now.y + dir[i][1];
    41 
    42             if (check(temp)) {
    43                 que[tail++] = temp;
    44                 vis[temp.x][temp.y] = 1;
    45                 int id = temp.x * 5 + temp.y;
    46                 pre[id].x = now.x, pre[id].y = now.y;
    47             }
    48         }
    49     }
    50 }
    51 
    52 void outPre(int num) {
    53     if (pre[num].x == -1 && pre[num].y == -1)
    54         return;
    55     else outPre(5*pre[num].x+pre[num].y);
    56     cout << "(" << pre[num].x << ", " << pre[num].y << ")
    ";
    57 }
    58 
    59 int main() {
    60     head = 0, tail = 0;
    61     memset(vis, 0, sizeof(vis));
    62 
    63     for (int i=0; i<5; ++i) {
    64         for (int j=0; j<5; ++j) {
    65             cin >> mp[i][j];
    66         }
    67     }
    68     now.x = 0, now.y = 0;
    69     vis[0][0] = 1;
    70     que[tail++] = now;
    71     pre[0].x = -1, pre[0].y = -1;
    72     bfs();
    73 
    74     outPre(24);
    75     cout << "(4, 4)
    ";
    76     return 0;
    77 }
    View Code

    string 

     1 /*
     2  很简单的广搜。如果不是+路径输出的话。想了一下 好像是dfs?好像不是啊。
     3 
     4  */
     5 
     6 #include <stdio.h>
     7 #include <string.h>
     8 #include <iostream>
     9 #include <queue>
    10 #include <string>
    11 using namespace std;
    12 
    13 struct Node {
    14     int x, y;
    15     string ansStep;
    16 }now, temp, ans;
    17 
    18 int mp[10][10];
    19 int vis[10][10];
    20 Node que[210];
    21 
    22 int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
    23 bool check(Node a) {
    24     if (a.x >= 0 && a.x < 5 && a.y >= 0 && a.y < 5 && !vis[a.x][a.y] && mp[a.x][a.y] == 0)
    25         return true;
    26     return false;
    27 }
    28 
    29 int head = 0, tail = 0;
    30 
    31 void bfs() {
    32     while(head < tail) {
    33         now = que[head++];
    34         if (now.x == 4 && now.y == 4) {
    35             now.ansStep += '';
    36             ans.ansStep = now.ansStep;
    37             return;
    38         }
    39         for (int i=0; i<4; ++i) {
    40             temp.x = now.x + dir[i][0];
    41             temp.y = now.y + dir[i][1];
    42 
    43             if (check(temp)) {
    44                 vis[temp.x][temp.y] = 1;
    45                 temp.ansStep = now.ansStep;
    46                 temp.ansStep += temp.x + '0';
    47                 temp.ansStep += temp.y + '0';
    48                 que[tail++] = temp;
    49             }
    50         }
    51     }
    52 }
    53 
    54 int main() {
    55     head = 0, tail = 0;
    56     memset(vis, 0, sizeof(vis));
    57 
    58     for (int i=0; i<5; ++i) {
    59         for (int j=0; j<5; ++j) {
    60             cin >> mp[i][j];
    61         }
    62     }
    63     now.x = 0, now.y = 0;
    64     now.ansStep += "00";
    65 
    66     vis[0][0] = 1;
    67     que[tail++] = now;
    68     bfs();
    69 
    70     string ansstr = ans.ansStep;
    71     int len = ansstr.length();
    72     for (int i=0; i<len && i+2<len; i+=2) {
    73         cout << "(" << ansstr[i] << ", " << ansstr[i+1] << ")" << endl;
    74     }
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    python中xrange和range的异同
    Python:使用threading模块实现多线程编程
    python Queue模块
    Python中pass语句的作用
    Python的作用域
    eclipse颜色配置
    protobuf
    python调试总结
    chardet安装
    Windows下搭建PHP开发环境
  • 原文地址:https://www.cnblogs.com/icode-girl/p/5164379.html
Copyright © 2020-2023  润新知