hdu4771

来源:互联网 发布:制造业数据分析 编辑:程序博客网 时间:2024/06/05 06:47

题目链接: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##@#.#12 24 4#@##....####....22 12 40 0
 

Sample Output
-15
/
/思路:广搜找到各点间的最短距离保存到edge数组里面,深度搜索找到最小和即可(因为是最多四个顶点,可暴力求解)。ps:本来想用最小生成树的,发现不可以#include<iostream>#include <string.h>#include <queue>#define MAXX 999999using 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++;      //点数加1if(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
原创粉丝点击