中国石油大学个人训练赛第五场 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;
}

​

收获:

对于有些题目,如果问有多少种可能,我们可以把所有的情况都列举出来,然后把每种情况的可能累乘,求的结果,上题就是。不过这种解法只适用于当前的选择只对局部的选择有影响,对全局的选择没有影响

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值