JosepRivaille

P45681: F012B. Wall-E

May 25th, 2015
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. typedef vector<char> Row;
  8. typedef vector<Row> Matrix;
  9.  
  10.  
  11. void read_matrix(Matrix& M) {
  12.     int f = M.size();
  13.     int c = M[0].size();
  14.     for (int i = 0; i < f; ++i) {
  15.         for (int j = 0; j < c; ++j) {
  16.             cin >> M[i][j];
  17.         }
  18.     }
  19. }
  20.  
  21. int move_matrix(Matrix& M, int f, int c, string s) {
  22.     int tam_s = s.size();
  23.     int count = 0;
  24.     for (int k = 0; k < tam_s; ++k) {
  25.         //Actualitzacič´¸ posicič´¸ i suma
  26.         //Nord
  27.         if (s[k] == 'N') {
  28.             while (M[f-1][c] != 'X') {
  29.                 if (M[f][c] != '.') {
  30.                     count += M[f][c] - '0';
  31.                     M[f][c] = '.';
  32.                 }
  33.                 --f;
  34.             }
  35.         }
  36.         //Sud
  37.         else if (s[k] == 'S') {
  38.             while (M[f+1][c] != 'X') {
  39.                 if (M[f][c] != '.') {
  40.                     count += M[f][c] - '0';
  41.                     M[f][c] = '.';
  42.                 }
  43.                 ++f;
  44.             }
  45.         }
  46.         //Est
  47.         else if (s[k] == 'E') {
  48.             while (M[f][c+1] != 'X') {
  49.                 if (M[f][c] != '.') {
  50.                     count += M[f][c] - '0';
  51.                     M[f][c] = '.';
  52.                 }
  53.                 ++c;
  54.             }
  55.         }
  56.         //Oest
  57.         else if (s[k] == 'O') {
  58.             while (M[f][c-1] != 'X') {
  59.                 if (M[f][c] != '.') {
  60.                     count += M[f][c] - '0';
  61.                     M[f][c] = '.';
  62.                 }
  63.                 --c;
  64.             }
  65.         }
  66.     }
  67.     //Ultima posicio
  68.     if (M[f][c] != '.') count += M[f][c] - '0';
  69.     return count;
  70. }
  71.  
  72. void write_matrix(int deixalles, int count) {
  73.     cout << "Cas " << count << ": " << deixalles << endl;
  74. }  
  75.  
  76. int main() {
  77.     int f, c;
  78.     int count = 0;
  79.     while (cin >> f >> c) {
  80.         ++count;
  81.         Matrix M(f, Row(c));
  82.         read_matrix(M);
  83.         string s;
  84.         cin >> f >> c >> s;
  85.         write_matrix(move_matrix(M, f, c, s), count);
  86.     }
  87. }
  88.  
  89. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment