我的代码(连样例都过不了):
#include <bits/stdc++.h>
using namespace std;
int n, m, dn, c, maze[55][55], cs[55][55], ans;
int mov[2][2] = {0, 1, 1, 0};
void dfs(int x, int y) {
for (int i = 0; i < 2; i++) {
int xx = x + mov[i][0];
int yy = y + mov[i][1];
//cout << xx << ' ' << yy << endl;
if (xx <= n && yy <= m) {
c += maze[xx][yy];
if (xx == n && yy == m) {
ans = max(ans, c);
c -= maze[xx][yy];
return;
}
if (cs[xx][yy] && cs[xx][yy] != -1) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (cs[i][j] && i != xx && j != yy) {
for (int x = 1; x <= n; x++)
for (int y = 1; y <= m; y++)
if (cs[x][y] == 1) cs[x][y] = -1;
dfs(i, j);
for (int x = 1; x <= n; x++)
for (int y = 1; y <= m; y++)
if (cs[x][y] == -1) cs[x][y] = 1;
}
}
dfs(xx, yy);
c -= maze[xx][yy];
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cin >> maze[i][j];
cin >> dn;
for (int i = 1; i <= dn; i++) {
int x, y;
cin >> x >> y;
cs[x][y] = 1;
}
cin >> c;
dfs(1, 1);
if (ans) cout << ans;
else cout << -1;
return 0;
}