中国石油大学个人训练赛第五场 e题

题目描述

We have a board with a 2×N grid. Snuke covered the board with N dominoes without overlaps. Here, a domino can cover a 1×2 or 2×1 square.
Then, Snuke decided to paint these dominoes using three colors: red, cyan and green. Two dominoes that are adjacent by side should be painted by different colors. Here, it is not always necessary to use all three colors.
Find the number of such ways to paint the dominoes, modulo 1000000007.
The arrangement of the dominoes is given to you as two strings S1 and S2 in the following manner:
Each domino is represented by a different English letter (lowercase or uppercase).
The j-th character in Si represents the domino that occupies the square at the i-th row from the top and j-th column from the left.

Constraints
1≤N≤52
|S1|=|S2|=N
S1 and S2 consist of lowercase and uppercase English letters.
S1 and S2 represent a valid arrangement of dominoes.

输入

Input is given from Standard Input in the following format:
N
S1
S2

输出

Print the number of such ways to paint the dominoes, modulo 1000000007.

样例输入

3
aab
ccb

样例输出

6

提示

There are six ways as shown below:

题目大意:

有个2*N的方格,一共有3种颜色,我们在这2*N的方格上放入多米诺骨牌,多米诺骨牌所占的空间是1*2的,然后用这三种颜色给多米诺骨牌涂色,要求相邻的两个过米诺骨牌的颜色不能够相同,问:一共有多少种排法。题目的输入已经规定好多米诺骨牌的摆放方式了。

思路:

我们只考虑局部,不考虑整体,从前面依次往后遍历,遍历的时候我们会遇到以下6种情况:

情况一:

这种情况涂色一共有6种可能。

情况二:

这种情况涂色一共有3种可能。

情况三:

这种情况涂色一共有2种可能。

情况四:

这种情况涂色只有一种可能。

情况五:

这种情况涂色有3中涂色可能。

情况六:

这种情况涂色有2种可能。

我们通过遍历所给定的两个字符串来分辨出是哪种情况,然后累乘上该种情况的涂色的可能,输出即可。

代码:

​
#include<bits/stdc++.h>
#define MAXN 1000000007
using namespace std;
int n;
string op1,op2;
int main()
{
    cin>>n>>op1>>op2;
    long long t=1;
    int p=0;//通过p来判断是哪种情况,p等于1代表前一列是一个竖着的多米诺骨牌,p等于2代表前一列是两个横着的多米诺骨牌
    for(int i=0;i<n;i++)
    {
        if(i==0)
        {
            if(op1[i]==op2[i])//上图中的情况二
            {
                 t*=3;//累乘上3种可能性
                 p=1;//此时对于下一列来说,它的前一列是一个竖着的多米诺骨牌了,把p标记为1
            }
            else
            {
                t*=6;//上图中的情况一
                p=2;//此时对于下下一列来说,它的前一列是两个横着的多米诺骨牌了,把p标记为2
                i++;//两个横着的多米诺骨牌占两列,我们只访问两列中的前一列就好,后一列直接跳过
            }
        }
        else
        {
            if(op1[i]==op2[i])
            {
                if(p==1)//上图中的情况三
                    t*=2;//累乘上2种可能性
                else    //上图的情况四
                    t*=1;//累乘上1种可能性
                p=1;//此时对于下一列来说,它的前一列是一个竖着的多米诺骨牌了,把p标记为1
            }
            else
            {
                if(p==1)//上图的情况六
                    t*=2;//累乘上2种可能性
                else    //上图的情况五
                    t*=3;//累乘上3种可能性
                p=2;//此时对于下下一列来说,它的前一列是两个横着的多米诺骨牌了,把p标记为2
                i++;
            }
        }
    }
    cout<<t%MAXN;
}

​

猜你喜欢

转载自blog.csdn.net/qq_40938077/article/details/81261707
今日推荐