Stealing Harry Potter's Precious
来源:互联网 发布:淘宝店铺异常无法开店 编辑:程序博客网 时间:2024/06/05 05:14
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.
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
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
dfs+bfs
题意:给出n*m的矩阵,还有给出几个点,要你在矩阵中遍历这几个点(不重复)遍历的最短距离。。刚看起来好像是tsp问题额,这么难???不过看了一下最多的点数就只有4+起点,唉,直接暴搜。。这题主要就是求出每两点的最短距离(BFS)。然后就是枚举全部路径(DFS递归)。
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;#define inf 0x3f3f3fchar a[110][110];int ni[6];int n,m,k;struct node{ int x,y,bushu;}a1[6];int minn,x1,y1,flag;int vis[110][110];int dr[4][2]= {1,0,-1,0,0,1,0,-1};int bfs(int x1,int y1,int x5,int y5){ memset(vis,0,sizeof(vis)); queue<node>q; node now,next1; now.bushu=0; now.x=x1; now.y=y1; q.push(now); vis[x1][y1]=1; while(!q.empty()) { now=q.front(); if(now.x==x5&&now.y==y5&&a[now.x][now.y]=='o') return now.bushu; q.pop(); for(int i=0; i<4; i++) { int xx=now.x+dr[i][0],yy=now.y+dr[i][1]; if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]!='#'&&vis[xx][yy]==0) { next1.x=xx,next1.y=yy,next1.bushu=now.bushu+1; q.push(next1); vis[xx][yy]=1; } } } return -1;}void we(int x,int sum,int sum2){ if(sum==k) { minn=min(minn,sum2); return; } for(int i=1;i<=k;i++) { if(ni[i]==0) { ni[i]=1; int ha; ha=bfs(a1[x].x,a1[x].y,a1[i].x,a1[i].y); if(ha==-1) flag=1; we(i,sum+1,sum2+ha); ni[i]=0; } }}int main(){ while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; int i,j; flag=0; for(i=0; i<n; i++) { getchar(); for(j=0; j<m; j++) { scanf("%c",&a[i][j]); if(a[i][j]=='@') { x1=i,y1=j; a[i][j]='.'; } } } scanf("%d",&k); a1[0].x=x1,a1[0].y=y1; for(i=1; i<=k; i++) { scanf("%d%d",&a1[i].x,&a1[i].y); a[a1[i].x-1][a1[i].y-1]='o'; a1[i].x=a1[i].x-1; a1[i].y=a1[i].y-1; } minn=inf; we(0,0,0); if(flag) printf("-1\n"); else printf("%d\n",minn); } return 0;}
0 0
- Stealing Harry Potter's Precious
- Stealing Harry Potter's Precious
- hdu 4771 - Stealing Harry Potter's Precious
- HDU:4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- HDU4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- hdu4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- 深度理解链式前向星
- nyoj93 汉诺塔(三)
- js面向对象之构造函数、封装与继承
- SpringJDBC 学习笔记
- 倍升工作效率的小策略
- Stealing Harry Potter's Precious
- 【英语小结】四月小结
- laravel学习笔记(3)— 数据库操作之
- 二叉树的三种先序后序中序遍历的互相推出(例UVA 536
- 二分贪心 T
- R回归分析
- ubuntu16.04配置Javaweb环境(jdk+ tomcat)
- Throwable(Error VS. Exception)
- iOS7实现后台拉取