现在的位置: 首页 > 综合 > 正文

hdu 4771 Stealing Harry Potter’s Precious(bfs预处理&TSP)

2019年04月12日 ⁄ 综合 ⁄ 共 4239字 ⁄ 字号 评论关闭

Stealing Harry Potter's Precious

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 284    Accepted Submission(s): 145

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 ##@ #.# 1 2 2 4 4 #@## .... #### .... 2 2 1 2 4 0 0
 

Sample Output
-1 5
 

Source
 

Recommend
We have carefully selected several similar problems for you:  4780 4779 4777 4776 4775 
 

题意:

给你一张N*M(0<N,M <= 100)的地图。'.'表示能走的地方。'#'表示不能走的地方。'@'表示你的起点。然后给你一个k( 0 < K <= 4)。表示k个必须去的地方。地方以坐标的形式给出。然后问你去完所有必须去的地方所需要的最小步数。

每一步只能往上下左右的一个方向走一格。

思路:

先BFS求出所有必须去点和起点之间的距离。然后就是TSP了。

详细见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=100010;
typedef __int64 ll;

struct po
{
    int x,y;
} p[10];
char mp[150][150];
int dx[4]={0,1,0,-1};
int dy[4]={-1,0,1,0};
int dis[10][10];
int vis[150][150];
int q[150*150][3],head,tail;
int dp[10][1<<6];
int n,m,k;
void bfs()
{
    int i,j,x,y,nx,ny,t;

    for(i=0;i<=k;i++)
    {
        memset(vis,0,sizeof vis);
        x=p[i].x;
        y=p[i].y;
        vis[x][y]=1;
        mp[x][y]='.';
        head=tail=0;
        q[tail][0]=x;
        q[tail][1]=y;
        q[tail][2]=0;
        tail++;
        while(head<tail)
        {
            x=q[head][0];
            y=q[head][1];
            for(j=0;j<4;j++)
            {
                nx=x+dx[j];
                ny=y+dy[j];
                if(nx<1||nx>n||ny<1||ny>m||mp[nx][ny]=='#'||vis[nx][ny])
                    continue;
                if(mp[nx][ny]>='0'&&mp[nx][ny]<='0'+k)
                {
                    t=mp[nx][ny]-'0';//这样就可以直接确定哪两个之间的距离了
                    dis[i][t]=dis[t][i]=q[head][2]+1;
                }
                q[tail][0]=nx;
                q[tail][1]=ny;
                q[tail][2]=q[head][2]+1;
                vis[nx][ny]=1;
                tail++;
            }
            head++;
        }
    }
}

int main()
{
    int i,j,x,y,lim,ns,s,ans;

    while(scanf("%d%d",&n,&m),n||m)
    {
        for(i=1;i<=n;i++)
            scanf("%s",mp[i]+1);
        scanf("%d",&k);
        for(i=1;i<=k;i++)
        {
            scanf("%d%d",&x,&y);
            p[i].x=x,p[i].y=y;
            mp[x][y]='0'+i;
        }
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                if(mp[i][j]=='@')
                {
                    p[0].x=i,p[0].y=j,mp[i][j]='0';
                    break;
                }
        memset(dis,0x3f,sizeof dis);
        memset(dp,0x3f,sizeof dp);
        bfs();
        dp[0][1]=0;
        lim=1<<(k+1);
        for(s=1;s<lim;s++)
        {
            for(i=0;i<=k;i++)
            {
                if(dp[i][s]==INF)
                    continue;
                for(j=0;j<=k;j++)
                {
                    if(s&(1<<j)||i==j)
                        continue;
                    ns=s|(1<<j);
                    dp[j][ns]=min(dp[j][ns],dp[i][s]+dis[i][j]);
                }
            }
        }
        ans=INF;
        for(i=0;i<=k;i++)
            ans=min(ans,dp[i][lim-1]);
        if(ans==INF)
            ans=-1;
        printf("%d\n",ans);
    }
    return 0;
}

抱歉!评论已关闭.