Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. char turn(char x,char y){
  4. //recibe posicion actual del robot (N,S,E,W) y la instruccion (left o right) devuelve la nueva posicion
  5.     switch(x){
  6.         case 'E':
  7.             if(y=='R'){
  8.                 return 'S';
  9.             }else if(y=='L'){
  10.                 return 'N';
  11.             }
  12.         break;
  13.         case 'W':
  14.             if(y=='R'){
  15.                 return 'N';
  16.             }else if(y=='L'){
  17.                 return 'S';
  18.             }
  19.             break;
  20.         case 'N':
  21.             if(y=='R'){
  22.                 return 'E';
  23.             }else if(y=='L'){
  24.                 return 'L';
  25.             }
  26.             break;
  27.         case 'S':
  28.             if(y=='R'){
  29.                 return 'W';
  30.             }else if(y=='L'){
  31.                 return 'E';
  32.             }
  33.             break;
  34.        
  35.     }
  36. }
  37. bool wasHere(char *lost,int x, int y){
  38. //busca las coordenadas dadas en el arreglo de posiciones de los perdidos
  39.     for(int i=0; lost[i]!='\0'; i=i+2){
  40.         if(lost[i]==x && lost[i+1]==y){
  41.             return true;
  42.             }
  43.     }
  44.     return false;
  45. }
  46.  
  47.  
  48. int main(int argc, char *argv[]) {
  49.     int limx,limy,actx,acty, tope=0,isLost;
  50.     char dir, aux;
  51.     char mov[100], lost[100];
  52.     scanf("%i%i",&limx,&limy); //recibe coordenadas de la esquina superior derecha
  53.     while(scanf("%i%i%c",&actx,&acty,&dir)!=EOF){ //pide input mientras reciba: coordenada inicial en x ,y, posicion actual
  54.         isLost=0;
  55.         fflush(stdin);
  56.         scanf("%s",mov); //lee instrucciones
  57.         for(int i=0;mov[i]!='\0';i++){
  58.             if(mov[i]=='R' || mov[i]=='L'){
  59.                 dir=turn(dir,mov[i]); //transforma la direccion
  60.             }else if(mov[i]=='F'){ //si el movimiento es avanzar
  61.             switch (dir){
  62.                     case 'N':
  63.                         if(acty+1 <0 || acty+1>limy ){ //si el sig. movimiento rebasa los limites esta perdido
  64.                         isLost=1;
  65.                         lost[tope++]=actx; //guarda coordenada x en arreglo
  66.                         lost[tope++]=acty; //guarda coordenada y en arreglo
  67.                         }
  68.                         if(!wasHere(lost,actx,acty)){ //valida: si la posicion actual no ha sido ocupada por un robot perdido, avanza
  69.                         acty++;
  70.                             }
  71.                         break;
  72.                     case 'S':
  73.                         if(acty-1 <0 || acty-1>limy ){
  74.                         isLost=1;
  75.                         lost[tope++]=actx;
  76.                         lost[tope++]=acty;
  77.                         }
  78.                         if(!wasHere(lost,actx,acty)){
  79.                         acty--;
  80.                             }
  81.                         break;
  82.                     case 'E':
  83.                         if(actx+1 <0 || actx+1>limy ){
  84.                         isLost=1;
  85.                         lost[tope++]=actx;
  86.                         lost[tope++]=acty;
  87.                         }
  88.                         if(!wasHere(lost,actx,acty)){
  89.                         actx++;
  90.                             }
  91.                         break;
  92.                     case 'W':
  93.                         if(actx-1 <0 || actx-1>limy ){
  94.                         isLost=1;
  95.                         lost[tope++]=actx;
  96.                         lost[tope++]=acty;
  97.                         }
  98.                         if(!wasHere(lost,actx,acty)){
  99.                         actx--;
  100.                             }
  101.                         break;
  102.        
  103.                     }
  104.                    
  105.                
  106.                
  107.             }
  108.         }
  109.         if(isLost){
  110.         printf("%i %i %c LOST",actx,acty,dir);
  111.         }else{
  112.         printf("%i %i %c",actx,acty,dir);
  113.         }
  114.    
  115.     }
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement