-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2055C.cpp
More file actions
82 lines (69 loc) · 1.88 KB
/
2055C.cpp
File metadata and controls
82 lines (69 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
string s;
cin >> s;
vector<vector<int>> a(n, vector<int>(m));
// Input the matrix
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
vector<pair<int, int>> path;
int r = 0, c = 0;
path.push_back({r, c});
// Parse the moves
for (char move : s) {
if (move == 'D') {
r++;
} else { // Assuming the only other move is 'R'
c++;
}
path.push_back({r, c});
}
// Process the path and fix the matrix
for (size_t k = 0; k < path.size(); ++k) {
int r = path[k].first, c = path[k].second;
if (k < path.size() - 1) {
int next_r = path[k + 1].first, next_c = path[k + 1].second;
if (next_r > r) { // Moving Down, fix the current row
int row_sum = 0;
for (int j = 0; j < m; ++j) {
row_sum += a[r][j];
}
a[r][c] = row_sum - a[r][c];
} else { // Moving Right, fix the current column
int col_sum = 0;
for (int i = 0; i < n; ++i) {
col_sum += a[i][c];
}
a[r][c] = col_sum - a[r][c];
}
} else { // At the last position
int col_sum = 0;
for (int i = 0; i < n; ++i) {
col_sum += a[i][c];
}
a[r][c] = col_sum - a[r][c];
}
}
// Output the modified matrix
for (const auto& row : a) {
for (int num : row) {
cout << num << " ";
}
cout << "\n";
}
}
int main() {
long long t;
cin>>t;
while (t--)
{
solve();
}
return 0;
}