EDITED POST
But why is reserved unsuitable?
Code is faulty read below posts
Anyways edited code (faulty):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
struct piece {
char ID;
char top;
char left;
char bottom;
char right;
};
int main() {
int N; // number of puzzle pieces
cin >> N;
vector <piece> pieces(N); // pieces
vector < vector<piece> > pieces_pos(1); // matrix holding solution
pieces_pos[0].resize(1);
for (int i = 0; i < N; i++) { // takes input
cin >> pieces[i].ID;
cin >> pieces[i].top;
cin >> pieces[i].left;
cin >> pieces[i].bottom;
cin >> pieces[i].right;
if (pieces[i].top == 'x' && pieces[i].left == 'x')
pieces_pos[0][0] = pieces[i]; // top left part of solution matrix
}
for (int i = 0 ;; i++) { // matches puzzles linearly for first row
bool exists = false;
for (int j = 0; j < N; j++) { // check if any puzzle matches right part
if (tolower(pieces_pos[0][i].right) == tolower(pieces[j].left)) {
pieces_pos[0].resize(i + 3);
pieces_pos[0][i] = pieces[j]; // later realized it should be [i+1]
exists = true;
}
}
if (!exists)
break; // no puzzle match the right part so break
// match was found, check if another puzzle matches this new puzzle's right part
}
system("pause");
return 0;
}
| |
Ganado here's the explanation:
"Piece" is a structure holding all information related to given jigsaw puzzle.
After taking 'N' - number of jigsaw puzzles, we define vector pieces which will hold all the jigsaw
puzzles and we input to it.
Now we define another vector pieces_pos - this will be our finaly output matrix.
Example input for program:
4
1 x x a b
2 x B c x
3 A x x d
4 C D x x |
Output:
The output need not be a square matrix.
Now about the for loop.
for(int i = 0 ;; i++)
would have been
for(;;)
, we just
are using the for-loop to keep looping until we manually break out of it.
We declared i because we can use that.
The outer for-loop has no purpose it just loops and keeps track of number of iteration (thinking of it like this helped me, but actually it has a very important role than just iterating, by keeping track of i).
But also, now we are using i in
if (tolower(pieces_pos[0][i].right) == tolower(pieces[j].left)) {
so now we can continue comparing the right most element instead of just the top left element.
The inner for-loop compares the element in question which is the right most element of the first row's right jigsaw part with every single jigsaw's left part.
If a match is found, we add that to the matrix by making IT the rightmost part. And we continue looping to find a jigsaw puzzle who's left part may match with this new puzzke's right part.
When no match is found, we stop for this row. Because we have assumed that the solution matrix is a rectangle. So there cannot be missing jigsaw elements.
Now after doing this we would have solved the first row. Now we find the jigsaw which comes below the topleft most jigsaw puzzle and do the same as above solve for the second row.
This is repeated until the matrix has as many elements as the given no. of jigsaw pieces.
This is under assumption that only two parts form a match that means not more than two puzzles have the same name. So only two puzzles can match. I.e there can only be two puzzles with 'a' as jigsaw part.
What do you think about this? Do you think there is a better way?