繁体   English   中英

为什么我的代码通过在 c++ 中注释单个打印 cout 语句来给出不同的 output?

[英]Why my code is giving different output by just commenting a single printing cout statement in c++?

您可以从中阅读问题陈述: https://practice.geeksforgeeks.org/problems/find-whether-path-exist/0

以下是最后的 C++ 代码。 我得到不同的 output 当我在以下输入( 3 :可步行路径, 1 :源, 2 :目的地, 0 :墙)上运行 cout 语句时,我只是评论它:

1
8
3 3 3 3 0 0 3 0
1 3 3 3 3 3 3 2
3 3 0 3 0 3 3 3
3 3 3 0 0 3 3 0
0 3 3 3 3 3 3 3
0 0 0 3 3 0 3 3
0 3 0 3 3 3 3 0
3 3 3 0 3 3 3 3

在第 31 和 35 行,有cout<<"found: 1\n"; cout<<"found: 2\n"; 打印我用来调试代码的语句。 这些语句正在打印纯引号字符串(不使用/不包含任何变量)。 我开始知道,如果我不评论其中至少一行(比如说第 31 行),output 就像:

found: 1
1

如果我评论这两行,我会得到 output:

0

我无法理解程序的这种行为。 正确的 ans 是1 ,因为矩阵中存在12之间的路径。 但我不想打印我提到的仅用于调试的行。 所以在评论他们时我得到了错误的答案0 那么任何人都可以找到这种行为的错误/原因吗?
以下是您可以复制并粘贴到编辑器中的完整代码:

// { Driver Code Starts

#include<bits/stdc++.h>
using namespace std;

 // } Driver Code Ends






class Solution {
public:

    // int n = 0;
    // void printTab(int n) {
    //     while(n-- > 0) cout<<"  ";
    // }
    
    void DFS(int i, int j, bool *found1, bool *found2, 
    unordered_set<string> visited, vector<vector<int>>& grid) {
        // printTab(++n);
        // cout<<">> "<<i<<", "<<j<<"\n";
        // n--;
        
        visited.insert(i+"_"+j);
        if(grid[i][j] == 0) return;
        else if(grid[i][j] == 1) {
            *found1 = true;
            cout<<"found: 1\n";
        }
        else if(grid[i][j] == 2) {
            *found2 = true;
            // cout<<"found: 2\n";
        }
        
        // switch(grid[i][j]) {
        //     case 0: return;
        //     case 1: *found1 = true;cout<<"found: 1\n";break;
        //     case 2: *found2 = true;break;
        // }
        
        if(*found1 && *found2)
            return;
        
        int r, c;
        
        // Down
        r = i+1;
        c = j;
        if((r>=0 && r<grid.size() && c>=0 && c<grid[0].size() && !visited.count(r+"_"+c)))
            DFS(r, c, found1, found2, visited, grid);
        if(*found1 && *found2)
            return;
        
        // Left
        r = i;
        c = j-1;
        if((r>=0 && r<grid.size() && c>=0 && c<grid[0].size() && !visited.count(r+"_"+c)))
            DFS(r, c, found1, found2, visited, grid);
        if(*found1 && *found2)
            return;
            
        // Right
        r = i;
        c = j+1;
        if((r>=0 && r<grid.size() && c>=0 && c<grid[0].size() && !visited.count(r+"_"+c)))
            DFS(r, c, found1, found2, visited, grid);
        if(*found1 && *found2)
            return;
            
        // Up
        r = i-1;
        c = j;
        if((r>=0 && r<grid.size() && c>=0 && c<grid[0].size() && !visited.count(r+"_"+c)))
            DFS(r, c, found1, found2, visited, grid);
    }
    
    bool is_Possible(vector<vector<int>>& grid) {
        bool found1, found2;
        found1 = found2 = false;
        unordered_set<string> visited;
        
        for(int i=0; i<grid.size(); i++) {
            for(int j=0; j<grid[0].size(); j++) {
                if(!visited.count(i+"_"+j)) {
                    DFS(i, j, &found1, &found2, visited, grid);
                    if(found1 || found2)
                        return (found1 && found2);
                }
            }
        }
        return false;
    }
};

// { Driver Code Starts.
int main(){
    int tc;
    cin >> tc;
    while(tc--){
        int n;
        cin >> n;
        vector<vector<int>>grid(n, vector<int>(n, -1));
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                cin >> grid[i][j];
            }
        }
        Solution obj;
        bool ans = obj.is_Possible(grid);
        cout << ((ans) ? "1\n" : "0\n");
    }
    return 0;
}  // } Driver Code Ends

这是未定义的行为

if(!visited.count(i+"_"+j)) {

显然,您认为您正在形成一个字符串,例如"1_2" ,但实际上您正在通过将 integer 添加到char*指针来进行指针运算。

试试这个

if(!visited.count(std::to_string(i)+"_"+std::to_string(j))) {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM