Google Code Jam problem

I'm practicing for the upcoming Google Code Jam but I'm stuck on one of the problems: http://code.google.com/codejam/contest/433101/dashboard#s=p0&a=0

I thought I had solved it before (I couldn't find any problems and my output for the example was correct) but I got the wrong answer for the small test. After making appropriate changes, I still can't get the right answer and now my program even gets the example wrong!

Can somebody help find what I've done wrong? I've commented the code to try and make it clearer what everything does:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
using namespace std;

int main()
{
	unsigned short int t, n;	
	bool snapper[31], powered[31];
	bool output[10001];
	unsigned int k;
	
	
	cin >> t;	//number of test cases
		for(int i=1; i <= t; i++){ //loops for each case
			
			snapper[0] = 1;	//set the socket to powered and the ON state to use as a snapper 
			powered[0] = 1;
			
			for(int a=1; a <= 30; a++)	//initialise each array to 0
					snapper[a] = 0;			
			
			for(int a=1; a <= 30; a++)		
					powered[a] = 0;
			
			
			cin >> n >> k;	//get input for number of snappers and number of snaps
			
			for(int snaps=1; snaps <= k; snaps++){	//loops for each snap
				
				for(int a = 1; a <= n; a++){	//sets each snapper to powered or unpowered
					
					if(powered[a-1] && snapper[a-1])	//if previous one is powered and sending power (ie. ON), it is powered
						powered[a] = 1;
						
					else
						powered[a] = 0;
				
				}
				
				for(int a = n; a >= 1; a--){	//sets each snapper to ON or OFF state
					
					if(powered[a]){	//if it is powered (recieving power) it changes ON/OFF state
						
						if(snapper[a])
							snapper[a] = 0;
						
						else
							snapper[a] = 1;	
							
					}
					
				}
				
			}
			
			if(powered[n] && snapper[n])	//after all snaps, if specified snapper is receiving and sending power the light is on
				output[i] = 1;	//output is stored
				
			else
				output[i] = 0;
			
			
		}
	
	
	for(int i=1; i <= t; i++){
			
			cout << "Case #" << i << ": ";
			
			if(output[i])		//output for each case displayed
				cout << "ON" << endl;
			
			else
				cout << "OFF" << endl;
			
		}
	
	return 0;	
}
Topic archived. No new replies allowed.