Advertisement
steverobinson

Problem 3[Square Puzzle] NEW

Oct 25th, 2010
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int matrix[5][5];
  5. int empty_r,empty_c;
  6.  
  7. inline int move(char direction)
  8. {
  9.     if(direction=='A')
  10.     {   if(empty_r==0)
  11.         {
  12.             return 1;
  13.         }
  14.         else
  15.         {
  16.         matrix[empty_r][empty_c]=matrix[empty_r-1][empty_c];
  17.         empty_r--;
  18.         matrix[empty_r][empty_c]=0;
  19.         }
  20.     }
  21.  
  22.     if(direction=='B')
  23.     {
  24.         if(empty_r==4)
  25.         {
  26.             return 1;
  27.         }
  28.         else
  29.         {
  30.             matrix[empty_r][empty_c]=matrix[empty_r+1][empty_c];
  31.             empty_r++;
  32.             matrix[empty_r][empty_c]=0;
  33.         }
  34.  
  35.     }
  36.     if(direction=='L')
  37.     {
  38.         if(empty_c==0)
  39.             return 1;
  40.         else
  41.         {
  42.             matrix[empty_r][empty_c]=matrix[empty_r][empty_c-1];
  43.             empty_c--;
  44.             matrix[empty_r][empty_c]=0;
  45.         }
  46.     }
  47.     if(direction=='R')
  48.     {
  49.         if(empty_c==4)
  50.             return 1;
  51.         else
  52.         {
  53.             matrix[empty_r][empty_c]=matrix[empty_r][empty_c+1];
  54.             empty_c++;
  55.             matrix[empty_r][empty_c]=0;
  56.         }
  57.  
  58.     }
  59.     return 0;
  60.  
  61. }
  62.  
  63. int main()
  64. {
  65.     bool flag;
  66.     char dirn[100]="ALARBRBZ";
  67.     for(int i=0;i<5;i++)
  68.         for(int j=0;j<5;j++)
  69.         {
  70.             cin>>matrix[i][j];
  71.             if(matrix[i][j]==0)
  72.             {
  73.                 empty_c=j;
  74.                 empty_r=i;
  75.             }
  76.         }
  77.  
  78.     cin>>dirn;
  79.  
  80.     for(int i=0;dirn[i]!='Z';i++)
  81.     {
  82.         int flag_0=move(dirn[i]);
  83.         if(flag_0)
  84.         {
  85.             cout<<"NO FINAL CONFIGURATION POSSIBLE";
  86.             flag=1;
  87.             break;
  88.         }
  89.         //cout<<dirn;
  90.  
  91.     }
  92.     if(flag==0)
  93.     {
  94.         for(int i=0;i<5;i++)
  95.         {
  96.             for(int j=0;j<5;j++)
  97.                 cout<<matrix[i][j]<<" ";
  98.             cout<<endl;
  99.         }
  100.  
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement