24h購物| | PChome| 登入
2012-04-19 09:30:13| 人氣2,453| 回應0 | 上一篇 | 下一篇

[UVA][Greedy] 12405 - Scarecrow

推薦 0 收藏 0 轉貼0 訂閱站台

C

Scarecrow

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 (≤ 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

Output for Sample Input

3
3
.#.
11
...##....##
2
##
Case 1: 1
Case 2: 3
Case 3: 0

做法 : 說是 Greedy 倒不如說是 特例處理
一個稻草人, 可以控制左中右三個區塊, 必須控制所有田地, 問最少的稻草人數目

#include <stdio.h>

int main() {
    int t, n, Case = 0;
    char str[101];
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        scanf("%s", str);
        int i, ans = 0, tmp = 0;
        for(i = 0; str[i]; i++) {
            if(str[i] == '.') {
                tmp ++;
                if(tmp == 3) {
                    ans++;
                    tmp = 0;
                }
            } else {
                if(tmp == -1) {
                    tmp = 0;
                } else if(tmp == 1) {
                    ans++;
                    tmp = -1;
                } else if(tmp == 2) {
                    ans++;
                    tmp = 0;
                }
            }
        }
        if(tmp > 0) ans++;
        printf("Case %d: %d\n", ++Case, ans);
    }
    return 0;
}

台長: Morris
人氣(2,453) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 1210 - Sum of Consecutive Prime Numbers
此分類上一篇:[UVA][EASY] 12403 - Save Setu

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文