Alex_tz307

Addictive Game - Level 3

Oct 26th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("text.in");
  6. ofstream fout("text.out");
  7.  
  8. int N, M, points;
  9. vector < vector < int > > grid, a;
  10.  
  11. int main() {
  12.     fin >> N >> M >> points;
  13.     grid = vector < vector < int > >(N + 1, vector < int >(M + 1));
  14.     for(int i = 0; i < points; ++i) {
  15.         int index, colour;
  16.         fin >> index >> colour;
  17.         int lin = index / M + 1;
  18.         if(index % M == 0)
  19.             --lin;
  20.         int col = index % M;
  21.         if(col == 0)
  22.             col = M;
  23.         grid[lin][col] = colour;
  24.     }
  25.     int n_paths;
  26.     fin >> n_paths;
  27.     for(int itr = 0; itr < n_paths; ++itr) {
  28.         int colour, index, lg;
  29.         fin >> colour >> index >> lg;
  30.         int lin = index / M + 1;
  31.         if(index % M == 0)
  32.             --lin;
  33.         int col = index % M;
  34.         if(col == 0)
  35.             col = M;
  36.         a = grid;
  37.         a[lin][col] = -1;
  38.         int ok = 1, cnt = 0, ans = 0;
  39.         bool reached = false;
  40.         for(int i = 0; i < lg; ++i) {
  41.             char dir;
  42.             fin >> dir;
  43.             fin.get();
  44.             if(ok == 1) {
  45.                 ++cnt;
  46.                 if(dir == 'N')
  47.                     --lin;
  48.                 else
  49.                     if(dir == 'E')
  50.                         ++col;
  51.                 else
  52.                     if(dir == 'S')
  53.                         ++lin;
  54.                 else
  55.                     --col;
  56.                 if(lin < 1 || col < 1 || lin > N || col > M) {
  57.                     ok = -1;
  58.                     ans = cnt;
  59.                 }
  60.                 if(a[lin][col] != 0) {
  61.                     if(a[lin][col] == colour) {
  62.                         if(i == lg - 1) {
  63.                             reached = true;
  64.                             ans = cnt;
  65.                         }
  66.                     }
  67.                     else {
  68.                         ok = -1;
  69.                         ans = (lin - 1) * M + col;
  70.                     }
  71.                 }
  72.                 a[lin][col] = -1;
  73.             }
  74.         }
  75.         if(!reached) {
  76.             ok = -1;
  77.             ans = cnt;
  78.         }
  79.         fout << ok << ' ' << ans;
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment