3 solutions

  • 0
    @ 2022-4-7 20:09:29

    不太会的dfs算法 QAQ

    #include<iostream>
    
    using namespace  std;
    bool light[35];
    long long ans;
    
    void  dfs(int n)
    {
    	if (n == 31)//边界判断
    	{
    		ans++;
    		return;
    	}
    	if (light[n - 1]) dfs(n + 1);
    	//前灯亮则此灯不亮,进入下一层搜索
    	else {//前灯不亮
    		light[n] = true;//标记
    		dfs(n + 1);//此灯亮
    		light[n] = false;//重新标记
    		dfs(n + 1);//此灯不亮
    	}
    
    }
    int main(void)
    {
    	int sum = 30;
    	dfs(1);
    	cout << ans;
    
    	return 0;
    }

    Information

    ID
    52
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    4
    Tags
    # Submissions
    29
    Accepted
    17
    Uploaded By