HDU 4856 tunnels

Source: Internet
Author: User
Tags exit in
Tunnels Time Limit: 3000/1500 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 844 accepted submission (s): 249

Problem description

Bob is traveling in Xi'an. he finds eclipsecret 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

 

Memory-based search

view code#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <vector>using namespace std;typedef long long ll;const int INF = 0x3f3f3f3f;int dp[15][1<<15];bool vis[15][1<<16];int bit[25], n, m;char str[20][20];int dis[25][25], ans, tot;int dx[4] = {0, 0, -1, 1};int dy[4] = {-1, 1, 0, 0};struct node{    int x, y, dis;    node() {}    node(int x, int y, int dis):x(x),y(y),dis(dis) {}    bool operator == (const node &o){        return x==o.x && y==o.y;    }};struct edge{    node a, b;}sto[20];void init(){    for(int i=0; i<20; i++) bit[i] = 1<<i;}bool is_ok(int x, int y){    if(x<0 || x>=n || y<0 || y>=n || str[x][y]==‘#‘) return 0;    return true;}bool look[20][20];int getdis(node a, node b){    queue<node >q;    q.push(node(a.x, a.y, 0));    memset(look, 0, sizeof(look));    look[a.x][a.y]=1;    while(!q.empty())    {        node o = q.front(); q.pop();        if(o==b) return o.dis;        for(int i=0; i<4; i++)        {            int x = o.x+dx[i], y = o.y+dy[i];            if(!is_ok(x, y) || look[x][y]) continue;            q.push(node(x, y, o.dis+1));            look[x][y] = 1;        }    }    return INF;}int dfs(int u, int s){    if(vis[u][s]) return dp[u][s];    if(s==tot) return 0;    vis[u][s] = 1;    int &res = dp[u][s];    res = INF;    for(int i=0; i<n; i++)    {        if(bit[i]&s || u==i) continue;        res = min(dfs(i, s|bit[i])+dis[u][i], res);    }    return res;}void solve(){    for(int i=0; i<n; i++) scanf("%s", str[i]);    for(int i=0; i<m; i++)    {        scanf("%d%d%d%d", &sto[i].a.x, &sto[i].a.y, &sto[i].b.x, &sto[i].b.y);        sto[i].a.x --; sto[i].a.y--; sto[i].b.x--; sto[i].b.y--;        for(int j=0; j<i; j++)        {            dis[i][j] = getdis(sto[i].b, sto[j].a);            dis[j][i] = getdis(sto[j].b, sto[i].a);        }    }    memset(vis, 0, sizeof(vis));    int ans = INF;    tot = bit[m]-1;    for(int i=0; i<m; i++){        int tmp = dfs(i, bit[i]);        ans = min(tmp, ans);    }    if(ans==INF) ans = -1;    printf("%d\n", ans);}int main(){//    freopen("in.txt", "r", stdin);    init();    while(scanf("%d%d", &n, &m)>0) solve();    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.