Solution of 12405 - Scarecrow

Problem Description
source: https://uva.onlinejudge.org/external/124/12405.html

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 

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.

Output

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

Sample Input 

2
10
..#..##...
13
..#..##....#.


Sample Output 

Case 1: 3
Case 2: 4



Solution:

#include <algorithm>
#include <cstdio>
#include <cmath>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

int main()
{
    int i,j,len,t,count;
    char ch[105];
    while(scanf("%d",&t)==1)
    {
        for(i=1;i<=t;i++)
        {
            scanf("%d",&len);
               scanf("%s",&ch);

            count=0;
            for(j=0;j<len;j++)
            {
                if(ch[j]=='.')
                {
                    ch[j+1]='#';
                    count+=1;
                    j+=2;
                }
            }
            printf("Case %d: %d\n",i,count);
        }
    }
    return 0;
}

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience