Example #1
0
int friendCircles(vector < string > friends) {
    Circles circles;
    circles.push_back(Circle());
    Circle discovered;
    for (int ii = 0; ii < friends.size(); ii++)
    {
        for (int jj = 0; jj < friends[ii].length(); jj++)
        {
            if (std::find(discovered.begin(), discovered.end(), make_pair(ii, jj))
                    != discovered.end())
            {
                continue;
            }
            cout << "looking from main, " << ii << ", " << jj << endl;
           lookAround(friends, ii, jj, circles, discovered);
           if (!circles.back().empty())
           {
            circles.push_back(Circle());
           }
        }
    }
    cout << "found " << circles.size() << " circles" << endl;
    dumpCircles(circles);
    return 0;
}
Example #2
0
int lookAround(Friend & friends, int ii, int jj, Circles & circles, Circle & discovered)
{
    cout << "looking for a friend at " << ii << ", " << jj << endl;
    dumpCircles(circles);
    if (ii < 0 || jj < 0 || ii > friends.size() - 1 || jj > friends[0].length() - 1)
    {
        //        cout << "out of bounds" << endl;
        return 0; // out of bounds
    }
    //    cout << "bounds ok" << endl;
    if (friends[ii][jj] == 'N')
    {
        //        cout << "not a friend" << endl;
        return 0;    // no friends here
    }
    //    cout << "I am someones friend" << endl;
    if (std::find(discovered.begin(),
                discovered.end(),
                make_pair(ii, jj)) != discovered.end())
    {
        cout << "already a friend" << endl;
        return 0; // already a friend
    }
    //    cout << "I am a new friend" << endl;
    circles.back().push_back(make_pair(ii, jj));
    discovered.push_back(make_pair(ii, jj));
    int newFriends(
            + lookAround(friends, ii, jj - 1, circles, discovered) // look left
            + lookAround(friends, ii, jj + 1, circles, discovered) // look right
            + lookAround(friends, ii - 1, jj, circles, discovered) // look up
            + lookAround(friends, ii + 1, jj, circles, discovered)); // look down
    return newFriends;
}