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 1Sample Output
7
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
int n,m;
char mp[20][20];
struct Point{
int sx,sy;
int ex,ey;
}p[20];
int dis[20][20];
struct node{
int x,y;
int step;
node(int _x,int _y,int _step):x(_x),y(_y),step(_step){}
};
int vis[20][20];
int in[20];
int wrong;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void get_dis(int so){
memset(vis,0,sizeof(vis));
memset(in,0,sizeof(in));
in[so]=1;
vis[p[so].ex][p[so].ey]=1;
for(int j=1;j<=m;j++){
if(in[j])continue;
if(p[so].ex==p[j].sx&&p[so].ey==p[j].sy){
in[j]=1;
dis[so][j]=0;
}
}
queue<node> q;
q.push(node(p[so].ex,p[so].ey,0));
while(q.size()){
node now=q.front();
q.pop();
for(int i=0;i<4;i++){
int nx=now.x+dir[i][0],ny=now.y+dir[i][1];
if(vis[nx][ny]||nx<1||nx>n||ny<1||ny>n||mp[nx][ny]=='#')continue;
vis[nx][ny]=1;
q.push(node(nx,ny,now.step+1));
for(int j=1;j<=m;j++){
if(in[j])continue;
if(nx==p[j].sx&&ny==p[j].sy){
in[j]=1;
dis[so][j]=now.step+1;
}
}
}
}
}
/*
难题就是简单题的组合,好像若有所思
还有不要把1<<16,这种东西,随便加个10,这可是指数啊
*/
/*
怎样来描述这个状态呢
所有走过的点,加上一个终点,也就是现在的点
转移就是在当前所有走过的点中,选择一个起点,在选择一个终点
然后从起点到终点进行松弛,看看当前点是不是可以松弛终点
不能用floyd来写,因为不是让你求一个点到其他点的最短路
而是让你去求一条遍历所有点的最短路
*/
int dp[20][1<<16];
void get_ans(){
memset(dp,inf,sizeof(dp));
for(int i=1;i<=m;i++)dp[i][1<<(i-1)]=0;//初始化,我们的起点
for(int sta=1;sta<(1<<m);sta++){
for(int i=1;i<=m;i++){//最后
if(sta&(1<<(i-1))){
for(int j=1;j<=m;j++){
if(i!=j&&(sta&(1<<(j-1)))){
dp[j][sta]=min(dp[j][sta],dp[i][sta-(1<<(j-1))]+dis[i][j]);
}
}
}
}
}
}
int main(){
while(scanf("%d %d",&n,&m)==2){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf(" %c",&mp[i][j]);
}
}
for(int i=1;i<=m;i++){
scanf("%d %d %d %d",&p[i].sx,&p[i].sy,&p[i].ex,&p[i].ey);
}
memset(dis,inf,sizeof(dis));
for(int i=1;i<=m;i++){
dis[i][i]=0;
get_dis(i);
}
/*
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++){
printf("i:%d j:%d %d\n",i,j,dis[i][j]);
}
}
*/
get_ans();
int ans=inf;
for(int i=1;i<=m;i++)
ans=min(ans,dp[i][(1<<m)-1]);
if(ans==inf)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}