Advertisement
Guest User

Untitled

a guest
Sep 25th, 2010
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct piece{
  5.     int w, h;
  6.     char* data;
  7.  
  8.     //this way I can quickly create a piece form a string:
  9.     piece(int w, int h, const char* dat=0) : w(w), h(h){
  10.         data = new char[w*h];
  11.         if(dat) memcpy(data, dat, w*h);
  12.         else memset(data, ' ', w*h);
  13.     }
  14.  
  15.     //these two copy all the data so that it can be safely manipulated and deleted
  16.     piece(const piece& p) : w(p.w), h(p.h){
  17.         data = new char[w*h];
  18.         memcpy(data, p.data, w*h);
  19.     }
  20.     piece& operator = (const piece& p){
  21.         w = p.w; h = p.h;
  22.         data = new char[w*h];
  23.         memcpy(data, p.data, w*h);
  24.         return *this;
  25.     }
  26.     ~piece(){
  27.         delete[] data;
  28.     }
  29.  
  30.     //some output
  31.     void print(){
  32.         for(int y = 0; y < h; y++){
  33.             for(int x = 0; x < w; x++)
  34.                 std::cout.put( data[x+y*w] );
  35.             std::cout.put('\n');
  36.         }
  37.     }
  38.  
  39.     //rotate a piece by 90 degrees:
  40.     void turn(){
  41.         char* ndat = new char[w*h];
  42.  
  43.         for(int x = 0; x < w; x++)
  44.             for(int y = 0; y < h; y++)
  45.                 ndat[(h-1-y)+x*h] = data[x+y*w];
  46.  
  47.         delete[] data;
  48.         data = ndat;
  49.         std::swap(w, h);
  50.     }
  51.  
  52.     //this function tries to put one piece onto another
  53.     //if there is some overlapping, function returns 0, neither piece is changed
  54.     //else function returns 1, 'this' is changed.
  55.     bool blit(int px, int py, piece& p){
  56.         if(px < 0 || py < 0 || px+p.w > w || py+p.h > h) return 0;
  57.        
  58.         for(int x = 0; x < p.w; x++)
  59.             for(int y = 0; y < p.h; y++)
  60.                 if(p.data[x+y*p.w] != ' ' &&  data[px+x+(py+y)*w] != ' ') return 0;
  61.  
  62.         for(int x = 0; x < p.w; x++)
  63.             for(int y = 0; y < p.h; y++)
  64.                 if(p.data[x+y*p.w] != ' ') data[px+x+(py+y)*w] = p.data[x+y*p.w];
  65.        
  66.         return 1;
  67.     }
  68. };
  69.  
  70. //this is where the magic happens
  71. bool TryAll(std::vector<piece> list, piece res){
  72.     piece result = res;
  73.     for(int a = 0; a < 4; a++){//function tries every coordinate and angle of the top piece in the list
  74.         for(int x = 0; x < result.w; x++){
  75.             for(int y = 0; y < result.h; y++){
  76.                 if( result.blit( x, y, list.back() ) ){//if piece fits in the jigsaw..
  77.                     result.print();
  78.                     std::cout.put('\n');
  79.                     bool b = true;
  80.                     if(list.size() > 1)//if this wasn't the last piece,
  81.                         //recursively call TryAll with a shorter list and the new variant of jigsaw
  82.                         b = TryAll( std::vector<piece>(list.begin(), list.end()-1), result);
  83.                     if(b) return true;//else the jigsaw is solved
  84.                     else result = res;//if something fails, and the piece you tried is not in the right place,
  85.                             //you have to reset the jigsaw to its former state.
  86.                 }
  87.             }
  88.         }
  89.         list.back().turn();
  90.     }
  91.     return 0;//this point is only reached when no piece matches into the puzzle.
  92. }
  93.  
  94. int main(){
  95.     std::vector<piece> list;//here I construct the pieces. normally you would use a file for that.
  96.     list.push_back( piece(3, 1, "###") );
  97.     list.push_back( piece(3, 3, "OOOO  OOO") );
  98.     list.push_back( piece(2, 3, "XXX XX") );
  99.  
  100.     piece result(5, 3);//the jigsaw itself is also a piece.
  101.     //however this piece never holds any value. If you want to change that you could make TryAll return piece*
  102.  
  103.     TryAll(list, result);
  104.  
  105.     std::cin.get();
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement