At first thought is the search, then did not know how to do ... So many states ... Because the summer wind classification is in the search, the last to read the title of the report is DP ....
Ideas:
First use BFS to find the distance between Y and G,g and F,f and y, and then use the two-point enumeration answer, with DP to determine whether the answer can be done.
DP is a tsp problem ....
When I write, there is a place to pay attention, is the DP judgment inside the end of the judgment ... It started with the 〉~~. Finally found that this does not work .... Because if a power bit is 1, it will satisfy this state ... Finally, a variable judge is constructed to judge ...
The AC code is as follows:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib
> #include <queue> using namespace std;
typedef struct{int x, y;
int step;
}point;
int dp[16][1<<16];
Char maps[20][20];
Point energy[16], switchs[16], point[20], start;
int Num_energy, Num_switchs, num_sum;
int N, M;
int dis[20][20];
int moves[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0,-1}};
int judge;
int Calc_dis (point first, point last) {queue<point> q;
BOOL MARK[20][20];
Memset (Mark, False, sizeof (mark));
First.step = 0;
Q.push (first);
MARK[FIRST.X][FIRST.Y] = true;
while (!q.empty ()) {Point n = Q.front ();
Q.pop ();
if (n.x = = Last.x && N.Y = = Last.y) {return n.step;
} for (int i = 0; i < 4; i++) {Point temp;
temp.x = n.x + moves[i][0];
TEMP.Y = N.Y + moves[i][1];
Temp.step = N.step + 1; if (temp.x < 0 | | temp.x >= N | | temp.y < 0 | | temp.y >= M | | maps[temp.x][TEMP.Y] = = ' D ') {continue;
} if (!mark[temp.x][temp.y]) {Q.push (temp);
MARK[TEMP.X][TEMP.Y] = true;
}}} return-1;
} bool Check (int size) {memset (DP,-1, sizeof (DP));
DP[0][1] = size; for (int i = 1; I <= (1 << (num_sum + 1))-1, i++) {for (int j = 0; J <= Num_sum; j + +) {if (dp[j][
I]! =-1) {if ((I & judge) = = judge) {//note end judgment, and judge construction return true; } for (int k = 1; k <= num_sum; k++) {if (! (I & (1 << k)) && k! = j) {if ( K > Num_switchs && dis[j][k] <= dp[j][i] && dis[j][k]! =-1) {dp[k][i| (
1<<K)] = size; }else if (k <= num_switchs && dis[j][k] <= dp[j][i] && dis[j][k]! =-1) {if (dp[k][i| ( 1<<k)] = =-1) {dp[k][i| (
1<<k)] = Dp[j][i]-dis[j][k]; }else{dp[k][i| ( 1<<k)] = max (dp[k][i| (
1<<K)], Dp[j][i]-dis[j][k]);
}}}}}}} return false; } int main () {while (scanf ("%d%d", &n, &m) && N && M) {memset (maps, 0, sizeof (MAPS)
);
Num_energy = 0;
Num_switchs = 0;
for (int i = 0; i < N; i++) {scanf ("%s", Maps[i]);
for (int j = 0; J < M; J + +) {if (maps[i][j] = = ' F ') {start.x = i;
Start.y = j;
}else if (maps[i][j] = = ' Y ') {switchs[num_switchs].x = i;
Switchs[num_switchs++].y = j;
}else if (maps[i][j] = = ' G ') {energy[num_energy].x = i;
Energy[num_energy++].y = j;
}}} num_sum = Num_energy + num_switchs;
for (int i = 1; I <= num_sum; i++) {if (I <= num_switchs) {point[i] = switchs[i-1];
}else{Point[i] = energy[i-num_switchs-1];
}} Point[0] = start;
Judge = 0;//Note Judge's construction for (int i = 0; I <= num_switchs; i++) {Judge |= (1 << i); } for (int i = 0, I <= num_sum; i++) {for (int j = i + 1; j <= Num_sum; j + +) {
DIS[I][J] = Dis[j][i] = Calc_dis (Point[i], point[j]);
}} int ans, L, R, Mid;
Ans = 260;
L = 0;
r = ans;
while (L <= r) {mid = (L + r)/2;
if (check (mid)) {ans = min (Mid, ans);
R = mid-1;
}else{L = mid + 1;
}} if (ans = = 260) {cout <<-1 << Endl;
}else{cout << ans <<endl;
}} return 0; }