0

I am facing a wrong answer on my submission. I have run all the test cases that came to my mind but unfortunately all seem to give the correct output. I even checked out others' submissions but didn't get very far. Apparently idea seems to be fine but maybe I am missing some serious detail. I simply have no idea.

The link to the question is: https://www.codechef.com/CCRC21C/problems/ISREC

My code is:


    #include<bits/stdc++.h>
    using namespace std;
    
    int num_consec_ones(string row,int m)
    {
        int c=0;
        for(int i=0;i<m;i++)
        {
            if(row[i]==1)   c++;
            if(c>1 && row[i-1]==0 && row[i]==1)   
            {
                c=-1;
                break;
            }
        }
        return c;
        
    }
    int start_index(string row,int m)
    {
        for (int i=0;i<m;i++)
        {
            if(row[i]==1)
            return i;
        }
    }
    
    void solve()
    {
        int n,m,flag=0;;
        cin>>n>>m;
        vector<string>row;
       // row.reserve(n);
        for(int i=0;i<n;i++)
        {
            string str;
            cin>>str;
            row.push_back(str);
            for(int j=0;j<m;j++)
               row[i][j]=row[i][j]-'0';
    
        }
        set<int>s;
        for(int j=0;j<n;j++)
        {
            if(num_consec_ones(row[j],m)==-1)
            {
                cout<<"No"<<endl;
                return;
            }
            if(num_consec_ones(row[j],m) && flag==1 && num_consec_ones(row[j-1],m)==0)
            {
                cout<<"No"<<endl;
                return;
            }
            if(num_consec_ones(row[j],m) && flag==0)
            {
                s.insert(num_consec_ones(row[j],m));
                flag=1;
            }
        }
        if(s.size()==1)
        {
            set<int>start;
            for(int j=0;j<n;j++)
            {
                if(num_consec_ones(row[j],m))
                {
    
                    start.insert(start_index(row[j],m));
                }
            }
            if(start.size()==1)
            {
                cout<<"Yes"<<endl;
                return;
            }
            else{
                cout<<"No"<<endl;
                return;
            }
    
        }
        
    }
    
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            solve();
        }
    }

Please help me out. Thank you!

Alex
  • 183
  • 1
  • 9
  • Remember, many of the coding sites like corner-cases. How does your code behave if there is a single `1` in the entire grid? (it would be a rectangle, actually a square, same length all sides). That type of corner case, and any similar you can think of, can explain some failures. Since you are using a `std::vector` why not use member functions like `.find_first_of()` and then `.find_first_not_of()`, etc.. From reading the statement, you just need to ensure all rows with `1` start and end at the same location for consecutive rows (including 1-consecutive row). – David C. Rankin Feb 27 '21 at 06:09
  • yes if there is a single one in the entire grid then my code returns yes – Alex Feb 27 '21 at 06:23
  • i have tested almost all corner cases that come to my mind but i cant figure out which possible other test case it is failing – Alex Feb 27 '21 at 06:25
  • 1
    I'll have to go though it in detail. In the mean time, take a look at: [Why should I not #include ?](https://stackoverflow.com/q/31816095/3422102) – David C. Rankin Feb 27 '21 at 06:36
  • okay thank you. I will try using individual headers from now on – Alex Feb 27 '21 at 06:44
  • Maybe not the problem, but the site mentions `YES` and `NO` in capital letters – Damien Feb 27 '21 at 08:25
  • No no all cases are accepted. And anyway subtask#1 gives correct answer while subtask#2 doesn't – Alex Feb 27 '21 at 19:15

0 Answers0