hdu 5600 N bulbs

探讨了如何通过懒熊的特定路径改变灯泡状态,最终实现关闭所有灯的目标。文章提供了一个算法解决方案,用于判断是否能成功关闭所有灯,并给出示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


N bulbs

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 241    Accepted Submission(s): 132


Problem Description
N bulbs are in a row from left to right,some are on, and some are off.The first bulb is the most left one. And the last one is the most right one.they are numbered from 1 to n,from left to right.

in order to save electricity, you should turn off all the lights, but you're lazy.
coincidentally,a passing bear children paper(bear children paper means the naughty boy), who want to pass here from the first light bulb to the last one and leave.

he starts from the first light and just can get to the adjacent one at one step.
But after all,the bear children paper is just a bear children paper. after leaving a light bulb to the next one, he must touch the switch, which will change the status of the light.

your task is answer whether it's possible or not to finishing turning off all the lights, and make bear children paper also reach the last light bulb and then leave at the same time.
 

Input
The first line of the input file contains an integer T, which indicates the number of test cases.

For each test case, there are 2 lines.

The first line of each test case contains 1 integers n.

In the following line contains a 01 sequence, 0 means off and 1 means on.

* 1T10
* 1N1000000
 

Output
There should be exactly T lines in the output file.

The i-th line should only contain "YES" or "NO" to answer if it's possible to finish.
 

Sample Input
  
  
1 5 1 0 0 0 0
 

Sample Output
  
  
YES
Hint
Child's path is: 123234545 all switchs are touched twice except the first one.
 

Source
 

好吧,依次把所有的数变成0,到最后一个如果是1就可以关闭所有的灯,否则就不可以

#include <iostream>
#include <cstdio>

using namespace std;

int a[1000000],n;

int dfs()
{
    for(int i = 0;i<n-1;i++)
    {
        if(!a[i])
        {
            a[i+1] = 1-a[i+1];
        }
    }
    return a[n-1];
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i = 0;i<n;i++)
            scanf("%d",&a[i]);
        if(dfs())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值