显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
int n, q, l[1505], r[1505], a[1505][1505];
char c[1505][1505];
void init() {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
a[i][j]++;
if(c[i][j] == 'R') a[i][j + 1] += a[i][j];
else if(c[i][j] == 'D') a[i + 1][j] += a[i][j];
}
}
}
void dfs(int x, int y, int cnt, bool ok) {
if(ok) a[x][y] += cnt;
if(y == n && c[x][y] == 'R') a[x][y + 1] += cnt;
else if(x == n && c[x][y] == 'D') a[x + 1][y] += cnt;
else if(c[x][y] == 'R') dfs(x, y + 1, cnt, 1);
else if(c[x][y] == 'D') dfs(x + 1, y, cnt, 1);
}
int ans() {
int sum = 0;
for(int i = 1; i <= n; i++) {
sum += a[i][n + 1] * l[i] + a[n + 1][i] * r[i];
}
return sum;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
cin >> c[i][j];
}
cin >> l[i];
}
for(int i = 1; i <= n; i++) cin >> r[i];
init(); // 初始化
cout << ans() << '\n'; // 计算最初的答案
cin >> q;
while(q--) {
int x, y;
cin >> x >> y;
dfs(x, y, -a[x][y], 0); // 从(x,y)走向右下的奶牛先剔除
if(c[x][y] == 'D') c[x][y] = 'R';
else c[x][y] = 'D';
dfs(x, y, a[x][y], 0); // 按新路线再走一遍
cout << ans() << '\n'; // 输出!
}
return 0;
}