http://poj.org/problem?id=1176

Party Lamps
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4023   Accepted: 1386

Description

To brighten up the gala dinner of the IOI'98 we have a set of N coloured lamps numbered from 
1 to N. The lamps are connected to four buttons: 
button 1 -- when this button is pressed, all the lamps change their state: those that are ON are turned OFF and those that are OFF are turned ON. 
button 2 -- changes the state of all the odd numbered lamps. 
button 3 -- changes the state of all the even numbered lamps. 
button 4 -- changes the state of the lamps whose number is of the form 3K+1 (with K >= 0), i.e., 1,4,7,... 
There is a counter C which records the total number of button presses. 
When the party starts, all the lamps are ON and the counter C is set to zero.

You are given the value of counter C and information on the final state of some of the lamps. Write a program to determine all the possible final configurations of the N lamps that are consistent with the given information, without repetitions.

Input

Your program is to read from standard input. The input contains four lines, describing the number N of lamps available, the number C of button presses, and the state of some of the lamps in the final configuration. 
The first line contains the number N and the second line the final value of counter C. The third line lists the lamp numbers you are informed to be ON in the final configuration, separated by one space and terminated by the integer -1. The fourth line lists the lamp numbers you are informed to be OFF in the final configuration, separated by one space and terminated by the integer -1.

The parameters N and C are constrained by: 
10 <= N <= 100 
1 <= C <= 10000 
The number of lamps you are informed to be ON, in the final configuration, is less than or equal to 2.The number of lamps you are informed to be OFF, in the final configuration, is less than or equal to 2.

Output

Your program is to write to standard output. The output must contain all the possible final configurations (without repetitions) of all the lamps. There is at least one possible final configuration. Each possible configuration must be written on a different line. Each line has N characters, where the first character represents the state of lamp 1 and the last character represents the state of lamp N. A 0 (zero) stands for a lamp that is OFF, and a 1 (one) stands for a lamp that is ON. Configurations should be listed in binary ascending order.

Sample Input

10
1
-1
7 -1

Sample Output

0000000000
0101010101
0110110110

分析:

题意:对于一串彩灯,提供四种改变彩灯状态(ON<=>OFF)的操作:a.改变所有彩灯状态;b.改变奇数彩灯状态;c.改变偶数彩灯状态;d.改变3k+1号彩灯状态(1,4,7,10...)。

给定彩灯数目,操作次数,和对于某几个彩灯必须为ON、某几个彩灯必须为OFF的要求,问经过给定次数的操作,最终能达到的满足要求的状态有多少种,输出所有满足要求的彩灯状态。

原题中操作次数是1<=C<=10000的,如果以此为搜索深度,时间复杂度相当可观。

转换思路:

  当按第一种操作时 :奇偶全变

  当按第二种操作时 :奇数全变

  当按第三种操作时 :偶数全变

  当按第四种操作时 :3K + 1 全变(1 , 4 ,7 , 10 , 13 , 16 , 。。。97 ,100)。

四种情况的最小公倍数为 8 ,即是周期为 8 ,打表可得:

string s[8]={
  "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010",
  "0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
  "0110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110",
  "1001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001",
  "1100011100011100011100011100011100011100011100011100011100011100011100011100011100011100011100011101",
  "0011100011100011100011100011100011100011100011100011100011100011100011100011100011100011100011100011", 
  "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
 };

    1,当只按键一次出现的状态有:1,2,3,4

2,当按键二次则出现的状态有:1,2,3,5,6,7,8

3,当按键三次或三次以上全部状态可以出现。

AC代码:
 #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int N,C;
