没什么特别的方法,c的大小不会影响到效率,最多仅有8种结果,我写了一个configuration类,每个configuration就是一种可能的结果
Code
/**//*
ID: sdjllyh1
PROG: lamps
LANG: JAVA
complete date:
complexity:
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class lamps
{
private static int n, c;
private static int[] mustBeOn;
private static int[] mustBeOff;
private static ArrayList configurations = new ArrayList();
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
configuration conf;
if (c==0)
{
conf = new configuration(n);
if (isConsistent(conf))
{
configurations.add(conf);
}
}
else if(c==1)
{
conf = new configuration(n);
conf.pressButton1();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton2();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton3();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton4();
if (isConsistent(conf))
{
configurations.add(conf);
}
}
else
{
conf= new configuration(n);
conf.pressButton2();
conf.pressButton3();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton1();
conf.pressButton3();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton1();
conf.pressButton2();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton1();
conf.pressButton4();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton2();
conf.pressButton4();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton3();
conf.pressButton4();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf = new configuration(n);
conf.pressButton4();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf = new configuration(n);
if (isConsistent(conf))
{
configurations.add(conf);
}
}
Collections.sort(configurations);
deleteRepetition();
}
private static boolean isConsistent(configuration value)
{
for (int i = 0; i < mustBeOn.length; i++)
{
if (!value.isOn[mustBeOn[i]])
{
return false;
}
}
for (int i = 0; i < mustBeOff.length; i++)
{
if (value.isOn[mustBeOff[i]])
{
return false;
}
}
return true;
}
// call must be after sort
private static void deleteRepetition()
{
int i=0;
while (i+1<configurations.size())
{
configuration cLeft=(configuration)configurations.get(i);
configuration cRight=(configuration)configurations.get(i+1);
if (cLeft.compareTo(cRight) == 0)
{
configurations.remove(i + 1);
}
else
{
i++;
}
}
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("lamps.in"));
n = Integer.parseInt(f.readLine());
c = Integer.parseInt(f.readLine());
int[] tmpMustBeON = new int[n];
int[] tmpMustBeOff = new int[n];
int mustBeOnCounter = 0;
int mustBeOffCounter = 0;
StringTokenizer st = new StringTokenizer(f.readLine());
int readNumber = Integer.parseInt(st.nextToken());
while (readNumber != -1)
{
tmpMustBeON[mustBeOnCounter] = readNumber - 1;
mustBeOnCounter++;
readNumber = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(f.readLine());
readNumber = Integer.parseInt(st.nextToken());
while (readNumber != -1)
{
tmpMustBeOff[mustBeOffCounter] = readNumber - 1;
mustBeOffCounter++;
readNumber = Integer.parseInt(st.nextToken());
}
f.close();
mustBeOn = new int[mustBeOnCounter];
mustBeOff = new int[mustBeOffCounter];
System.arraycopy(tmpMustBeON, 0, mustBeOn, 0, mustBeOnCounter);
System.arraycopy(tmpMustBeOff, 0, mustBeOff, 0, mustBeOffCounter);
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("lamps.out")));
Iterator it = configurations.iterator();
if (it.hasNext())
{
while (it.hasNext())
{
out.println(it.next());
}
}
else
{
out.println("IMPOSSIBLE");
}
out.close();
}
}
class configuration implements Comparable
{
public boolean[] isOn;
public int length;
public configuration(int size)
{
this.length = size;
this.isOn = new boolean[length];
Arrays.fill(isOn, true);
}
public int compareTo(Object arg0)
{
configuration compareObject = (configuration)arg0;
for (int i = 0; i < this.length; i++)
{
if (this.isOn[i] && !compareObject.isOn[i])
{
return 1;
}
if (!this.isOn[i] && compareObject.isOn[i])
{
return -1;
}
}
return 0;
}
public void pressButton1()
{
for (int i = 0; i < this.length; i++)
{
isOn[i] = !isOn[i];
}
}
public void pressButton2()
{
int i = 0;
while (i < this.length)
{
isOn[i] = !isOn[i];
i += 2;
}
}
public void pressButton3()
{
int i = 1;
while (i < this.length)
{
isOn[i] = !isOn[i];
i += 2;
}
}
public void pressButton4()
{
int i = 0;
while (i < this.length)
{
isOn[i] = !isOn[i];
i += 3;
}
}
public String toString()
{
StringBuffer retStr = new StringBuffer();
for (int i = 0; i < this.length; i++)
{
if (isOn[i])
{
retStr.append("1");
}
else
{
retStr.append("0");
}
}
return retStr.toString();
}
}
/**//*
ID: sdjllyh1
PROG: lamps
LANG: JAVA
complete date:
complexity:
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class lamps
{
private static int n, c;
private static int[] mustBeOn;
private static int[] mustBeOff;
private static ArrayList configurations = new ArrayList();
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
configuration conf;
if (c==0)
{
conf = new configuration(n);
if (isConsistent(conf))
{
configurations.add(conf);
}
}
else if(c==1)
{
conf = new configuration(n);
conf.pressButton1();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton2();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton3();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton4();
if (isConsistent(conf))
{
configurations.add(conf);
}
}
else
{
conf= new configuration(n);
conf.pressButton2();
conf.pressButton3();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton1();
conf.pressButton3();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton1();
conf.pressButton2();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton1();
conf.pressButton4();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton2();
conf.pressButton4();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf= new configuration(n);
conf.pressButton3();
conf.pressButton4();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf = new configuration(n);
conf.pressButton4();
if (isConsistent(conf))
{
configurations.add(conf);
}
conf = new configuration(n);
if (isConsistent(conf))
{
configurations.add(conf);
}
}
Collections.sort(configurations);
deleteRepetition();
}
private static boolean isConsistent(configuration value)
{
for (int i = 0; i < mustBeOn.length; i++)
{
if (!value.isOn[mustBeOn[i]])
{
return false;
}
}
for (int i = 0; i < mustBeOff.length; i++)
{
if (value.isOn[mustBeOff[i]])
{
return false;
}
}
return true;
}
// call must be after sort
private static void deleteRepetition()
{
int i=0;
while (i+1<configurations.size())
{
configuration cLeft=(configuration)configurations.get(i);
configuration cRight=(configuration)configurations.get(i+1);
if (cLeft.compareTo(cRight) == 0)
{
configurations.remove(i + 1);
}
else
{
i++;
}
}
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("lamps.in"));
n = Integer.parseInt(f.readLine());
c = Integer.parseInt(f.readLine());
int[] tmpMustBeON = new int[n];
int[] tmpMustBeOff = new int[n];
int mustBeOnCounter = 0;
int mustBeOffCounter = 0;
StringTokenizer st = new StringTokenizer(f.readLine());
int readNumber = Integer.parseInt(st.nextToken());
while (readNumber != -1)
{
tmpMustBeON[mustBeOnCounter] = readNumber - 1;
mustBeOnCounter++;
readNumber = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(f.readLine());
readNumber = Integer.parseInt(st.nextToken());
while (readNumber != -1)
{
tmpMustBeOff[mustBeOffCounter] = readNumber - 1;
mustBeOffCounter++;
readNumber = Integer.parseInt(st.nextToken());
}
f.close();
mustBeOn = new int[mustBeOnCounter];
mustBeOff = new int[mustBeOffCounter];
System.arraycopy(tmpMustBeON, 0, mustBeOn, 0, mustBeOnCounter);
System.arraycopy(tmpMustBeOff, 0, mustBeOff, 0, mustBeOffCounter);
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("lamps.out")));
Iterator it = configurations.iterator();
if (it.hasNext())
{
while (it.hasNext())
{
out.println(it.next());
}
}
else
{
out.println("IMPOSSIBLE");
}
out.close();
}
}
class configuration implements Comparable
{
public boolean[] isOn;
public int length;
public configuration(int size)
{
this.length = size;
this.isOn = new boolean[length];
Arrays.fill(isOn, true);
}
public int compareTo(Object arg0)
{
configuration compareObject = (configuration)arg0;
for (int i = 0; i < this.length; i++)
{
if (this.isOn[i] && !compareObject.isOn[i])
{
return 1;
}
if (!this.isOn[i] && compareObject.isOn[i])
{
return -1;
}
}
return 0;
}
public void pressButton1()
{
for (int i = 0; i < this.length; i++)
{
isOn[i] = !isOn[i];
}
}
public void pressButton2()
{
int i = 0;
while (i < this.length)
{
isOn[i] = !isOn[i];
i += 2;
}
}
public void pressButton3()
{
int i = 1;
while (i < this.length)
{
isOn[i] = !isOn[i];
i += 2;
}
}
public void pressButton4()
{
int i = 0;
while (i < this.length)
{
isOn[i] = !isOn[i];
i += 3;
}
}
public String toString()
{
StringBuffer retStr = new StringBuffer();
for (int i = 0; i < this.length; i++)
{
if (isOn[i])
{
retStr.append("1");
}
else
{
retStr.append("0");
}
}
return retStr.toString();
}
}