[英]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
,因為矩陣中存在1
和2
之間的路徑。 但我不想打印我提到的僅用於調試的行。 所以在評論他們時我得到了錯誤的答案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.