Python code gives AC but C++ code produces the wrong output. Why?

Here is the link to the problem:
Now the thing is that I first wrote the code in C++ but that code was producing the wrong output for the sample test cases. Then I wrote the code in Python using the same logic and the same statements (the Python way) and it was producing the correct outputs. Now I can’t figure out what was wrong with the C++ code. Can anyone please help me with this?

C++ Code:

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

#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
    int grid[6][6];
    int h,w,k;
    cin >> h >> w >> k;
    char x;
    for(int i=0; i<h; i++)
        for(int j=0; j<w; j++)
            cin >> x;
            if(x == '#') grid[i][j] = 1;
//    for(int i=0; i<h; i++)
//    {
//        for(int j=0; j<w; j++)
//            cout << grid[i][j] << " ";
//        cout << "\n";
//    }
    int cnt = 0;
    for(int i=0; i<(1<<h); i++)
        for(int j=0; j<(1<<w); j++)
            int black = 0;
            for(int m=0; m<h; m++)
                for(int n=0; n<w; n++)
                    if((1&(i>>m) == 0) && (1&(j>>n) == 0) && (grid[m][n] == 1)) black++;
            if(black == k) cnt++;
    cout << cnt << "\n";
    return 0;

Python Code:

h, w, k = map(int,input().split())
grid = [[] for x in range(h)]
for i in range(h):
    grid[i] = input()
cnt = 0
for i in range(1<<h):
    for j in range(1<<w):
        black = 0
        for p in range(h):
            for q in range(w):
                if 1&(i>>p) == 0 and 1&(j>>q) == 0 and grid[p][q] == '#':
                    black += 1
        if black == k:
            cnt += 1

Note: Don’t know why the Python code is not color formatted correctly. (I did use ```)

somewhat offensive comment so remove. :slightly_smiling_face:

1 Like

Hey, did you just copy-paste that line from somewhere without even seeing what is being asked?

1 Like

I think there is some issue with & operator precedence.

After adding parenthesis your code works fine. ( I just checked for one test case which is present locally, isn’t opening)

if(((1&(i>>m)) == 0) && ((1&(j>>n)) == 0) && (grid[m][n] == 1)) black++;

To avoid this kind of mistake in the future you could use -Wparentheses while compiling your code.

1 Like

It worked for all the test cases. :smile:
I actually added those parenthesis while I was trying to debug, I should have been more generous and should have added some more of them. :laughing:
Thanks. :smile:

Oh No! @hello_hell The C++ code is giving WA on submission. But it worked perfectly fine for the sample test cases. :thinking:

You can get the testcases from this link. Just download them locally and see where your code is incorrect.

1 Like

OK. Thanks :slightly_smiling_face:

sorry mate,the code looked like the question 3 of ongoing live contest. Again Sorry and I"ll delete my answer

Sometimes it happens, no problem bro. Yesterday I answered Dijsktra optimization here. But after some comment, I figured out that I’m reading the input wrong. So it’s natural.