题意:给定上一个 n * m的矩阵,你的出发点是 F,你初始有一个电量,每走一步就会少1,如果遇到G,那么就会加满,每个G只能第一次使用,问你把所有的Y都经过,初始电量最少是多少。
析:首先先预处理每个F,G,Y的最短距离,用 bfs 可以实现,然后再二分电量,进行判断,在进行判断时,dp[s][i] 表示已经走点的状态是s,当前在 i,然后如果能走过所有的就就是true,否则是false。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() //#define all 1,n,1 #define FOR(i,x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 15 + 10; const int maxm = 1e5 + 10; const int mod = 50007; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, -1, 0, 1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } char s[maxn][maxn]; vector<P> v; int dp[(1<<16)+10][20]; int dist[20][20]; int vis[20][20]; int bfs(int ss, int tt){ queue<P> q; ms(vis, -1); vis[v[ss].fi][v[ss].se] = 0; q.push(v[ss]); while(!q.empty()){ P p = q.front(); q.pop(); for(int i = 0; i < 4; ++i){ int x = p.fi + dr[i]; int y = p.se + dc[i]; if(!is_in(x, y) || vis[x][y] != -1 || s[x][y] == 'D') continue; vis[x][y] = vis[p.fi][p.se] + 1; P qq = P(x, y); if(qq == v[tt]) return vis[x][y]; q.push(qq); } } return -1; } int start, st;; bool judge(int mid){ ms(dp, -1); dp[1][0] = mid; int all = 1<<v.sz; for(int i = 0; i < all; ++i) for(int j = 0; j < v.sz; ++j){ if(dp[i][j] >= 0 && (i & st) == st) return true; if(dp[i][j] <= 0) continue; for(int k = 0; k < v.sz; ++k){ if(dist[j][k] == -1 || i&1<<k) continue; int &ans = dp[i|1<<k][k]; ans = max(ans, dp[i][j] - dist[j][k]); if(ans >= 0 && s[v[k].fi][v[k].se] == 'G') ans = mid; if(ans >= 0 && ((i|1<<k)&st) == st) return true; } } return false; } int main(){ while(scanf("%d %d", &n, &m) == 2 && n+m){ v.clear(); st = 0; for(int i = 0; i < n; ++i){ scanf("%s", s[i]); for(int j = 0; j < m; ++j){ if(s[i][j] == 'D' || s[i][j] == 'S') continue; if(s[i][j] == 'F') start = v.sz; else if(s[i][j] == 'Y') st |= 1<<v.sz; v.push_back(P(i, j)); } } if(s[v[0].fi][v[0].se] == 'Y') st ^= 1|1<<start; swap(v[start], v[0]); for(int i = 0; i < v.sz; ++i) for(int j = i+1; j < v.sz; ++j) dist[i][j] = dist[j][i] = bfs(i, j); int l = 0, r = n * m; if(!judge(r)){ puts("-1"); continue; } while(l <= r){ int m = l + r >> 1; if(judge(m)) r = m - 1; else l = m + 1; } printf("%d ", l); } return 0; }