A square picture is cut into 16 squares and then shuffled. Write a program to rearrange the 16 squares to get the original big square.
uppose the original picture is 16 pieces and the individual pieces are numbered as follows.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The problem is then, given any one of these numbered pieces, how do you place each piece in the
correct position in a two dimensional array.
Let the string str contain the shuffled pieces. Then -
int **restore(char const *str)
{
int x,y,i;
int final[4][4];
for (i = 0, i < 16, i++) {
if !(str[i]%4) {
x = (int)(str[i]/4) - 1;
y = 3;
}
else {
x = (int)(str[i]/4);
y = (int)(str[i]%4) - 1;
}
final[x][y] = str[i];
}
return final;
}
Check:
say str[i] = 5
x = 1
y = 0
So final[1][0] = 5 which is the correct position in the actual picture
say str[i] = 12
x = 2
y = 3
So final[2][3] = 12
and so on.
(Return to questio
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The problem is then, given any one of these numbered pieces, how do you place each piece in the
correct position in a two dimensional array.
Let the string str contain the shuffled pieces. Then -
int **restore(char const *str)
{
int x,y,i;
int final[4][4];
for (i = 0, i < 16, i++) {
if !(str[i]%4) {
x = (int)(str[i]/4) - 1;
y = 3;
}
else {
x = (int)(str[i]/4);
y = (int)(str[i]%4) - 1;
}
final[x][y] = str[i];
}
return final;
}
Check:
say str[i] = 5
x = 1
y = 0
So final[1][0] = 5 which is the correct position in the actual picture
say str[i] = 12
x = 2
y = 3
So final[2][3] = 12
and so on.
(Return to questio
Comments
Post a Comment
https://gengwg.blogspot.com/