HDU - 4662 MU Puzzle——思维

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 3e6 + 10;
int ans = 0, cnt = 0;
bool vis[maxn];
void bfs() {
    memset(vis, false, sizeof(vis));
    queue<int> q;
    q.push(1);
    vis[1] = true;
    while (!q.empty()) {
        int x = q.front(); q.pop();
        cnt++;
        if (x * 2 <= 3e6 && !vis[x*2]) {
            vis[x*2] = true;
            q.push(x*2);
        }
        if (x - 6 >= 0 && !vis[x-6]) {
            vis[x - 6] = true;
            q.push(x-6);
        }
    }
}
int T;
char str[maxn];
int main() {
    bfs();
    scanf("%d", &T);
    while (T--) {
        scanf("%s", str);
        int cntm = 0, posm = 0, cnti = 0;
        for (int i = 0; str[i]; i++) {
            if (str[i] == 'M') {
                cntm++;
                posm = i;
            }
            else if (str[i] == 'I') cnti++;
            else cnti += 3;
        }
        if (cntm == 1 && posm == 0) {
            if (vis[cnti]) printf("Yes\n");
            else printf("No\n");
        }
        else printf("No\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hao_zong_yin/article/details/80160492