Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 609121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:31:55+00:00 2026-05-13T17:31:55+00:00

I am preparing myself for programming competitions and i would like to know how

  • 0

I am preparing myself for programming competitions and i would like to know how can i solve this problem. I guess it’s geometry problem, and it seems i can’t get any ideas about solving it.

Here it is:

There is a yard in which there are wolves and sheep. In the yard there are also blocks which do not allow to pass. The wolves are represented with ‘w’ and the sheep with ‘s’, while the blocks are with ‘#’ and the space where everyone can move is ‘.’ . So a possible input will look like:

8 8
.######.
#..s...#
#.####.#
#.#w.#.#
#.#.s#s#
#s.##..#
#.w..w.#
.######.

The 2 numbers above the yard are rows x columns.

As you can see, by this there can be formed sectors of different kind in the yard. Here are two sectors:

####
#.w#
####
#s.#

In the first one there is a wolf and in the second a sheep. Because they are placed in two different sectors (i.e. the wolf can’t get to the sheep), he can’t eat it. If they were in a same sector, the wolf would eat the sheep.

My question for you is this: Given an input like the ones above, how should i calculate how many sheep will survive ? How can i represent the ‘yard’ in c++ ? How should the algorithm looks like ? Are there any materials for understanding similar problems and issues ?

Any help is appreciated. Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:31:55+00:00Added an answer on May 13, 2026 at 5:31 pm

    What you are looking for here is to find the connected components of the graph, then you just need to count the number of wolves and sheep in each one.

    using namespace std;
    
    int w, h;
    cin >> w >> h;
    vector<string> grid(h);
    for (int i = 0; i < h; ++i)
      cin >> grid[i];
    
    vector< vector<bool> > seen(h, vector<bool>(w, false));
    int survived = 0;
    const int mx[] = {-1, 0, 1, 0}, my[] = {0, -1, 0, 1};
    
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        if (!seen[i][j] && grid[i][j] != '#')
        {
          int sheep = 0, wolves = 0;
          typedef pair<int, int> point;
          stack<point> s;
          s.push(point(i, j));
    
          while (!s.empty())
          {
            point p = s.top();
            int x = p.first, y = p.second;
            if (grid[x][y] == 'w') wolves++;
            if (grid[x][y] == 's') sheep++;
            for (int k = 0; k < 4; ++k)
            {
              int x2 = x + mx[k], y2 = y + my[k];
              if (x2<0 || x2>=h || y2<0 || y2>=w) continue;
              if (grid[x2][y2] == '#' || seen[x2][y2]) continue;
              s.push(point(x2, y2));
            }
          }
          survived += max(0, sheep - wolves);
        }
    
    cout << "Surviving sheep = " << survived << endl;
    

    Running time and memory usage is optimal at O(rows x columns).

    Note that code is untested, but I believe this should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 342k
  • Answers 342k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer <?php $groups = array("FACULTY","STAFF"); if(isset($_SESSION['user_session'])) { $username = $_SESSION['user_session']; foreach… May 14, 2026 at 5:14 am
  • Editorial Team
    Editorial Team added an answer If I'm not mistaken it's because you're binding the hover… May 14, 2026 at 5:14 am
  • Editorial Team
    Editorial Team added an answer Here's what you want to do. Something akin to what's… May 14, 2026 at 5:14 am

Related Questions

Just preparing myself for a task that i feel could be quite troublesome, and
I am preparing for the development of an enterprise-style application for a very small
I am not sure if there is already a nomenclature for this, but for
I am preparing to use continuous integration for the first time. I will be
I am preparing a project proposal for a client who wants to publish video

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.