Scarecrow UVA – 12405 | Solution

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Taso owns a very long field. He plans to grow different types of crops in the upcoming growing season. The area, however, is full of crows and Taso fears that they might feed on most of the crops. For this reason, he has decided to place some scarecrows at different locations of the field. The field can be modeled as a 1×N grid. Some parts of the field are infertile and that means you cannot grow any crops on them. A scarecrow, when placed on a spot, covers the cell to its immediate left and right along with the cell it is on. Given the description of the field, what is the minimum number of scarecrows that needs to be placed so that all the useful section of the field is covered? Useful section refers to cells where crops can be grown.


Input starts with an integer T (≤ 100), denoting the number of test cases. Each case starts with a line containing an integer N (0 < N < 100). The next line contains N characters that describe the field. A dot (.) indicates a crop-growing spot and a hash (#) indicates an infertile region.


For each case, output the case number first followed by the number of scarecrows that need to be placed.

Sample Input








Sample Output

Case 1: 1

Case 2: 3

Case 3: 0

Solution Code

#include <iostream>
#include <vector>
using namespace std;

int main()
    int t, count = 1;
    cin >> t;
    while ( t-- )
        int n;
        cin >> n;
        vector<char> crr(n + 2, '#');
        for (int i = 0; i < n; ++i)
            cin >> crr[i];

        int scarecrows = 0;
        for (int i = 0; i < n; i++)

            if (crr[i] == '.')
                crr[i] = crr[i + 1] = crr[i + 2] = '#';
        cout << "Case "
             << count++
             << ": "
             << scarecrows
             << endl;
    return 0;

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Do You Want To Contact With me?

drop us a line and keep in touch