您的位置:首页 > 其它

HDU 4856 Tunnels(BFS+状压DP)

2015-11-07 00:50 351 查看


Tunnels

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1859 Accepted Submission(s): 546



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 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 4
1 2 3 5
2 3 3 1
5 4 2 1


Sample Output

7


Source

2014西安全国邀请赛

Recommend

liuyiding

题意:给出一张N*N的地图,然后有M个隧道的入口和出口,从入口到出口的时间不计,然后要你求经过所以的隧道一次仅一次的最短时间。

思路:首先用BFS求出隧道与隧道之间的最短时间(即一个隧道的出口到另一隧道的入口),然后就是一个不会到起点的TSP模板了。

代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <stack>
#include <cmath>
#include <iomanip>
using namespace std;
#define inf 0x3f3f3f3f
#define CLR(x) memset(x,0,sizeof(x))
#define MEM(x) memset(x,inf,sizeof(x))
typedef long long ll;
int dp[1<<15][25];
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int n,m;
int vis[25][25],cnt[25][25],dist[25][25];
char Map[25][25];
struct edge
{
int x,y;
int step;
};
int bfs(edge start, edge end)//求出隧道的出口到另一隧道的入口的最短时间
{
if(start.x == end.x &&  start.y == end.y) return 0;
CLR(vis);
queue<edge>Q;
start.step = 0;
vis[start.x][start.y] = 1;
Q.push(start);
edge head,next;
while (!Q.empty())
{
head = Q.front();
Q.pop();
if(head.x == end.x && head.y == end.y)return head.step;
for(int i =0; i< 4; i++)
{
next.x = head.x + dir[i][0];
next.y = head.y + dir[i][1];
if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < n && !vis[next.x][next.y] && Map[next.x][next.y] == '.')
{
next.step = head.step + 1;
vis[next.x][next.y] = 1;
Q.push(next);
}
}
}
return inf;
}
int main()
{
#ifdef CDZSC_June
freopen("t.txt","r",stdin);
#endif
while(~scanf("%d %d",&n,&m))
{
for(int i = 0; i < n;i++)
{
scanf("%s",Map[i]);
}
edge start[20],end[20];
for(int i = 0; i < m;i++)
{
scanf("%d %d %d %d",&start[i].x,&start[i].y,&end[i].x,&end[i].y);
start[i].x--,start[i].y--,end[i].x--,end[i].y--;
}
for(int i = 0; i < m;i++)
{
for(int j = 0; j < m;j++)
{
if(i == j)
{
dist[i][j] = 0;
continue;
}
dist[i][j] = bfs(end[i],start[j]);
}
}
MEM(dp);
for(int i = 0; i < m;i++)
{
dp[1<<i][i] = 0;
}
int tmp = 1<<m;
for(int s = 0; s < tmp; s++)//某一状态的枚举
{
for(int v = 0; v< m; v++)//当前到达的那个点
{
for(int u = 0; u<m; u++)//枚举v(当前到达的那个点)能到达哪个点
{
if(!((s >> u) & 1))//判断这个点有木有被访问过了
{
dp[s | 1<<u][u] = min(dp[s][v] + dist[v][u],dp[s | 1<<u][u]);
}
}
}
}
int min1 = inf;
for(int i = 0;i < m; i++)
{
min1 = min(min1,dp[tmp-1][i]);
}
if(min1 == inf)
printf("-1\n");
else
printf("%d\n",min1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: