hdu4771
제목 설명:
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
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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
제1 6 장 파일 에서 텍스트 검색 도구: grep 명령 과 egrep 명령제1 6 장 파일 에서 텍스트 검색 도구: grep 명령 과 egrep 명령 옵션 grep 명령 파일 에서 단 어 를 검색 하면 명령 은 "match pattern"을 포함 하 는 텍스트 줄 을 되 돌려 줍 니 다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.