Reply
Sat 11 Mar, 2006 02:27 am
Consider an n by n board. Each square has a pair of co-ordinates ( x,y ) where 1<= x, y <= n. Each square has two states : either "ON" or "OFF". Initially, all the squares are OFF. For a square (i.j), its neighbors are the squares: { (i' , j' ) | 1<= i' , j' <=n; |i-i'| , |j-j'| <=1} . The neighbors of (4,2) in case of n=4 are {(4,1),(4,2),(3,2),(4,3)} . Note that (i, j) is a neighbor of itself. Define the operation toggle (i,j) that toggles the state of square (i ,j)'s neighbors. i.e. if the state is "ON" then it is turned "OFF" and vice versa. Write a program that, given a n by n board off all OFF squares, computes the shortest sequence of toggle operations that turns all the squares ON. If no sequences can turn all the squares "ON", output "No Solution". On addition to that, the solution should contain step by step change of state (shown graphically).
Example Input
2 3
Example Output
(1,1) (1,2) (2,2) (2,1)
(2,2) (1,3) (3,3) (3,1) ( 1,1)
if you have to toggle ( 2,1) then we toggle its neighbors ( 2,2) (2,1) (1,1) (1,3) . The diagonal elements are not considered.
tell me yhe logic for doing this or full solution
Order doesn't matter, parity does. You need to fill the grid with 0s and (the least number of) 1s such that for each square, the sum of its neighbors is odd. A 1 represents a toggle operation.
possibly non-optimal solutions:
1x1
1
2x2
11
11
3x3
101
010
101
4x4
1001
0000
0110
0110
My program gives:
n, toggles
2, 4
3, 5
4, 4
5, 15
6, 28
7, 33
8, 40
9, 25
10, 44
11, 55
12, 72
13, 105
14, 56
15, 117
16, 104
17, 147
18, 188
19, 141
20, 224
Here's 20x20:
1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0 1 0 1
0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0
1 0 1 1 0 1 1 0 0 0 0 0 0 1 1 0 1 1 0 1
0 0 1 1 1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 0
0 0 0 1 1 0 1 1 1 0 0 1 1 1 0 1 1 0 0 0
1 1 1 0 0 1 0 0 0 1 1 0 0 0 1 0 0 1 1 1
1 0 1 1 1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 1
1 0 0 0 1 0 0 1 1 1 1 1 1 0 0 1 0 0 0 1
1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 1 1 0 0 1
1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1
1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1
1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 1 1 0 0 1
1 0 0 0 1 0 0 1 1 1 1 1 1 0 0 1 0 0 0 1
1 0 1 1 1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 1
1 1 1 0 0 1 0 0 0 1 1 0 0 0 1 0 0 1 1 1
0 0 0 1 1 0 1 1 1 0 0 1 1 1 0 1 1 0 0 0
0 0 1 1 1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 0
1 0 1 1 0 1 1 0 0 0 0 0 0 1 1 0 1 1 0 1
0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0
1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0 1 0 1
markr
could u please tell me the logic and program for this.please. is it possible for all n to turn squares on
give your program.paste it here.i want to see
I've given you a big enough hint already. You should be able to code that.
markr
i request u to please give me the code for this problem if u have.i need it urgently.also tell me the logic u have used.
i will be thankful to u.
u can mail me at
[email protected]
mail me as well as paste it here so that everybody can see.
i am waiting for it.please
hurry up
hi markr
i need it urgently.please give it to me the code and tell me the logic.
pleaseeeeee
hurry up
bye
hi markr
for a 4*4 square u have shown no of 1 to be 6.but u given ans 4.how is it .please reply.give me code for this .Tell me the logic how to approach and which square to go .or give me the coding please
Let's see your attempt (code) to solve it.
request
well markr
u di not tell me the logic.i have to submit the code before 21 march for a event.
please give me the code.it will really help me.after all forum is made for helping each other.please do it early and reply me
i am asking u as a friend,please
You seem really desperate. But I agree with Mark, why take part of a programming event (or is it homework?) if you can't show a bit of work yourself.
Whim
I will show you, anurag1408.
But it will cost you - one million dollars.
e-mail me at
[email protected]
I've already provided you with what I think is the key to solving the problem. At this point, it's a coding exercise. I've also given you data to test your algorithm (assuming I've coded it correctly). I'm not going to post my solution.
please
i need it.mail me at
[email protected] if u dont want to paste it here