Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过多个点的最短路径[C++]_C++_Breadth First Search - Fatal编程技术网

通过多个点的最短路径[C++]

通过多个点的最短路径[C++],c++,breadth-first-search,C++,Breadth First Search,给定一个网格,其中每个单元格的内容都可以 X:墙 H:一个检查站,最多5个 G:起始位置正好是1 我必须找到从G开始覆盖所有检查点H所需的最小移动次数。单个移动可以是水平移动也可以是垂直移动,尽管对角线移动可以通过结合水平移动和垂直移动来实现。无法移出网格或移动到墙X。 对于网格 ..H.. ..X.H G...X 预期答案是7 网格中的行数和列数最多可以为20 我试着编写bfs代码来解决这个问题,但是当使用上面的网格进行测试时,它会生成6,这是不正确的 我不知道我做错

给定一个网格,其中每个单元格的内容都可以

X:墙

H:一个检查站,最多5个

G:起始位置正好是1 我必须找到从G开始覆盖所有检查点H所需的最小移动次数。单个移动可以是水平移动也可以是垂直移动,尽管对角线移动可以通过结合水平移动和垂直移动来实现。无法移出网格或移动到墙X。 对于网格

    ..H..
    ..X.H
    G...X
预期答案是7

网格中的行数和列数最多可以为20

我试着编写bfs代码来解决这个问题,但是当使用上面的网格进行测试时,它会生成6,这是不正确的

我不知道我做错了什么。有人能帮忙吗

#include <iostream>
#include &lt;bits/stdc++.h>
#include &lt;algorithm>
#include &lt;cstdio>
#include &lt;stdlib.h>
#include &lt;cmath>
#include &lt;cstring>
#include &lt;math.h>
#include &lt;string>
#include &lt;sstream>
#include &lt;vector>
#include &lt;iomanip>
#include &lt;deque>
#include &lt;queue>

#define loop(i, n) for(int i = 0; i < n; i++)  
#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0' < (_=getchar());x=(x<<3)+(x<<1)+_-'0');}while(0)  
#define ull unsigned long long  

char _;

using namespace std;
typedef pair <int, int> ipair;

int rows, columns, startx, starty, hiders;

char grid[20][20];
char buffer[255];

bool valid(int x, int y){  
    if(x >= 0 && x < rows && y >= 0 && y < columns && grid[x][y] != 'X')  
        return true;  
    return false;  
}  

int main(){  
    scanf("%i%i%i", &rows, &columns, &hiders);  

    //this is where I set up the grid, at put it in the grid array.  

    for(int i = 0; i < rows; i++){  
        scanf("%s", &buffer);  
        for(int j = 0; j < columns; j++){  
            if(buffer[j] == 'G'){  
                startx = i;  
                starty = j;  
            }  
            grid[i][j] = buffer[j];  
        }  
    }  

    queue<ipair> Q; //keeps track of positions  
    queue<int> DQ; //keeps track of distance  
    int found = 0; //how many checkpoints are found  

    Q.push(make_pair(startx, starty)); //push start pos  
    DQ.push(0);  

    while(!Q.empty()){  
        int x = Q.front().first; //check x  
        int y = Q.front().second; //check y  

        if(found == hiders){ //if we found the amount needed  
            printf("%i", DQ.front()); // print current distance  
            return 0;  
        }  

        if(grid[x][y] == 'H'){ //found checkpoint  
            found++; //found a hider  
        }  
        //if it's possible to move right  
        if(valid(x + 1, y)){  
            Q.push(make_pair(x+1, y));  
            DQ.push(DQ.front() + 1);  
        }  
        //if it's possible to move left  
        if(valid(x - 1, y)){  
            Q.push(make_pair(x-1, y));  
            DQ.push(DQ.front() + 1);  
        }  
        //if it's possible to move up  
        if(valid(x, y + 1)){  
            Q.push(make_pair(x, y + 1));  
            DQ.push(DQ.front() + 1);  
        }  
        //if it's possible to move down  
        if(valid(x, y - 1)){  
            Q.push(make_pair(x, y - 1));  
            DQ.push(DQ.front() + 1);  
        }  

        Q.pop();  
        DQ.pop();  
        grid[x][y] = 'X'; //set the current coordinates as visited/walls  
    }  
}  

这不是BFS可以解决的问题

乍一看,分支和绑定或动态是前进的方向

B&B肯定会让你达到目的,但它很难实现,而且比动态的慢


第一个出现在脑海中的动态将需要5个20x20网格,但我不能100%确定这是第一眼反馈

我想你最好在编程面试/竞赛QA网站上问这个问题。但是为了使您获得答案的机会最大化,请至少给出一个输入及其预期正确输出的示例。我们也不容易判断出哪里出了问题,所以您能告诉我们吗?您有构建问题吗?运行时崩溃?错误或意外的输出?请提供更多详细信息,如果可能的话,请尝试通过在调试器中运行并逐行检查代码来缩小范围。是否需要最短路径以覆盖所有检查点?@sasha保证存在有效路径。顺便说一句,将grobal变量命名为\uz是非法的。