题目链接

题目

题目描述

BLUESKY007,fengxunling和dreagonm三个人发现了一个像素游戏,这款神奇的游戏每次会生成一个nxm的网格,其中每一个格子都被随机染色为R,G,B三种颜色之一,每次都可以选择任意一个非B颜色的格子进行一次操作,每次操作都会满足以下规则:

  1. 操作的范围为从整个网格的左上角到选定方格的矩形区域

  2. 操作区域内所有方格都遵循 \(R\rightarrow G,G\rightarrow B,B\rightarrow R\) 变换

  3. 第一个不能执行操作的人为失败者,且按操作顺序在失败者之前的人取胜

为了能让BLUESKY007感到快乐(照顾到BLUESKY007是个蒟蒻),fengxunling和dreagonm的操作都尽可能的让BLUESKY007取胜,她们想知道在操作顺序为 \(BLUESKY007\rightarrow fengxunling\rightarrow dreagonm\rightarrow\cdots\) 的情况下,失败者是谁.

输入描述

题目有多组数据
第一行一个整数t,表示数据组数
对于每组数据,第一行两个整数n,m,接下来n行每行m个字符

输出描述

输出共t行,每行一个字符串表示答案

示例1

输入

2
3 3
RGG
BBG
RRR
3 3
GRB
RGR
RBG

输出

dreagonm
fengxunling

备注

对于 \(25\%\) 的数据, \(\forall n,m\leq3\)
对于另 \(25\%\) 的数据, \(\forall n=1,m\leq100\)
对于\(100\%\)的数据, \(1\leq t\leq100,1\leq n,m\leq10^3,1\leq n\cdot m\leq10^5\)

题解

知识点:博弈论。

注意到,无论怎么操作,最后一定是 \((1,1)\) 这个位置最后变成 \(B\) ,因此考虑它变到 \(B\) 需要的轮数即可。

时间复杂度 \(O(nm)\)

空间复杂度 \(O(1)\)

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

bool solve() {
    int n, m;
    cin >> n >> m;
    int ans = 0;
    for (int i = 1;i <= n;i++)
        for (int j = 1;j <= m;j++) {
            char ch;
            cin >> ch;
            if (i == 1 && j == 1) ans = ch == 'R' ? 2 : ch == 'G' ? 1 : 0;
        }
    cout << (ans == 0 ? "BLUESKY007" : ans == 1 ? "fengxunling" : "dreagonm") << '\n';
    return true;
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        if (!solve()) cout << -1 << '\n';
    }
    return 0;
}