fork download
  1. #pragma GCC optimize("Ofast,unroll-loops")
  2. #pragma GCC optimize("O3")
  3. #include <bits/stdc++.h>
  4. #define ll long long
  5. #define endl "\n"
  6. using namespace std;
  7. ll a[1000][1000], Q, n;
  8. int main() {
  9. ios_base::sync_with_stdio(0);
  10. cin.tie(0);
  11. //Author: PhuocThien.
  12. // Input
  13. cin >> n;
  14. for(ll i = 1; i <= n; i ++)
  15. for(ll j = 1; j <= n; j ++)
  16. cin >> a[i][j];
  17. cin >> Q;
  18. while(Q --) {
  19. string s;
  20. cin >> s;
  21. ll x, y;
  22. cin >> x >> y;
  23. bool ok = true;
  24. // TEST
  25. ll ans = a[x][y];
  26. // update 1
  27. for(ll i = 0; i < (ll)s.size(); i ++) {
  28. if(s[i] == 'L') y --;
  29. // LEFT
  30. if(s[i] == 'R') y ++;
  31. //RIGHT
  32. if(s[i] == 'U') x --;
  33. //UP
  34. if(s[i] == 'D') x ++;
  35. //DOWN
  36. ans += a[x][y];
  37. // update
  38. if(a[x][y] == 0) {
  39. // ERROR
  40. cout << "-1\n";
  41. ok = false;
  42. break;
  43. }
  44. }
  45. if(ok) cout << ans << endl;
  46. }
  47. }
  48.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty