c++ Diffucult图任务,无法弄清楚为什么我的代码返回错误的答案

gwbalxhn  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(95)

最近我接到一个任务
第三天,一个战略要地的防御一直在进行,并且已经宣布敌人将在明天晚上进行登陆作战。这个要地的Map被有条件地划分成方格,每个方格的平均高度是已知的。根据侦察兵的报告,登陆将在所有方格中平均分配。
我们决定在一些广场上建造活板门。当一个活板门被放置在一个广场上时,所有最终在那个广场上的登陆部队都会掉进活板门里。对防御者来说幸运的是,整个战场目前都被冰覆盖着,当登陆部队降落在一个被冰覆盖的广场上时,他们会滑倒并滑下斜坡。具体来说,如果可以从登陆点滚过方格的边界到达活板门而不升高高度,那么所有从该登陆点登陆的部队将滑入活板门。
它需要确定需要放置的最小数量的活板门,以便在登陆后抓住所有登陆部队。
输入格式:输入文件以两个整数N和M开始,这两个整数指定Map的尺寸-不超过100的自然数。接下来,有N行,每行包含M个数字,这两行指定Map方格的高度。高度是不超过10,000的自然数。假设位于Map外部的方格具有无限大的高度(即登陆部队永远不会到达那里)。
输出格式:输出捕获所有登陆部队所需的最小活板门数量。
我决定从相邻矩阵开始,其中两个正方形之间的边存在,如果我们可以从一个正方形到另一个正方形,如果它们是无边界的,并且我们可以在不提高高度的情况下到达正方形2。接下来,ChatGPT为我编写函数,返回我们可以从指定的顶点到达的所有顶点。
这种算法被称为“广度优先搜索(BFS)”或“广度优先遍历”。它从一个给定的顶点开始,在移动到更远的顶点之前,探索距离起始顶点相同距离的所有顶点。在这种实现中,它用于在无向图中找到从给定的目标顶点可以到达的所有顶点。
在main中,我得到了所有我们可以从零顶点得到的顶点,添加到向量“可达”,并继续这样做,直到查看所有顶点
这段代码在比赛中以20/40的成绩通过了6分,就像一个魅力,这就是我看不到的测试数据,我不知道我做错了什么。
一些示例

4 4
1 2 4 1
2 4 4 4
1 4 3 2
1 2 3 2

aaaa(0, 0), Vertices that can reach all other vertices: (0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (3, 1), (1, 2), 
aaaa(3, 0), Vertices that can reach all other vertices: (3, 0), 
aaaa(0, 2), Vertices that can reach all other vertices: (0, 2), (2, 2), (0, 3), (1, 3), (2, 3), 
aaaa(3, 2), Vertices that can reach all other vertices: (3, 2), (3, 3), 

4

个字符
这可能不是超级容易进入例子,但请记住,我们采取所有顶点,并完全不看它在下一步
完整代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include<algorithm>
#include <vector>
#include<stack>
#include <queue>

#define DEBUG 1

const int MAXD = 100;
const int MAXN = MAXD*MAXD;
using namespace std;
int heights[MAXD][MAXD];
bool adjGraph[MAXN][MAXN];
int w, h;
int n;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int toOne(int x, int y){
    return y*w + x;
}

int oneToX(int o){
    return o % w;
}
int oneToY(int o){
    return o / w;
}

void generateAdj(){
    for (int y1 = 0; y1 < h; ++y1) {
        for (int x1 = 0; x1 < w; ++x1) {
            for (int i = 0; i < 4; ++i) {
                int x2 = x1 + dx[i];
                int y2 = y1 + dy[i];
                if(x2 < 0 || y2 < 0 || x2 >= w || y2 >= h) continue;
                adjGraph[toOne(x1, y1)][toOne(x2, y2)] = heights[y1][x1] >= heights[y2][x2];
            }
        }
    }
}
vector<int> dfs(int u, bool visited[]) {
    visited[u] = true;
    vector<int> reachable = {u};
    for (int v = 0; v < n; v++) {
        if (adjGraph[u][v] && !visited[v]) {
            vector<int> sub_reachable = dfs(v, visited);
            reachable.insert(reachable.end(), sub_reachable.begin(), sub_reachable.end());
        }
    }
    return reachable;
}

vector<int> get_reachable_vertices(int target) {
    bool visited[MAXN] = {false};
    vector<int> reachable = dfs(target, visited);
    queue<int> q;
    for (int u : reachable) {
        visited[u] = true;
        q.push(u);
    }
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int v = 0; v < n; v++) {
            if (adjGraph[v][u] && !visited[v]) {
                visited[v] = true;
                q.push(v);
            }
        }
    }
    vector<int> result;
    for (int u = 0; u < n; u++) {
        if (visited[u]) {
            result.push_back(u);
        }
    }
    return result;
}
bool isElementInVector(std::vector<int>& vec, int elem) {
    for (int i = 0; i < vec.size(); i++) {
        if (vec[i] == elem) {
            return true;
        }
    }
    return false;
}

