Advertisement
Riposati

ROBO_COM_ERRO

Dec 8th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int linhas,colunas,qtdInstrucoesRobo,i,j,contFigurinhas,x,y;
  8.     string instrucoes;
  9.     char orientacaoInicial;
  10.     bool f;
  11.  
  12.     scanf("%d %d %d",&linhas,&colunas,&qtdInstrucoesRobo);
  13.     getchar();
  14.  
  15.     while(linhas && colunas && qtdInstrucoesRobo){
  16.  
  17.         contFigurinhas=0;
  18.         f=false;
  19.  
  20.         char grid[linhas][colunas];
  21.  
  22.         for(i=0;i<linhas;i++){
  23.             for(j=0;j<colunas;j++){
  24.                 scanf("%c",&grid[i][j]);
  25.  
  26.                 if(grid[i][j]=='N' || grid[i][j]=='O' || grid[i][j]=='L'
  27.                    || grid[i][j]=='S'){
  28.                     orientacaoInicial = grid[i][j];
  29.                 }
  30.             }
  31.             getchar();
  32.         }
  33.  
  34.         cin>>instrucoes;
  35.  
  36.         for(i=0;i<instrucoes.size();i++){
  37.  
  38.             // instrucao tipo E
  39.             if(orientacaoInicial=='N' && instrucoes[i]=='E'){
  40.                 instrucoes[i] = 'w';
  41.                 orientacaoInicial = 'O';
  42.             }
  43.             if(orientacaoInicial=='O' && instrucoes[i]=='E'){
  44.                 instrucoes[i] = 'w';
  45.                 orientacaoInicial = 'S';
  46.             }
  47.             if(orientacaoInicial=='S' && instrucoes[i]=='E'){
  48.                 instrucoes[i] = 'w';
  49.                 orientacaoInicial = 'L';
  50.             }
  51.             if(orientacaoInicial=='L' && instrucoes[i]=='E'){
  52.                 instrucoes[i] = 'w';
  53.                 orientacaoInicial = 'N';
  54.             }
  55.  
  56.             // instrucao tipo D
  57.             if(orientacaoInicial=='N' && instrucoes[i]=='D'){
  58.                 instrucoes[i] = 'w';
  59.                 orientacaoInicial = 'L';
  60.             }
  61.             if(orientacaoInicial=='L' && instrucoes[i]=='D'){
  62.                 instrucoes[i] = 'w';
  63.                 orientacaoInicial = 'S';
  64.             }
  65.  
  66.             if(orientacaoInicial=='S' && instrucoes[i]=='D'){
  67.                 instrucoes[i] = 'w';
  68.                 orientacaoInicial = 'O';
  69.             }
  70.  
  71.             if(orientacaoInicial=='O' && instrucoes[i]=='D'){
  72.                 instrucoes[i] = 'w';
  73.                 orientacaoInicial = 'N';
  74.             }
  75.  
  76.             // instruções para andar
  77.             if(instrucoes[i]=='F'){
  78.  
  79.                 // precisa disso pra achar o robo na grade
  80.                 for(x=0;x<linhas;x++){
  81.                     for(y=0;y<colunas;y++){
  82.  
  83.                            if(grid[x][y]=='O' || grid[x][y]=='L'|| grid[x][y]=='S' || grid[x][y]=='N'){
  84.                                 f = true;
  85.                                 break;
  86.                             }
  87.                     }
  88.  
  89.                     if(f)break;
  90.                 }
  91.  
  92.                  if(grid[x][y]=='N'){// tem que andar pra cima
  93.  
  94.                     if(x > 0 && grid[x-1][y]!='#'){
  95.  
  96.                         if(grid[x-1][y]=='*'){
  97.                             contFigurinhas++;
  98.                         }
  99.                         grid[x-1][y] = 'N';
  100.                         grid[x][y] = '.';
  101.                     }
  102.                 }
  103.  
  104.                 if(grid[x][y]=='S'){// tem que andar pra baixo
  105.  
  106.                     if(x < linhas && grid[x+1][y]!='#'){
  107.  
  108.                         if(grid[x+1][y]=='*'){
  109.                             contFigurinhas++;
  110.                         }
  111.  
  112.                         grid[x+1][y] = 'S';
  113.                         grid[x][y] = '.';
  114.                     }
  115.                 }
  116.  
  117.                 if(grid[x][y]=='O'){// tem que andar pra DIREITA
  118.  
  119.                     if(y < colunas && grid[x][y+1]!='#'){
  120.  
  121.                         if(grid[x][y+1]=='*'){
  122.                             contFigurinhas++;
  123.                         }
  124.  
  125.                         grid[x][y+1] = 'O';
  126.                         grid[x][y] = '.';
  127.                     }
  128.                 }
  129.  
  130.                 if(grid[x][y]=='L'){// tem que andar pra ESQUERDA
  131.  
  132.                     if(y > 0 && grid[x][y-1]!='#'){
  133.  
  134.                         if(grid[x][y-1]=='*'){
  135.                             contFigurinhas++;
  136.                         }
  137.                         grid[x][y-1] = 'L';
  138.                         grid[x][y] = '.';
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.  
  144.         printf("%d\n",contFigurinhas);
  145.  
  146.         scanf("%d %d %d",&linhas,&colunas,&qtdInstrucoesRobo);
  147.         getchar();
  148.     }
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement