fzu 2041 Checker 枚举 模拟

Description

Checker game is an interesting game. But now Bob is tired of playing with others, he wants to play by himself. The following are the rules of his games:

1. The chessboard is a straight line which has n lattices.

2. There is only one kind of chess.

3. In the initial state, there are some chesses in the chessboard. There would be at most one chess in each lattice.

4. One chess can be moved directly to the adjacent empty lattice.

5. Your can move m steps at most. You can stop any time before you use up m steps.

6. The final score you get is the length of the longest continuous empty lattice.

Now Bob want to get as high score as possible. Can you tell him the possible highest score? The following is one example when m equals to 3. The highest score you can get in this case is 6.

Input

The first line of the input contains an integer T(T≤100), indicating the number of test cases. Each case begins with two integers n(1≤n≤500) and m(1≤m≤n*n), the number of lattices and the steps you can move at most, and is followed by a 0-1 string of length n. Here, 0 indicates an empty lattice and 1 indicates a full one.

Output

For each test case, print a line containing the test case number (beginning with 1) and the possible highest score Bob can get.

Sample Input

2
4 1
1010
10 3
1000100010

Sample Output

Case 1: 2
Case 2: 6

思路:

枚举每段空格,然后贪心扩大这段空格,看最大能扩大到多少,最后去最大值即可

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

typedef long long LL;
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int MAXN = 500 + 10;
int t, len, step;
char G[MAXN], CG[MAXN];

int Calc(int left, int right) {
    strcpy(CG, G);
    int cnt = step;
    int low = left - 1, high = right + 1;

    while (cnt--) {
        if (low - 1 < 0 && high + 1 >= len) break;
        for (;;) {
            if (low - 1 < 0 && high + 1 >= len) break;

            int lena = INF, lenb = INF;
            bool yes = false;

            if (low - 1 >= 0 && CG[low - 1] == '0') {
                lena = left - (low - 1);
                yes = true;
            } 

            if (high + 1 < len && CG[high + 1] == '0') {
                lenb = high + 1 - right;
                yes = true;
            }
            
            if (!yes) {
                --low;
                ++high;
                continue;
            }

            if (lena < lenb) {
                swap(CG[low], CG[low - 1]);
                if (CG[left - 1] == '0') {
                    --left;
                }
            } else {
                swap(CG[high], CG[high + 1]);
                if (CG[right + 1] == '0') {
                    ++right;
                }
            }

            low = left - 1;
            high = right + 1;
            break;
        }
    }
    return right - left + 1;
}

int main() {
#ifdef NIGHT_13
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    scanf("%d", &t);
    int cas = 0;
    while (t--) {
        scanf("%d%d%s", &len, &step, G);
        int ans = 0;
        for (int i = 0; i < len;) {
            if (G[i] == '0') {
                int j = i;
                while (j + 1 < len && G[j + 1] == '0') { ++j; }
                ans = max(ans, Calc(i, j));
                i = j + 1;
            } else {
                ++i;
            }
        }
        printf("Case %d: %d\n", ++cas, ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值