hdu4771

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771

题目描述:

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
/
/思路:广搜找到各点间的最短距离保存到edge数组里面,深度搜索找到最小和即可(因为是最多四个顶点,可暴力求解)。ps:本来想用最小生成树的,发现不可以
#include<iostream>
#include <string.h>
#include <queue>
#define MAXX 999999
using namespace std;
int edge[105][105];    //保存两个点间的最短距离
char map[105][105];       //保存银行布局
int visit[105][105],visit2[5];      //标记访问
int dir[4][2]={0,1,0,-1,1,0,-1,0};   //广搜方向
int sx,sy,mindis;   //sx,sy为起始点坐标,mindis为找到各点间最短距离的中间变量
int n,m, ncase,minsum;//n*m的银行布局,ncase为有几个要走的点,minsum为最小和


struct point   //定义一个结构体,保存要走的点的坐标,z是走了多少步
{
int x,y,z;
}p[5],temp1,temp2;  //p为保存各点坐标数组


void bfs(int x1,int y1,int x2,int y2)   //广搜找最小距离
{
int i;
    queue<point>Q;     //定义一个结构体队列
temp1.x=x1;
temp1.y=y1;
temp1.z=0;
visit[x1][y1]=1; 
Q.push(temp1);        //将temp1初始化后入队列;
while(!Q.empty())
{
temp1=Q.front();   //得到队列头部元素
Q.pop();   //出队列
for(i=0;i<4;i++)
{
temp2.x=temp1.x+dir[i][0];
temp2.y=temp1.y+dir[i][1];
temp2.z=temp1.z+1;
if(temp2.x<1||temp2.x>n||temp2.y<1||temp2.y>m) continue;   //若出界,继续
if(temp2.x==x2&&temp2.y==y2)     //若找到,记录步数即距离
{
mindis=temp1.z+1;
break;
}
if(map[temp2.x][temp2.y]!='#'&&!visit[temp2.x][temp2.y])  //若没找到但该点可走,标记访问,入队列
{
visit[temp2.x][temp2.y]=1;
Q.push(temp2);
}
}
if(mindis!=MAXX) break;   //找到,结束
}
return;
}


void dfs(int sum,int k,int front)         //深搜找到最小和,front代表前一个点,k代表找到了几个点,sum为当前和
{
int i,j;
for(i=1;i<=ncase;i++)
{
if(!visit2[i]&&edge[front][i]!=MAXX)                  //没有被找到且存在边
{
k++;      //点数加1
if(k==ncase&&minsum==MAXX) minsum=sum+edge[front][i];           //第一次找到
if(k==ncase)             //以后找到一种走法和mindis比较一次
{
                if(sum+edge[front][i]<minsum)        //若更小,则替换
minsum=sum+edge[front][i];
}
visit2[i]=1;           //标记访问
dfs(sum+edge[front][i],k,i);       //递归搜索
visit2[i]=0;           //回复状态
k--;
}
}
}




int main()
{

while(cin>>n>>m&&n||m)  //输入n*m银行布局
{
int i,j;

for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
cin>>map[i][j];      //输入n*m银行布局
if(map[i][j]=='@')    //记录@点坐标
{
sx=i;
sy=j;
}
}

cin>>ncase;
for(i=1;i<=ncase;i++)
{
                cin>>p[i].x>>p[i].y;     //输入有宝藏的点
}
for(i=0;i<=ncase;i++)
for(j=0;j<=ncase;j++)
edge[i][j]=MAXX;  //将各点之间的距离初始化为最大值
p[0].x=sx;
p[0].y=sy;
for(i=0;i<=ncase;i++)
{
   for(j=0;j<=ncase;j++)
{
if(i==j) continue; 
memset(visit,0,sizeof(visit));
mindis=MAXX;
bfs(p[i].x,p[i].y,p[j].x,p[j].y);         //搜索找到各点最短距离
if(mindis<edge[i][j])
edge[i][j]=edge[j][i]=mindis;
}
}
minsum=MAXX;
dfs(0,0,0);              //找到最小和
if(minsum!=MAXX) cout<<minsum<<endl;
else cout<<"-1"<<endl;        //未找到,输出-1
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值