Blue Bridge Nations Grand Exams --- Laser Style

x To increase the planet's grand festival atmosphere, lined with 30 machine light, a beam of light shot into space.
When installation is found, for unknown reasons, the adjacent two lasers can not be opened at the same time!
King would like to know, in the current circumstances this bug exists, how many kinds of laser effects can play altogether?

Obviously, if only three machines, can total into five styles, namely:
all shut (sorry, at this time silence speaks, which is considered a)
to open a total of three kinds of
open two, only one kind

30 bad Well, the king had to make you useful.

Requested an integer representing the number of kinds of pattern 30 can be formed of a laser.

Note, submit only one integer, do not fill in any extra content.

 

 

Ideas, beginning from the first open lamp, each lamp can turn, depends only on the previous state of the lamp

 

#include<iostream>
using namespace std;
bool a[31]={false};   //开始状态设为所有灯未点亮 
long long cut=0;     //记录可以有多少种点法 
void dfs(int x){   //设置第X盏灯的开关 
    if(x>30){     //如果X大于30 ,则获得了一种灯开关的排列方式 
        cut++;
        return ;
    }
    dfs(x+1);     //默认当前灯为关,继续下一盏灯的开关设置 
    if(a[x-1]==false){   //如果前一盏灯为关闭的,则设置当前为开的,进行下一个尝试 
        a[x]=true;   //开灯 
        dfs(x+1);
        a[x]=false;  //状态还原 
    }
}
int main(){
    dfs(1);
    cout<<cut<<endl;
    return 0;
} 
//答案 :2178309 

 

Published 42 original articles · won praise 16 · views 3418

Guess you like

Origin blog.csdn.net/qq_41542638/article/details/90439388