Enumeration lights out problem java implementation

Enumeration lights out problem java implementation

import java.util.Scanner;
// lights out problem
public class S4 {
    static char[] orLights=new char[5]; //Each element corresponds to a row of the light matrix  
    static char[] light=new char[5]; //Matrix of changing lights  
    static char[] result=new char[5];//result switch matrix
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
			for(int i=0;i<5;++i) {
				for(int j=0;j<6;++j) {
					int s=sc.nextInt();
					orLights[i]=setBit(orLights[i],j,s);
				}
			}
			for(int n=0;n<64;n++) {
				int switchs=n;
				 System.arraycopy(orLights, 0, light, 0, 5);
				for(int i=0;i<5;i++) {
					result[i]=(char) switchs;
					for(int j=0;j<6;j++) {
						if(getBit((char)switchs,j)==1) {
							if(j>0)
								light[i]=Flip(light[i],j-1);
							light[i]=Flip(light[i],j);
							if(i<5)
								light[i]=Flip(light[i],j+1);
						}
					}
					if(i<4)
						light[i+1]^=switchs;
					switchs=light[i];
					
				}
				 if(light[4]==0) { //The last line is equal to 0, which means all are off  
					 outputResult(result);  
		                break;  
				
			}
			
			}
		}
	
	static int getBit(char c,int i) {
		// Take the i-th bit of C
		return (c>>i)&1;
	}
	static char setBit(char c,int i,int v) {
		//Set the i-th bit of c to v
		if(v!=0)
			c|=(1<<i);
		else
			c&=~(1<<i);
		return c;
	}
	static char Flip(char c,int i) {
		//Invert the i-th bit of c
		c^=(1<<i);
		return c;
	}
	static void outputResult(char result[]) {
		System.out.println("PUZZLE#");
		for(int i=0;i<5;i++) {
			for(int j=0;j<6;++j) {
				System.out.print(getBit(result[i],j));
				if(i<5)
					System.out.print(" "); //There are spaces between each number  
			}
			System.out.println();
		}
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324657734&siteId=291194637