More Related Content More from zahraa F.Muhsen (20) Puzzle the neighbor2. Puzzle problem
• Sahil has got a challenge problem from his
friend Akhil. Sahil has to find all values of n for
which one can fill an n × n table with +’s and -
’s (one per cell) so that every cell
has exactly one neighbor with the opposite
sign. Two cells are considered neighbors if
they are either in the same row or the same
column. Can you help out Sahil to find such
values of n ?
3. Solution
• We will scan row by row and find the value of
each cell using three rules 2 neighbors, 3
neighbors , 4 neighbors.
4. Rule for 2 neighbors
A B
C
A/1
B/1 C/0
If A=1 and B=1 and C=0
5. First step
1 1 ? ? ? ?
0 ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
2 neighbors rule
A=1 then B=1,C=0
6. Rule of 3 neighbors
B A D
C
A
B C D
If A=1 then B=1, D=1 C=0
Or A=1 and B=1 then C=0 , D=1
7. Second step
1 1 1 ? ? ?
0 0 ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
3 neighbors rule
A,B=1 then D=1, C=0
8. Rule of 4 neighbors
C
B A E
D
A
B C D E
If A=1 then one neighbor will be 0 other will be 1
Or If A=0 then one neighbor will be 1 other will be 0
9. 4 neighbors
1 1 1 ? ? ?
0 0 0 ? ? ?
? 0 ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
4 neighbors rule
A=0 , one neighbor will be
1 other will be 0
10. Third step
1 1 1 1 ? ?
0 0 0 ? ? ?
? 0 ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
3 neighbors rule
11. 4th step
1 1 1 1 1 ?
0 0 0 0 ? ?
? 0 ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
3 neighbors rule
12. 5th step
1 1 1 1 1 1
0 0 0 0 0 ?
? 0 ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
3 neighbors rule
13. 6th step
1 1 1 1 1 1
0 0 0 0 0 0
? 0 ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
2 neighbors rule
14. 7th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
3neighbors rule
15. 8th step
1 1 1 1 1 1
0 0 0 0 0 ?
0 0 ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
4neighbors rule
16. 9th step
1 1 1 1 1 1
0 0 0 0 0 ?
0 0 0 ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
4neighbors rule
17. 10th step
1 1 1 1 1 1
0 0 0 0 0 ?
0 0 0 0 ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
4neighbors rule
18. 11th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
4neighbors rule
19. 12th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
3neighbors rule
20. 13th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
3neighbors rule
21. 14th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
4 neighbors rule
22. 15th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
4 neighbors rule
23. 16th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 ? ?
? ? ? ? ? ?
? ? ? ? ? ?
4neighbors rule
24. 17th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 ?
? ? ? ? ? ?
? ? ? ? ? ?
4neighbors rule
25. 18th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
? ? ? ? ? ?
? ? ? ? ? ?
3neighbors rule
26. 19th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 ? ? ? ? ?
? ? ? ? ? ?
3neighbors rule
27. 20th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 ? ? ? ?
? ? ? ? ? ?
4neighbors rule
28. 21th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 ? ? ?
? ? ? ? ? ?
4neighbors rule
29. 22th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 ? ?
? ? ? ? ? ?
4neighbors rule
30. 23th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 ?
? ? ? ? ? ?
4 neighbors rule
31. 24th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
? ? ? ? ? ?
3neighbors rule
32. 25th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
0 ? ? ? ? ?
3neighbors rule
33. 26th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
0 0 ? ? ? ?
4 neighbors rule
34. 27th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
0 0 0 ? ? ?
4 neighbors rule
35. 28th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
0 0 0 0 ? ?
4neighbors rule
36. 29th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
0 0 0 0 0 ?
4neighbors rule
37. 30th step
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
0 0 0 0 0 0
3neighbors rule
38. End row by row scan
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
0 0 0 0 0 0