码迷,mamicode.com
首页 > 其他好文 > 详细

LightOJ 1389 - Scarecrow(贪心啊)

时间:2015-03-29 10:53:17      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:lightoj   贪心   

题目链接:http://lightoj.com/volume_showproblem.php?problem=1389


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 x 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 (≤ 50), 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, print the case number and the number of scarecrows that need to be placed.

Sample Input

Output for Sample Input

3

3

.#.

11

...##....##

2

##

Case 1: 1

Case 2: 3

Case 3: 0


题意:

每个稻草人可以覆盖自己所在点和左右两个点,求使用最少的稻草人覆盖完所有可以种植的点(‘.’);

代码如下:

#include <cstdio>
#include <cstring>

int main()
{
    int t;
    int n;
    char s[147];
    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        scanf("%s",s);
        //int len = strlen(s);
        int ans = 0;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '#')
                continue;
            if(s[i] == '.')
            {
                ans++;
            }
            i += 2;
        }
        printf("Case %d: %d\n",++cas,ans);
    }
    return 0;
}


LightOJ 1389 - Scarecrow(贪心啊)

标签:lightoj   贪心   

原文地址:http://blog.csdn.net/u012860063/article/details/44724599

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!