Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

comments - Why my code is giving different output by just commenting a single printing cout statement in c++?

The problem statement you can read from this: https://practice.geeksforgeeks.org/problems/find-whether-path-exist/0

Following is the C++ code in the last. I am getting different output by just commenting the cout statements when I run it on the following input (3: walkable path, 1: source, 2: destination, 0: wall):

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

On the Line no 31st and 35th, there are cout<<"found: 1 "; and cout<<"found: 2 "; printing statements that I was using to debug the code. The statements are printing pure quoted strings (doesn't uses/includes any variable). I came to know that if I don't comment at least one of the line (let say only 31st line), the output is like:

found: 1
1

And if I comment both of the lines I am getting output:

0

I am not able to understand this behavior of the program. The correct ans is 1 as there is a path between 1 and 2 in the matrix. But I don't want to print the lines I have mentioned that was only for debugging. So on commenting them I am getting wrong answer 0. So can anyone find the fault/reason for such behavior?
Following is the entire code that you can copy and paste into your editor:

// { 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--;
        
        visited.insert(i+"_"+j);
        if(grid[i][j] == 0) return;
        else if(grid[i][j] == 1) {
            *found1 = true;
            cout<<"found: 1
";
        }
        else if(grid[i][j] == 2) {
            *found2 = true;
            // cout<<"found: 2
";
        }
        
        // switch(grid[i][j]) {
        //     case 0: return;
        //     case 1: *found1 = true;cout<<"found: 1
";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
" : "0
");
    }
    return 0;
}  // } Driver Code Ends
question from:https://stackoverflow.com/questions/65651359/why-my-code-is-giving-different-output-by-just-commenting-a-single-printing-cout

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is undefined behaviour

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

Clearly you think you are forming a string such as "1_2" but actually you are doing pointer arithmetic by adding an integer to a char* pointer.

Try this instead

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...