Programming Contest: Led
Description
You have an led alarm clock that may have different bar-shaped LED lights burnt out. You are to right a program to figure out what the time is, even if there are some non-functioning LEDs. See the description for visual aids.
[ complete description ] [ led.java ]
Sample Input
7
0000000 0000000 1000001 1000001 0100000 0000001 0000000 0000000
0000000 0000001 0100000 0100100 0010000 1000000 0000000 0000001
0100000 0110000 0010100 0100111 0000000 0110100 0010100 0100111
0100000 1000001 1000001 1000001 0000000 0000000 0000000 0000000
0000000 0000000 0100101 1010001 0110000 1101101 0110000 0110000
0000000 0000000 1111001 1010001 0110000 1101101 1111001 0110000
0000000 0000000 0000000 0000000 0110000 1101101 1111001 1111111
Sample Output
12:11
10:55
8:08
1:11
12:11
12:31
12:38
Solution
class led
{
int nor[];
int bitz[];
String sitz[];
public boolean is_time(int i)
{ // i is 0 or 1
int h = get_digit(i*4)*1000+get_digit(i*4+1)*100;
int m = get_digit(i*4+2)*10+get_digit(i*4+3);
return (100<=h && h<=1200 && 0<=m && m<=59)? true : false;
}
public int the_time(int i)
{ // i is 0 or 1
int h = get_digit(i*4)*1000+get_digit(i*4+1)*100;
int m = get_digit(i*4+2)*10+get_digit(i*4+3);
return (100<=h && h<=1200 && 0<=m && m<=59)? h+m : -1;
}
public boolean both_valid()
{
return is_time(0) && is_time(1);
}
public int get_digit(int i)
{
switch(bitz[i])
{
case 0000000: return 0;
case 1111110: return 0;
case 110000: return 1;
case 1101101: return 2;
case 1111001: return 3;
case 110011: return 4;
case 1011011: return 5;
case 1011111: return 6;
case 1110000: return 7;
case 1111111: return 8;
case 1111011: return 9;
}
return -1;
}
public int calculate_nor()
{
int c=0;
for(int i=0; i<4; i++)
{ //word
for(int j=0; j<7; j++)
{ //char
if(sitz[i].charAt(j)=='0' && sitz[i+4].charAt(j)=='0')
{
nor[c++] = i*4+j;
}
}
}
return c;
}
public boolean combinate(int elements[], int s, int e)
{ //true if done
if(both_valid())
{
int final_time = the_time(1);
System.out.println(((int)final_time/100)+":"+String.format("%02d",final_time%100));
return true;
}
for(int i=s; i<=e; i++)
{
if(combinate(elements,,))
{
// stop
return true;
}
else
{
// undo and continue
return false;
}
}
}
public void go() throws Exception
{
Scanner inf = new Scanner(new File("led.in"));
int c,i,j,n = inf.nextInt();
for(i=0; i<n; i++)
{
nor = new boolean[28];
bitz = new int[8];
sitz = new String[8];
for(j=0; j<8; j++)
{
bitz[j]=inf.nextInt();
sitz[j]=String.format("%07d",bitz[j]);
System.out.println(sitz[j]+" "+get_digit(j));
}
c=calculate_nor();
combinate(nor,0,c);
}
}
public static void main(String[] args) throws Exception
{
(new led()).go();
}
}