思路错了,没办法解决分支的问题。想了想,应该是个插头dp。 电脑清文件,在这里备份下代码。说不定还以后用得上
#include <bits/stdc++.h>
using namespace std;
int mp[15][15];
int vis[15][15];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int sum = 1, m, tem;
set<int> st[70];
vector<int> p(70);
vector<pair<int,int> > E[100];
int book[10];
int ans = -0x3f3f3f3f;
bool ck1 = false;
int ss,xx,yy ;
void dfs2(int s,int nowans) {
if(s == ss) ck1 =true;
if(nowans > ans && ck1 == true) ans = nowans;
int now = s;
for(int i = 0; i < (int)E[now].size(); i++) {
int next = E[now][i].first;
if(book[next] == 0) {
book[next] = 1;
if(next == ss) ck1 =true;
dfs2(next, nowans + E[now][i].second);
book[next] = 0;
}
}
if(s == ss) ck1 = false;
}
void dfs(int x,int y) {
for(int i = 0; i < 4;i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if(tx >= 1 && tx <= m && ty >= 1 && ty <=m)
if(mp[tx][ty] >=0 && vis[tx][ty] == 0) {
vis[tx][ty] = sum;
tem += mp[tx][ty];
dfs(tx,ty);
}
}
}
int main(void) {
cin >> m >> xx >> yy;
for(int i = 1; i<= m; i++)
for(int j = 1; j <= m; j++)
cin >> mp[i][j];
for(int i = 1; i <= m; i++)
for(int j = 1; j <= m; j++) {
if(mp[i][j] >= 0 && vis[i][j] == 0) {
vis[i][j] = sum;
tem = mp[i][j];
dfs(i,j);
p[sum] = tem;
sum++;
}
else if(mp[i][j] < 0 && vis[i][j] == 0) {
vis[i][j] = sum;
p[sum] = mp[i][j];
sum++;
}
}
cout << endl;
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= m; j++) {
cout << vis[i][j] << " ";
}
cout <<endl;
}
cout << endl;
for(int x = 1; x <= m; x++) {
for(int y = 1; y <= m; y++)
for(int k = 0; k < 4; k++){
int tx = x + dx[k];
int ty = y + dy[k];
if(tx >= 1 && tx <= m && ty >= 1 && ty <=m && vis[x][y] != vis[tx][ty] ) {
st[vis[x][y]].insert(vis[tx][ty]);
}
}
}
for(int i = 1; i < sum; i++) {
for(auto it = st[i].begin(); it != st[i].end(); ++it) {
cout << i << " " << *it << " " << p[*it] << endl;
E[i].push_back(make_pair(*it, p[*it]));
}cout << endl;
}
ss = vis[xx][yy];
for(int i = 1 ; i < sum; i++) {
memset(book,0,sizeof(book));
book[i] = 1;
dfs2(i, p[i]);
}
cout << ans << endl;
return 0;
}