vendredi 1 mai 2020

How to Break out of Recursion Search after desired value found?

I am trying to solve a Competitive Programming problem, and I have created a method that returns a paired vector seen below:

bool warning = false;
vector<pair <int,int>> search(vector <pair <int,int>> vi){
    vector <pair <int,int>>m;
    if (warning){
        return m;
    }
    bool alg = false;
    if (vi.size() == 1){
        m = vi;
        warning = true;
    }
    for (int i = 0; i < vi.size()-1; i++){
        if (vi[i].second >= vi[i+1].first){
            alg = true;
            //auto it = vi.begin() + i;
            //auto it1 = vi.begin() + i + 1;
            vector <int> b{vi[i].first, vi[i].second, vi[i+1].first, vi[i+1].second};
            sort(b.begin(), b.end());
            vi.push_back(make_pair(b[0],b[3]));
            vi.erase(vi.begin() + i);
            vi.erase(vi.begin() + i);
            sort(vi.begin(), vi.end());
            //cout << "round one";
            search(vi);
            break;
        }
    }
    if (!alg){
        warning = true;
        m = vi;
        sort(vi.begin(), vi.end());
    }
}

Basically when I reach the bottom statement, I want to return the paired vector, and immediately exit out of the code, as that is what I am trying to achieve.

Thank you

Aucun commentaire:

Enregistrer un commentaire