Arrays Safe
Arrays Safe
House House House House House House House House House House
#1 #2 #3 #4 #5 #6 #7 #8 #9 #10
Data types
• An atomic data type
• .e.g. Array
Arrays
• Array
– a collection of a fixed number of components
wherein all of the components have the same
behavior, so obvious the same data type
• The general form :
dataType arrayName[intExp];
where intExp is any expression that evaluates
to a positive integer
Declaring an array
• The statement
65510 10 num_arr[0]
65510 10 num_arr[0]
65510 10 num_arr[0]
65510 10 num_arr[0]
0 1 2 3 4 5
1 11 12 13 14 15
2 21 22 23 24 25
3 31 32 33 34 35
4 41 42 43 44 45
5 51 52 53 54 55
Q
Left Diagonal
0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7
IS SAFE
1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7
Row Col
4 4
2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7
To Be checked
Row Col 4,0 4,1 4,2 4,3 4,4 4,5 4,6 4,7
3 3 Queen
1 1
0 0 6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7
-1 -1
7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7
UP
0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7
IS SAFE
1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7
Row Col
4 4
2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7
To Be checked
Row Col 4,0 4,1 4,2 4,3 4,4 4,5 4,6 4,7
3 4 Queen
1 4
0 4 6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7
-1 4
7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7
Right Diagonal
0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7
IS SAFE
1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7
Row Col
4 4
2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7
To Be checked
Row Col 4,0 4,1 4,2 4,3 4,4 4,5 4,6 4,7
3 5 Queen
1 7
0 8 6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7
-
7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 6
4 -1
5 -1
6
-1
7
-1
Backtrack
• The next column to check for availability is the
last placed column + 1
• Update the array by placing -1
• Remove the QUEEN from the screen
• BACKTRACKING MIGHT REQUIRE AN
IMMEDIATE BACKTRACKING, SO PLACE THE
BACKTRACKING CODE TWICE….NOT MORE
THAN TWO TIMES BACKTRACKING CAN HAPPEN
Save the Solution