By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

please tell the code of tht puzzle using arrays n strings

P: 1
Problem statement:
You might have come across a puzzle which contains 15 numbered square pieces, which can be moved horizontally or vertically. A possible arrangement of these pieces is shown below:

11 43 15 76
87 120 12 1
66 22 43 4
21 89 93

As you can see, there is a blank at the bottom right corner. Implement the following program:
Numbers should be displayed in the above fashion. Allow the user to hit any of the arrow keys (up, down, left or right). If user hits up key, then the pieces with number 4 and blank piece will be swapped, and this table will look like

11 43 15 76
87 120 12 1
66 22 43
21 89 93 4

After that if user hits left key, then the piece with number 93 and blank piece will be swapped and table will look like:

11 43 15 76
87 120 12 1
66 22 43
21 89 93 4

If user hits left key and there is no column on left, then it should give appropriate message, which shows that it can’t move this side. With every hit, it will check whether numbers are sorted in ascending order or not. When all 15 integers are sorted and blank is at last position, then it should stop by giving success message, and this table will look like:

1 4 11 12
15 21 22 43
43 66 76 87
89 93 120
To keep track of keys, use getch ( ) and identify each key by its codes as follows:

• Up arrow key: 72
• Down arrow key: 80
• Right arrow key: 75
• Left arrow key: 77

It should also show number of moves at each step. It may also be stopped by press ‘q’ somewhere to quit the program.
Jun 4 '07 #1
Share this Question
Share on Google+
3 Replies


100+
P: 208
Please don't post questions right out of a text book.

Start this on you're own and then come here for help once you have a question about coding.
Jun 4 '07 #2

ilikepython
Expert 100+
P: 844
Problem statement:
You might have come across a puzzle which contains 15 numbered square pieces, which can be moved horizontally or vertically. A possible arrangement of these pieces is shown below:

11 43 15 76
87 120 12 1
66 22 43 4
21 89 93

As you can see, there is a blank at the bottom right corner. Implement the following program:
Numbers should be displayed in the above fashion. Allow the user to hit any of the arrow keys (up, down, left or right). If user hits up key, then the pieces with number 4 and blank piece will be swapped, and this table will look like

11 43 15 76
87 120 12 1
66 22 43
21 89 93 4

After that if user hits left key, then the piece with number 93 and blank piece will be swapped and table will look like:

11 43 15 76
87 120 12 1
66 22 43
21 89 93 4

If user hits left key and there is no column on left, then it should give appropriate message, which shows that it can’t move this side. With every hit, it will check whether numbers are sorted in ascending order or not. When all 15 integers are sorted and blank is at last position, then it should stop by giving success message, and this table will look like:

1 4 11 12
15 21 22 43
43 66 76 87
89 93 120
To keep track of keys, use getch ( ) and identify each key by its codes as follows:

• Up arrow key: 72
• Down arrow key: 80
• Right arrow key: 75
• Left arrow key: 77

It should also show number of moves at each step. It may also be stopped by press ‘q’ somewhere to quit the program.
We don't "tell" the code to someone. We help them with their problems. Post a clear and specific question and we will help you.
Jun 4 '07 #3

ilikepython
Expert 100+
P: 844
Please don't post questions right out of a text book.

Start this on you're own and then come here for help once you have a question about coding.
Ah you beat me to it...
Jun 4 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.