USACO 2017 January Contest, Bronze Problem 3. Cow Tipping

Problem Info

USACO 2017 January Contest, Bronze Problem 3. Cow Tipping

My Work

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

#define FOR(i,a,b); for (int i = a; i < b; i++)
int main()
{
    freopen("cowtip.in","r",stdin);
    freopen("cowtip.out","w",stdout);
    int n;
    cin >> n;
    vector<string> data;
    string line;
    bool swaps[n][n];
    int ans = 0;
    int word;
    //set up bool array
    FOR(i,0,n) {
        FOR(j,0,n) {
            swaps[i][j] = false;
        }
    }
    //set up data
    FOR(i,0,n) {
        cin >> line;
        data.push_back(line);
    }
    //decide if a new flip is needed
    FOR(i,0,n) {
       FOR(j,0,n) {
           if(data[i][j] == '0') {
                continue;
            }else if(swaps[i][j] == true) {
                continue;
            }else {
                //decide how big the flip should be
                FOR(l,0,n - i) {
                    FOR(k,0,n - j) {
                        if(data[i+l][j + k] == '0') {
                            if(data[i+l + 1][j] == '1') {
                                continue;
                            }else {
                                ans += 1;
                                break;
                            }
                        }else if(j+k == n -1 && i+l == n -1) {
                            swaps[i+l][j+k] = true;
                            ans += 1;
                            break;
                        }else{
                            swaps[i+l][j+k] = true;
                        }
                    }
                }
            }
        }
    }
    cout << ans;




}

I created a bool array to keep track of what cows have already been flipped. the first 4 test cases ran fine but from the 5th test case it shows a run time error.

What I’ve Tried

I have tried downloading the test data, but i couldn’t figure out why the error happened

Question

Why the error happened when the first 4 test case run fine.

Checklist

See How to ask for help on a problem for more information.

  • Include a link to the problem
  • Format your post (especially code blocks)
  • Include what you’ve tried so far
  • Add comments to your code
  • Read the debugging module

If you haven’t found a small test case on which your solution fails, we expect that you tried (but failed) to generate a small counter-test (as described in the above module). Include the generator code.