Advertisement
juanjo12x

UVA_227_Puzzle

Aug 4th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <cstdio>
  7. #include <algorithm>
  8. #include <cstring>
  9. #include <string>
  10. #include <cctype>
  11. #include <stack>
  12. #include <queue>
  13. #include <list>
  14. #include <vector>
  15. #include <map>
  16. #include <set>
  17. #include <sstream>
  18. #include <stdlib.h>
  19. #include <cmath>
  20. #define LL unsigned long long
  21. using namespace std;
  22. char table[5][5];int px,py;
  23. int cont=0;
  24. bool move(char move){
  25.     char temp;
  26.     if(move=='L'){
  27.       if(py-1>=0){
  28.         temp=table[px][py-1];
  29.         table[px][py]=temp;
  30.         table[px][py-1]='x';
  31.         py--;/*nueva posicion*/
  32.         return true;
  33.       }else return false;
  34.     }else if(move=='R'){
  35.         if(py+1<5){
  36.         temp=table[px][py+1];
  37.         table[px][py]=temp;
  38.         table[px][py+1]='x';
  39.         py++;/*nueva posicion*/
  40.         return true;
  41.         }else return false;
  42.     }else if(move=='B'){
  43.         if(px+1<5){
  44.         temp=table[px+1][py];
  45.         table[px][py]=temp;
  46.         table[px+1][py]='x';
  47.         px++;
  48.         return true;
  49.         }else return false;
  50.     }else if(move=='A'){
  51.         if(px-1>=0){
  52.         temp=table[px-1][py];
  53.         table[px][py]=temp;
  54.         table[px-1][py]='x';
  55.         px--;
  56.                 return true;
  57.         }else return false;
  58.     }else return false;/*caracter no reconocido*/
  59. }
  60. int main() {
  61. char cad[5];char moves[200];int n;bool termina;
  62. while(gets(cad)){
  63.     if(strcmp(cad,"Z")==0) break;
  64.     if(cont>0)printf("\n");
  65.        
  66.     cont++;
  67.    
  68.     termina=true;
  69.    
  70.     for (int j=0;j<5;j++){
  71.         if(cad[j]==' ') table[0][j]='x';    
  72.         else table[0][j]=cad[j];   
  73.         if(cad[j]==' '){px=0;py=j;}
  74.     }
  75.     for(int i=1;i<=4;i++){
  76.         gets(cad);
  77.         for (int j=0;j<5;j++){
  78.         if(cad[j]==' ') table[i][j]='x';
  79.         else table[i][j]=cad[j];
  80.         if(cad[j]==' '){px=i;py=j;}
  81.        
  82.         }
  83.     }
  84.        
  85.     while(gets(moves)){
  86.         n=strlen(moves);
  87.         for (int i=0;i<n;i++){
  88.           if(moves[i]!='0' && moves[i]!=' ') termina=move(moves[i]);
  89.           else if(moves[i]!=0 && moves[i]==' ') continue;
  90.           if(!termina) break;
  91.         }
  92.         if(moves[n-1]=='0') break;//indica que terminamos de leer el input
  93.        
  94.     }
  95.     printf("Puzzle #%d:\n",cont);
  96.     if(termina){
  97.         for(int i=0;i<5;i++){
  98.             for(int j=0;j<5;j++){
  99.                 if((j<4) &&(table[i][j]!='x')) printf("%c ",table[i][j]);
  100.                  else if(table[i][j]=='x'&& j<4) printf("  ");
  101.                 else if(table[i][j]=='x' && j==4)printf(" \n");
  102.                         else if(table[i][j]!='x' && j==4)printf("%c\n",table[i][j]);
  103.             }
  104.         }
  105.     }else{
  106.         printf("This puzzle has no final configuration.\n");
  107.     }
  108.    
  109.     cad[0]='\0';
  110.    
  111. }
  112.  
  113.  return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement