fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. int main() {
  6. ll t;
  7. cin >> t;
  8. while (t--) {
  9. ll n, m;
  10. cin >> n >> m;
  11. char s[n + m - 2];
  12. scanf("%s", s);
  13. vector<vector<ll>> a(n, vector<ll>(m));
  14. vector<ll> rowSum(n, 0);
  15. vector<ll> colSum(m, 0);
  16.  
  17. for (ll i = 0; i < n; i++) {
  18. for (ll j = 0; j < m; j++) {
  19. cin >> a[i][j];
  20. rowSum[i] += a[i][j];
  21. colSum[j] += a[i][j];
  22. }
  23. }
  24.  
  25. ll x = 0, y = 0;
  26. for (ll i = 0; i < n + m - 1; i++) {
  27. if (s[i] == 'D') {
  28. a[x][y] = -rowSum[x];
  29. colSum[y] += a[x][y];
  30. x++;
  31. } else {
  32. a[x][y] = -colSum[y];
  33. rowSum[x] += a[x][y];
  34. y++;
  35. }
  36. }
  37. for (ll i = 0; i < n; i++) {
  38. for (ll j = 0; j < m; j++) {
  39. cout << a[i][j] << " ";
  40. }
  41. cout << endl;
  42. }
  43. }
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 5284KB
stdin
4
3 3
DRRD
0 2 3
0 0 0
3 1 0
4 5
DRRRRDD
0 1 0 2 3
0 0 0 0 0
-1 0 -3 -3 0
0 0 0 -1 0
2 3
RRD
0 0 0
0 1 0
5 5
DDDDRRRR
0 25 2 9 11
0 6 13 20 22
0 17 24 1 8
0 3 10 12 19
0 0 0 0 0
stdout
-5 2 3 
2 -3 1 
3 1 -4 
-6 1 0 2 3 
7 -1 3 2 -11 
-1 0 -3 -3 7 
0 0 0 -1 1 
0 -1 1 
0 1 -1 
-47 25 2 9 11 
-61 6 13 20 22 
-50 17 24 1 8 
-44 3 10 12 19 
202 -51 -49 -42 -60