为什么我的代码仅通过在 C++ 中注释单个打印 cout 语句就给出不同的输出?

问题描述

您可以从中阅读问题陈述:

enter image description here

以下是最后的C++代码。当我在以下输入 (3: walkable path,1: source,2: destination,0 : 墙):

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 行),输出就像:

found: 1
1

如果我评论这两行,我就会得到输出

0

我无法理解程序的这种行为。正确的答案是 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,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,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,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,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",但实际上您正在通过将整数添加到 char* 指针来进行指针运算。

试试这个

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