int v1[],v2[];
int cmp(const void *a,const void *b)
{
if(*(string*)a>*(string*)b)
return ;
else return ;
} int main()
{
//freopen("input.txt","r",stdin);
string result[];
int temp,i,i1(),i2(),j,id();
string s[]={
"",
"",
"",
"",
"",
"",
"",
""
}; cin>>N>>C;
cin>>temp;
while(temp!=-)
{
v1[i1++]=temp-;
cin>>temp;
}
cin>>temp;
while(temp!=-)
{
v2[i2++]=temp-;
cin>>temp;
}
if(C==)
{
for(i=;i<N;i++)
cout<<s[][i];
cout<<endl;
}//c=0
else if(C>=)
{
for(i=;i<;i++)
{
if(i==)
{
if(C==) continue;
}
if(i== || i== || i== || i==)
{
if(C<) continue;
}
for(j=;j<i1;j++)
{
temp=v1[j];
if(s[i][temp]!='') break;
}
if(j==i1)
{
for(j=;j<i2;j++)
{
temp=v2[j];
if(s[i][temp]!='') break;
}
if(j==i2)
{
result[id]=s[i];
id++;
}
}
}//for
qsort(result,id,sizeof(string),cmp);
for(i=;i<id;i++)
{
for(j=;j<N;j++)
cout<<result[i][j];
cout<<endl;
}
}//if return ; }

poj 1176 Party Lamps的更多相关文章

  1. POJ 1176 Party Lamps&& USACO 2.2 派对灯(搜索)

    题目地址 http://poj.org/problem?id=1176 题目描述 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码. 这些灯都 ...

  2. POJ 1176 Party Lamps (DFS)

    对于一束灯光.提供四种改变彩灯状态(ON<=>OFF)的操作:a.改变全部彩灯状态:b.改变奇数彩灯状态.c.改变偶数彩灯状态:d.改变3k+1号彩灯状态(1,4,7,10...). 给定 ...

  3. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  4. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  5. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  6. poj 题目分类(1)

    poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...

  7. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

  8. POJ题目分类(转)

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  9. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

随机推荐

  1. P1018 乘积最大

    开始定义状态f[i][j][k]为[i,j)区间插入k个括号,使用记忆化搜索,但是成功爆栈,得到4个mle #include <bits/stdc++.h> using namespace ...

  2. SQL Server游标【转】

      什么是游标 结果集,结果集就是select查询之后返回的所有行数据的集合. 游标则是处理结果集的一种机制吧,它可以定位到结果集中的某一行,多数据进行读写,也可以移动游标定位到你所需要的行中进行操作 ...

  3. Bootstrap 进度条媒体对象和条组

    列表组组件 列表组组件用于显示一组列表的组件. //基本实例 <ul class="list-group"> <li class="list-group ...

  4. 【Android开发学习笔记】【第一课】初识New Project,工程文件介绍

    初学者新建一个Andriod工程后,往往不知道Pakage Explorer区域的每个文件是什么作用,今天学习了一下,自我总结一下. 1.先新建一个工程 2.输入名称,以及支持的SDK版本等(这些可以 ...

  5. JavaScipt选取文档元素的方法

    摘自JavaScript权威指南(jQuery根据样式选择器查找元素的终极方式是 先用getElementsByTagName(*)获取所有DOM元素,然后根据样式选择器对所有DOM元素进行筛选) 选 ...

  6. 毕老师的Editplus

    简介 EditPlus是一款由韩国 Sangil Kim (ES-Computing)出品的小巧但是功能强大的可处理文本.HTML和程序语言的Windows编辑器,你甚至可以通过设置用户工具将其作为C ...

  7. Dynamics AX Bitmap to Image File

    static void SSW_Bit2ImageFile(Args _args) { Bitmap curBitmap; Image curImage; ; curBitmap = FormLett ...

  8. LightOj1366 - Pair of Touching Circles(求矩形内圆的对数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1366 题意:一个H*W的矩形,现在要放入两个外切的圆,问能放多少对这样的圆,其中圆心和 ...

  9. linq世界走一走(LINQ TO SQL)

    前言:作为linq的一个组件,同时作为ADO.NET的一个组成部分,LINQ TO SQL提供了将关系数据映射为对象的运行时基础结构. LINQ TO SQL是通过将关系数据库对象的数据模型(如一个数 ...

  10. imx6 android5.1 打开 调试串口

    imx6的工板烧录android 5.1的镜像,uboot中能使用debug口,kernel,文件系统中不能使用debug口. 打开kenel和文件系统debug口方法,在uboot的bootargs ...