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.
![](https://www.bnuoj.com/v3/images/hdu/C486-1002-1.jpg)
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 ##@ #.# 1 2 2 4 4 #@## .... #### .... 2 2 1 2 4 0 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 0x3f3f3f char 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; }