JosepRivaille

X61274: Robots

May 24th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. typedef vector< vector<char> > Matriucar;
  7.  
  8. typedef vector< vector<bool> > Rep;
  9.  
  10.  
  11. //Pre: Rep 2 matrius d'entrada
  12. //Post: Escriu valors a la matriu a i omple la b amb fals
  13. void read_matrix(Matriucar& a, Rep& b) {
  14.     int tam = a.size();
  15.     for (int i = 0; i < tam; ++i) {
  16.         for (int j = 0; j < tam; ++j) {
  17.             cin >> a[i][j];
  18.             b[i][j] = false;
  19.         }
  20.     }
  21. }
  22.  
  23.  
  24. //Pre: Rep com a valors les dues matrius i una posicič´¸ inicial
  25. //Post: Escriu el desti dels moviments
  26. void move_matrix(const Matriucar& a, Rep& b, int f, int c) {
  27.     int tam = a.size();
  28.     bool acabat = false;
  29.     while (not acabat) {
  30.         //Explota
  31.         if (a[f][c] == 'X') {
  32.             cout << "kaputt" << endl;
  33.             acabat = true;
  34.         }
  35.         //Es queda sempre dins
  36.         else if (b[f][c] == true) {
  37.             cout << "no escape" << endl;
  38.             acabat = true;
  39.         }      
  40.         //En cas contrari
  41.         else {
  42.             b[f][c] = true;
  43.             //Nord
  44.             if (a[f][c] == 'N') {
  45.                 if (f == 0) {
  46.                     acabat = true;
  47.                     cout << "escape" << endl;
  48.                 }
  49.                 else --f;
  50.             }
  51.             //Oest
  52.             else if (a[f][c] == 'W') {
  53.                 if (c == 0) {
  54.                     acabat = true;
  55.                     cout << "escape" << endl;
  56.                 }
  57.                 else --c;
  58.             }
  59.             //Est
  60.             else if (a[f][c] == 'E') {
  61.                 if (c == tam-1) {
  62.                     acabat = true;
  63.                     cout << "escape" << endl;
  64.                 }
  65.                 else ++c;
  66.             }
  67.             //Sud
  68.             else if (a[f][c] == 'S') {
  69.                 if (f == tam-1) {
  70.                     acabat = true;
  71.                     cout << "escape" << endl;
  72.                 }
  73.                 else ++f;
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79.  
  80. //Pre: Llegeix m matrius de tamany nxn i una posicio inicial
  81. //Post: Escriu el resultat del despla莽ament per la matriu a
  82. //         partir de la posicio inicial:
  83. //         kapput si es destrueix, escape si surt i no escape si es queda per sempre
  84. int main() {
  85.     int m;
  86.     cin >> m;
  87.     while (m != 0) {
  88.         int n, f, c;
  89.         cin >> n >> f >> c;
  90.         Matriucar a(n, vector<char>(n));
  91.         Rep b(n, vector<bool>(n));
  92.         read_matrix(a, b);
  93.         move_matrix(a, b, f, c);
  94.         --m;
  95.     }
  96. }
  97.  
  98. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment