Tunnels
Time Limit: 1500ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 64-bit integer IO format: %I64d Java class name: Main Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In his eyes, the city is a grid. He can’t enter a grid with a barrier. In one minute, he can move into an adjacent grid with no barrier. Bob is full of curiosity and he wants to visit all of the secret tunnels beneath the city. To travel in a tunnel, he has to walk to the entrance of the tunnel and go out from the exit after a fabulous visit. He can choose where he starts and he will travel each of the tunnels once and only once. Now he wants to know, how long it will take him to visit all the tunnels (excluding the time when he is in the tunnels).
Input
The input contains mutiple testcases. Please process till EOF. For each testcase, the first line contains two integers N (1 ≤ N ≤ 15), the side length of the square map and M (1 ≤ M ≤ 15), the number of tunnels. The map of the city is given in the next N lines. Each line contains exactly N characters. Barrier is represented by “#” and empty grid is represented by “.”. Then M lines follow. Each line consists of four integers x1, y1, x2, y2, indicating there is a tunnel with entrence in (x1, y1) and exit in (x2, y2). It’s guaranteed that (x1, y1) and (x2, y2) in the map are both empty grid.
Output
For each case, output a integer indicating the minimal time Bob will use in total to walk between tunnels. If it is impossible for Bob to visit all the tunnels, output -1.
Sample Input
5 4....#...#................2 3 1 41 2 3 52 3 3 15 4 2 1
Sample Output
7
Source
解题:状压dp。1<<(j-1)表示第i条隧道被选。
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #define LL long long14 #define pii pair 15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 20;18 struct Tunnels {19 int u,v,x,y;20 };21 Tunnels e[maxn];22 int n,m,g[maxn][maxn],dp[1<<15][maxn];23 char mp[maxn][maxn];24 int bfs(Tunnels &a,const Tunnels &b) {25 queue< pii >q;26 static int vis[maxn][maxn];27 static const int dir[4][2] = { 0,-1,-1,0,1,0,0,1};28 memset(vis,-1,sizeof(vis));29 q.push(make_pair(a.x,a.y));30 vis[a.x][a.y] = 0;31 while(!q.empty()) {32 pii now = q.front();33 q.pop();34 if(now.first == b.u && now.second == b.v)35 return vis[now.first][now.second];36 for(int i = 0; i < 4; i++) {37 int x = now.first+dir[i][0];38 int y = now.second+dir[i][1];39 if(vis[x][y] > -1 || mp[x][y] == '#') continue;40 vis[x][y] = vis[now.first][now.second]+1;41 q.push(make_pair(x,y));42 }43 }44 return -1;45 }46 int main() {47 while(~scanf("%d %d",&n,&m)) {48 memset(mp,'#',sizeof(mp));49 for(int i = 1; i <= n; i++)50 scanf("%s",mp[i]+1);51 for(int i = 1; i <= m; i++)52 scanf("%d %d %d %d",&e[i].u,&e[i].v,&e[i].x,&e[i].y);53 for(int i = 1; i <= m; i++) {54 for(int j = 1; j <= m; j++)55 g[i][j] = bfs(e[i],e[j]);56 }57 memset(dp,INF,sizeof(dp));58 for(int i = 1; i <= m; i++) dp[1<<(i-1)][i] = 0;59 int ans = INF;60 for(int i = 1,M = 1 << m; i < M; i++) {61 for(int j = 1; j <= m; j++) {62 if(i&(1<<(j-1))) {63 for(int k = 1; k <= m; k++) {64 if(k == j || (i&(1<<(k-1)) == 0) || g[k][j] == -1) continue;65 dp[i][j] = min(dp[i][j],dp[i^(1<<(j-1))][k]+g[k][j]);66 }67 }68 if(i == M-1) ans = min(ans,dp[i][j]);69 }70 }71 printf("%d\n",ans == INF?-1:ans);72 }73 return 0;74 }