csansoon

P8.13 P67340 F010B. Threatening bishops

Nov 30th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. typedef vector<bool> Fila;
  7. typedef vector<Fila> Tauler;
  8.  
  9. void find_threats_direction(const Tauler& tauler, int f1, int c1, int dx, int dy) {
  10.     int n = tauler.size();
  11.     int m = tauler[0].size();
  12.    
  13.     int i = f1 + dx;
  14.     int j = c1 + dy;
  15.    
  16.     bool trobat = false;
  17.     while (not trobat and i < n and i >= 0 and j < m and j >= 0) {
  18.         if (tauler[i][j]) {
  19.             cout << '(' << f1+1 << ',' << c1+1 << ")<->(" << i+1 << ',' << j+1 << ')' << endl;
  20.             trobat = true;
  21.         }
  22.        
  23.         i += dx;
  24.         j += dy;
  25.     }
  26. }
  27.  
  28. void find_threats(const Tauler& tauler, int f1, int c1) {
  29.     find_threats_direction(tauler, f1, c1, 1, 1);  // cap a baix a la dreta
  30.     find_threats_direction(tauler, f1, c1, 1, -1);  // cap a baix a l'esquerra
  31.     find_threats_direction(tauler, f1, c1, -1, 1);  // cap a dalt a la dreta
  32.     find_threats_direction(tauler, f1, c1, -1, -1);  // cap a dalt a l'esquerra
  33. }
  34.  
  35. int main() {
  36.     int n, m;
  37.     cin >> n >> m;
  38.    
  39.     Tauler tauler = Tauler(n, Fila(m));
  40.    
  41.     char c;
  42.     for (int i = 0; i < n; ++i) {
  43.         for (int j = 0; j < m; ++j) {
  44.             cin >> c;
  45.             tauler[i][j] = (c == 'X');
  46.         }
  47.     }
  48.    
  49.     for (int i = 0; i < n; ++i) {
  50.         for (int j = 0; j < m; ++j) {
  51.             if (tauler[i][j]) {
  52.                 find_threats(tauler, i, j);
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58.  
  59. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment