hdu 4771 Stealing Harry Potter's Precious(搜索,状压,TSP)

来源:互联网 发布:windows 10 iso 种子 编辑:程序博客网 时间:2024/06/05 05:20

上题:

Problem Description
  Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know, uncle Vernon never allows such magic things in his house. So Harry has to deposit his precious in the Gringotts Wizarding Bank which is owned by some goblins. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:



  Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers' properties, so they live in the indestructible rooms and put customers' properties in vulnerable rooms. Harry Potter's precious are also put in some vulnerable rooms. Dudely wants to steal Harry's things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can't access the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry's precious are. He wants to collect all Harry's precious by as less steps as possible. Moving from one room to another adjacent room is called a 'step'. Dudely doesn't want to get out of the bank before he collects all Harry's things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry's precious.
 

Input
  There are several test cases.
  In each test cases:
  The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 100).
  Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, '.' means a vulnerable room, and the only '@' means the vulnerable room from which Dudely starts to move.
  The next line is an integer K ( 0 < K <= 4), indicating there are K Harry Potter's precious in the bank.
  In next K lines, each line describes the position of a Harry Potter's precious by two integers X and Y, meaning that there is a precious in room (X,Y).
  The input ends with N = 0 and M = 0
 

Output
  For each test case, print the minimum number of steps Dudely must take. If Dudely can't get all Harry's things, print -1.
 

Sample Input
2 3##@#.#12 24 4#@##....####....22 12 40 0
 

Sample Output
-15
题意:题的意思是 ,给你一张图,其实#表示 你现在没有办法过去,.表示你可以走过去,之后又n个点,你要去这n个点拿东西,问你最短的路是多少。起初看见这个问题的时候,想到的唯一思路就是直接搜啊搜搜啊搜 ,但是这里有个问题 ,也就是如果直接搜的话,我们是肯定会走我们曾经走过的路的,也就是说我们这里的标记数组是没有办法用的 ,而且直接搜的话 ,是肯定会 爆栈的,所以写了一大 runtime erro,那么我们应该要怎么做呢?我其实也是没想到 ,比赛结束之后 我问了下同学 ,他的思路就是 我们利用广搜找到每一个宝物之间的最短距离了,之后枚举每一段路,找到最短的那一条路 ,瞬间就明白了 ,发现自己对于 搜索这个精髓还是没有掌握好啊,就只会xjb敲模板,下面上代码吗 其实 枚举每一段路,一个学长用了深搜 。。但是自己太菜 ,,一个全排列水过了。。。如果看到现在 还没看懂的话,就直接看代码吧 ,代码写得很清楚
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>#define INF 9999999using namespace std;int n,m,vis[105][105];char ch[105][105],sx,sy;struct node {int x,y;}p[105];int movee[4][2]={1,0,-1,0,0,1,0,-1};int dist[105][105];int can_go(int x,int y){if(x<0||y<0||x>=n||y>=m){return 0;}if(ch[x][y]=='#'){return 0;}return 1;}int bfs(node s,node e){memset(vis,-1,sizeof(vis));node now,next;queue<node>que;vis[s.x][s.y]=0;que.push(s);while(!que.empty()){now=que.front();que.pop();if(now.x==e.x&&now.y==e.y){//printf("ret=%d\n",vis[now.x][now.y]);return vis[now.x][now.y];}for(int i=0;i<4;i++){next.x=now.x+movee[i][0];next.y=now.y+movee[i][1];if(can_go(next.x,next.y)&&vis[next.x][next.y]==-1){vis[next.x][next.y]=vis[now.x][now.y]+1;//printf("vis[%d][%d]=%d\n",next.x,next.y,vis[next.x][next.y]);que.push(next);}}}return -1;}int main(){while(scanf("%d%d",&n,&m),n||m){int flag=0;for(int i=0;i<n;i++){scanf("%s",ch[i]);for(int j=0;j<m;j++){if(ch[i][j]=='@'){p[0].x=i;p[0].y=j;}}}int t;scanf("%d",&t);for(int i=1;i<=t;i++){int a,b;scanf("%d%d",&a,&b);a--,b--;p[i].x=a;p[i].y=b;}for(int i=0;i<=t;i++){for(int j=0;j<=t;j++){int dis=bfs(p[i],p[j]);if(dis==-1){flag=1;break;}dist[j][i]=dist[i][j]=dis;//第i个宝藏 到第j个宝藏 的距离(0,j表示的是 从起点到宝藏的距离) }}//for(int i=0;i<=t;i++)//{//for(int j=0;j<=t;j++)//{//printf("dis[%d][%d]=%d ",i,j,dist[i][j]);//}//printf("\n");//}if(flag){printf("-1\n");}else {int d[10];for(int i=0;i<=t;i++){d[i]=i;}int ans=INF,sum;do{sum=0;for(int i=0;i<t;i++){//printf("d[%d]=%d d[%d]=%d\n",i,d[i],i+1,d[i+1]);sum+=dist[d[i]][d[i+1]];}//printf("sum==%d\n",sum);if(ans>sum){ans=sum;}}while(next_permutation(d+1,d+t+1)); //记住 0要放在开头的 之后是 1-》2,2—》3的距离 printf("%d\n",ans); }}}


最近学了状态压缩,然后就写了一个状压版本的 思路也是很简单,就是给第i个物品编号为i,然后在标记数组上再加一维表示状态,拿到了就把哪一位的二进制变成1就好了上代码

#include<stdio.h>#include<iostream>#include<queue>#include<string.h>#define mes(a) memset(a,0,sizeof(a));using namespace std;struct node{int x,y;int state;}locate[5];int n,m,sx,sy,t;int dir[4][2]={1,0,-1,0,0,-1,0,1},vis[102][102][1<<4];char mp[101][101];int can_go(int x,int y){if(x<0||y<0||x>=n||y>=m){return 0;}//cout<<mp[x][y]<<"x="<<x<<"y="<<y<<endl; if(mp[x][y]=='#') return 0;return 1;}int judge(int x,int y){for(int i=0;i<t;i++){if(locate[i].x==x&&locate[i].y==y){return i;}}return -1;}int bfs(){node now,next;now.x=sx,now.y=sy;now.state=0;//vis[now.x][now.y][0]=0;queue<node>que;que.push(now);while(!que.empty()){now=que.front();que.pop();//cout<<now.x<<"    "<<now.y<<"----"<<now.state<<endl;if(now.state==(1<<t)-1){return vis[now.x][now.y][now.state];}for(int i=0;i<4;i++){next.x=now.x+dir[i][0];next.y=now.y+dir[i][1];int te=judge(next.x,next.y);if(te==-1){next.state=now.state;}else next.state=now.state|(1<<te);if(can_go(next.x,next.y)&&!vis[next.x][next.y][next.state]){vis[next.x][next.y][next.state]=vis[now.x][now.y][now.state]+1;que.push(next);}}}return -1;}int main(){while(scanf("%d%d",&n,&m)!=EOF){if(n==0&&m==0) break;mes(vis);mes(mp);mes(locate);for(int i=0;i<n;i++){cin>>mp[i];for(int j=0;j<m;j++){if(mp[i][j]=='@') sx=i,sy=j;}}scanf("%d",&t);int ta,tb;for(int i=0;i<t;i++){scanf("%d%d",&ta,&tb);ta--,tb--;locate[i].x=ta;locate[i].y=tb;}int ans=bfs();if(ans==-1)puts("-1");else printf("%d\n",ans);}}

自己在一个深夜的晚上,又想到了 ,这不是一道TSP问题吗,,只走取宝物的点,那么我们可以把宝物抽出来重新建图,每次只走最短路吗,下面上代码吧:

#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>#include<iostream>#define inf 9999999using namespace std;struct node{    int x,y;}edg[5];int vis[111][111],sx,sy,dp[1<<5][5],t,dist[25][25];char ch[111][111];int dir[4][2]={1,0,-1,0,0,1,0,-1};int n,m;int can_go(int x,int y){    if(x<0||y<0||x>=n||y>=m)    {        return 0;    }    if(ch[x][y]=='#')return 0;    return 1;}int bfs(int sx,int sy,int ex,int ey){    node now,next;    now.x=sx,now.y=sy;    queue<node>que;    vis[now.x][now.y]=1;    memset(vis,0,sizeof(vis));    que.push(now);    while(!que.empty())    {        now=que.front();        que.pop();        if(now.x==ex&&now.y==ey)        {            return vis[now.x][now.y];        }        for(int i=0;i<4;i++)        {            next.x=now.x+dir[i][0];            next.y=now.y+dir[i][1];            if(can_go(next.x,next.y)&&!vis[next.x][next.y])            {                vis[next.x][next.y]=vis[now.x][now.y]+1;                que.push(next);            }        }    }    return -1;}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {    int cnt=0;        if(n==0&&m==0) break;        memset(dp,inf,sizeof(dp));        for(int i=0;i<n;i++)        {            cin>>ch[i];            for(int j=0;j<m;j++)            {                if(ch[i][j]=='@')                {                edg[cnt].x=i;                edg[cnt++].y=j;                    sx=i,sy=j;                }            }        }        int ta,tb;        cin>>t;        for(int i=0;i<t;i++)        {            cin>>ta>>tb;            ta--,tb--;            edg[cnt].x=ta,edg[cnt++].y=tb;//把必须走的点抽出来         }        int flag=0;      /*  for(int i=0;i<t;i++)        {            dist[i][0]=dist[0][i]=bfs(sx,sy,edg[i].x,edg[i].y);            if(dis[i][0]==-1) flag=1;        }*/        for(int i=1;i<cnt;i++)        {            for(int j=0;j<cnt;j++)            {                dist[i][j]=dist[j][i]=bfs(edg[i].x,edg[i].y,edg[j].x,edg[j].y);//重新建图                     if(dist[i][j]==-1) flag=1;            }        }        if(flag) puts("-1");        else        {        dp[0][0]=0;//这里就直接抽象成了 ,我现在有cnt个点,我要从第一个点遍历全部的点所需要的最短路 tsp问题吗             for(int state=0;state<(1<<cnt);state++)            {                for(int j=0;j<cnt;j++)                {                    if(dp[state][j]==inf) continue;                    for(int k=0;k<cnt;k++)                    {                        if(state&(1<<k)) continue;                        dp[state|(1<<k)][k]=min(dp[state|(1<<k)][k],dp[state][j]+dist[j][k]);                    }                }            }            int ans=99999999;            for(int i=0;i<cnt;i++)            {               // cout<<dp[(1<<cnt)-1][i]<<"  "<<endl;                ans=min(ans,dp[(1<<cnt)-1][i]);                //cout<<dist[i][0]<<" ";            }            cout<<ans<<endl;        }    }}



0 0