Different answers on "Run Code" and "Submit"


  • 0
    P

    I am submitting the solution, but it is showing different answer on RunCode and on Submit. However, answer on RunCode is coming correct, while incorrect on submission. How is this even possible ? Is Judge working properly ?

    I have attached the pics of output and the code below, for the verification.
    Thanks

    enter image description here
    Submit : Wrong Answer Output

    class Solution {
    public:
        void solveNQueensUtil(int n, int & count, vector<string> & status, vector<int> & row, vector<int> & col, vector<int> & sum, vector<int> & diff, int level)
        {
        	if(level == n)
        	{
        		count++;
        		//cout << count << endl;
        		return ;
        	}
        	for(int i=0; i<n; i++)
        	{
        		if(row[level] == 0 && col[i] == 0 && sum[level+i] == 0 && diff[n+(level-i)] == 0)
        		{
        			//cout << "here : " << level << endl;
        			string temp(n,'.');
        			temp[i] = 'Q';
        			status.push_back(temp);
        			row[level] = 1;
        			col[i] = 1;
        			sum[level+i] = 1;
        			diff[n+(level-i)] = 1;
        			solveNQueensUtil(n, count, status, row, col, sum, diff, level+1);
        			status.pop_back();
        			row[level] = 0;
        			col[i] = 0;
        			sum[level+i] = 0;
        			diff[n+(level-i)] = 0;
        		}
        	}
        	return ;
        }
        int totalNQueens(int n) {
                vector<string> status;
            vector<int> row(n, 0);
            vector<int> col(n, 0);
            vector<int> sum(2*n-1, 0);
            vector<int> diff(2*n-1, 0);
            //vector<vector<string> > res;
            int count = 0;
            solveNQueensUtil(n, count, status, row, col, sum, diff, 0);
            status.clear();
            row.clear();
            col.clear();
            sum.clear();
            diff.clear();
            return count;
        }
    };
    

  • 0

    status and temp aren't actually needed, are they? Please don't include unnecessary stuff that just makes the program harder to analyze.

    To answer your question: The problem is that your code has invalid memory access caused by an off-by-1 bug. And you're only lucky that it doesn't affect the custom testing.

    If you want, I can tell you the exact bug, but I think you can also find it.


  • 0
    P

    Yeah,

    These are actually required in "N Queen I" problem, I directly used code from there. Now, I have removed 'temp' and 'status'. Now, the same problem is coming for n = 8. However, it has cleared the test case for n = 9.

    New Error


  • 0
    P

    After removing 'temp' and 'status', the same problem is coming for n = 8. However, it has cleared the test case for n = 9.


  • 0

    Yeah, they're just a little nuisance but they're not where your bug is. Did you find it yet?


  • 0
    P

    Yea, I find it in diff[] array, index should be [n-1+(level-i)]. It was [n-1+(level-i)] before.

    Thanks a lot,
    Stefan


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.