int main() {
    cin >> h >> w;
    n = w*h;
    if(n == 1){
        cout << 1;
        exit(0);
    }
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            cin >> heights[i][j];
        }
    }
    #ifdef DEBUG
    cout << "w:" << w << ", h:" << h << "\n";
    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < w; ++x) {
            int o = toOne(x, y);
            if(oneToX(o) != x || oneToY(o) != y) {
                cout << "ABOBA" << x <<", " << y;
            }
        }
    }
    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < w; ++x) {
            cout << heights[y][x] << " ";
        }
        cout << "\n";
    }
    #endif
    generateAdj();

    #ifdef DEBUG
    for (int y1 = 0; y1 < h; ++y1) {
        for (int x1 = 0; x1 < w; ++x1) {
            for (int y2 = 0; y2 < h; ++y2) {
                for (int x2 = 0; x2 < w; ++x2) {
                    if(adjGraph[toOne(x1, y1)][toOne(x2, y2)]) {
                        cout << x1 << "," << y1 << " -> " << x2 << "," << y2 << " " << "\n";
                    }
                }
            }
        }
    }
    #endif
    vector<int> reachable;
    int count = 0;
    for (int i = 0; i < n; ++i) {
        if(!isElementInVector(reachable, i)) {
            count++;
            vector<int> reachable2 = get_reachable_vertices(i);
            // Output the result
#ifdef DEBUG
            cout << "aaaa(" << oneToX(i) << ", " << oneToY(i) << "), ";
            cout << "Vertices that can reach all other vertices: ";
            for (int j : reachable2) {
                if(!isElementInVector(reachable, j)) {
                    cout << "(" << oneToX(j) << ", " << oneToY(j) << "), ";
                }
            }
            cout << endl;
#endif
            reachable.insert(reachable.end(), reachable2.begin(), reachable2.end());
        }

    }
    cout << count;
    return 0;
}

2ekbmq32

2ekbmq321#

检查此输入:

10 9
11 11 11 11 11 11 11 11 11
6 6 5 11 11 11 11 5 5
6 5 5 11 10 11 11 5 5
11 11 11 11 10 10 11 11 11
11 11 11 10 10 10 10 11 2
11 11 10 10 11 10 11 11 2
11 11 11 10 10 11 11 2 2
11 11 11 10 11 11 11 11 11
11 11 11 11 11 1 1 11 11
11 11 11 11 11 11 11 1 11

字符串
您的代码打印1,但在这种情况下必须是6。因此,请尝试调试代码并修复错误。

0lvr5msh

0lvr5msh2#

vector<int> dfs(int u, bool visited[]) {
    visited[u] = true;
    vector<int> reachable = {u};
    for (int v = 0; v < n; v++) {
        if (adjGraph[u][v] && !visited[v]) {
            vector<int> sub_reachable = dfs(v, visited);
            reachable.insert(reachable.end(), sub_reachable.begin(), sub_reachable.end());
        }
    }
    return reachable;
}

字符串
没有必要遍历图中的每个节点!
取代

for (int v = 0; v < n; v++) {


for( int v : reachable ) {

相关问题