编程介的小学生 2019-04-02 19:04 采纳率: 20.5%
浏览 501

二进制开关的状态的计算问题,如何利用C程序的设计的思想的办法来解决的

Problem Description
There are N lamps in a line and M switches control them. Each switch is connected to a subset of these lamps. When a switch is flipped, all the lamps connected to it change its state (on to off and off to on).

Input
The input contains multiple test cases. Each test case starts with two integers N and M (0<N<=50, 0<M<=100), which stand for the number of lamps and swithes. Then M lines follows, each line contains N characters. The jth character of the ith line is '1' if the ith switch is connected to the jth lamp, and '0' otherwise. The next line is a integer Q, which is the number of queries. Each of following Q lines contains the initial and target state of lamps ('0' is off and '1' is on).

Output
For each query, output "Yes" if it is possible to change the state from initial to target with these swithes. Otherwise, output "No".

Sample Input
5 3
00000
10101
01010
3
00000 11111
10101 01010
11111 10001

Sample Output
Yes
Yes
No

  • 写回答

1条回答

  • wudengyu 2019-04-03 21:14
    关注

    每盏灯的状态用一个bit就能表示,但是每组灯最大可能有50个,所以需要用long long。开关与灯的连接关系也是一样,每个开关也无非『开』与『关』两种状态, 假设初始状态为『关』开的话其实就是拿灯的状态与开关按位异或就行了。但是开关的个数最大可能有100个,long long的位数都不够,只能用数组表示,同时用一个 数组来记录每个开关的状态。遍历每个开关状态的时候类似二进制运算,从0开始,每试一种状态就加1,该进位进位,直到溢出为止。

    #include <stdio.h>
    long long bstr_to_int(char* p,int len){
        long long tmp=0;
        for(int i=0;i<len;i++){
            tmp<<=1;
            if(*p++!='0')
                tmp|=1;
        }
        return tmp;
    }
    int main(){
        int n,m,q;
        long long swithes[100];
        char str1[50],str2[50];
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++){
            scanf("%s",str1);
            swithes[i]=bstr_to_int(str1,n);
        }
        scanf("%d",&q);
        for(int i=0;i<q;i++){
            long long initial,target,halfway;
            int status[100];
            int prt=0;
            scanf("%s %s",str1,str2);
            initial=bstr_to_int(str1,n);
            target=bstr_to_int(str2,n);
            if(initial==target){
                printf("Yes\n");
               continue;
            }
            while(prt<m){
                status[prt]^=1;
                if(!status[prt]){
                    prt++;
                    continue;
                }
                halfway=initial;
                for(int j=0;j<m;j++){
                    if(status[j])
                        halfway^=swithes[j];
                }
                if(halfway==target){
                    printf("Yes\n");
                    break;
                }else{
                    prt=0;
                    continue;
                }
            }
            if(prt>=m){
                printf("No\n");
            }
    
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题