设为首页 加入收藏

TOP

HDU 4856 Tunnels(bfs+状压dp)
2015-07-20 17:26:31 来源: 作者: 【 】 浏览:4
Tags:HDU 4856 Tunnels bfs 状压

题目大意:给你一个N*N的图让你到达所有的“.”点,“#”不能通过,有m组每组有一个入口,一个出口,入口可以传送到出口,不知道经过m组的先后顺序,让你求出走过所有的“.”的最小时间。

思路:先bfs出来所有的m之间的最短距离,然后dp[j][i] 表示,在j状态下开始第i步的最小路程,枚举找到一个最小的dp[1<


Tunnels

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1139 Accepted Submission(s): 344


Problem Description 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 x 1, y 1, x 2, y 2, indicating there is a tunnel with entrence in (x 1, y 1) and exit in (x 2, y 2). It’s guaranteed that (x 1, y 1) and (x 2, y 2) 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 4
1 2 3 5
2 3 3 1
5 4 2 1

Sample Output
7
#include 
  
   
#include 
    #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #define LL __int64 using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 20; struct node { int x0, y0; int x1, y1; } f[maxn]; struct node1 { int x, y; }; int cnt; char str[maxn][maxn]; bool vis[maxn][maxn]; int dis[maxn][maxn]; int d[maxn][maxn]; int dx[] = {0, 1, -1, 0}; int dy[] = {1, 0, 0, -1}; int dp[1<<16][maxn]; int mp[maxn][maxn]; int n, m; void spfa(int s, int t) { for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { vis[i][j] = false; d[i][j] = INF; } } node1 tmp; tmp.x = s; tmp.y = t; vis[s][t] = true; queue
             
              fp; fp.push(tmp); d[s][t] = 0; while(!fp.empty()) { tmp = fp.front(); fp.pop(); for(int i = 0; i < 4; i++) { int x = tmp.x+dx[i]; int y = tmp.y+dy[i]; if(x <= n && x >= 1 && y <= n && y >= 1 && mp[x][y]) { if(vis[x][y]) continue; d[x][y] = d[tmp.x][tmp.y]+1; vis[x][y] = true; node1 tmx; tmx.x = x; tmx.y = y; fp.push(tmx); } } } } int main() { while(~scanf("%d %d",&n, &m)) { memset(mp, 0, sizeof(mp)); for(int i = 1; i <= n; i ++) { scanf("%s", str[i]+1); for(int j = 1; j <= n; j++) if(str[i][j] == '.') mp[i][j] = 1; } for(int i = 1; i <= m; i++) scanf("%d %d %d %d",&f[i].x0, &f[i].y0, &f[i].x1, &f[i].y1); for(int i = 1; i <= m; i++) { spfa(f[i].x1, f[i].y1); for(int j = 1; j <= m; j++) { if(i == j) { dis[i][j] = 0; continue; } dis[i][j] = d[f[j].x0][f[j].y0]; } } memset(dp, INF, sizeof(dp)); for(int i = 1; i <= m; i++) dp[1<<(i-1)][i] = 0; for(int i = 2; i <= m; i++) { for(int j = 0; j < (1<
              
               

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇使用C/C++编译预处理时需要注意的.. 下一篇HDU 1016-Prime Ring Problem(DF..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python爬虫教程(从 (2025-12-26 16:49:14)
·【全269集】B站最详 (2025-12-26 16:49:11)
·Python爬虫详解:原 (2025-12-26 16:49:09)
·Spring Boot Java: (2025-12-26 16:20:19)
·Spring BootでHello (2025-12-26 16:20:15)