[AtCoder](2697)Coloring Dominoes ---- 思维(规律)

题目链接

思路: 一开始知道这应该是道找规律的题目,但是想的特别复杂,考虑了很多情况。但是在以前杭电做过一道很简单的递推,就想起来可以把这种模型转换成两种情况。
第一种情况:一个竖的多米诺,这里简称为X
第二种情况:两个横着叠在一起的多米诺,这里简称为Y

第一种情况涂颜色有3种
第二种情况涂颜色有6种

现在我们讨论一下四种情况:
(1)X->X (即在X右边添加X),画图可知有2种,原基础*2
(2)X->Y (即在X右边添加Y),画图可知有2种,原基础*2
(3)Y->Y (即在Y右边添加Y),画图可知有3种,原基础*3
(4)Y->X (即在Y右边添加X),画图可知有1种,原基础*1
这里写图片描述

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
int main()
{
    #ifdef LOCAL_FILE
    freopen("in.txt","r",stdin);
    #endif // LOCAL_FILE
    ios_base::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    char s1[105],s2[105];
    int n,last,i=0;
    long long ans = 1;
    cin>>n;
    cin>>s1;
    cin>>s2;
    while(i<n)
    {
        if(i == 0)
        {
            if(s1[i] == s2[i])
            {
                ans = ans*3;
                last = 1;
                i++;
            }
            else
            {
                ans = ans*6;
                last = 2;
                i+=2;
            }
        }
        else
        {
            if(s1[i] == s2[i])
            {
                if(last == 1) ans = (ans*2)%mod; //X->X
                else if(last == 2) ans = (ans*1)%mod; //Y->X
                last = 1;
                i++;
            }
            else
            {
                if(last == 1) ans = (ans*2)%mod;//X->Y
                else if(last == 2) ans = (ans*3)%mod; //Y->Y
                last = 2;
                i+=2;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37624640/article/details/81261397