# Untitled

unknown
plain_text
a year ago
3.8 kB
2
Indexable
Never
```Given a map of gold mine which can be represented as a NxN matrix
In this map, there are several gold mines that locates on random places (maximum 4 mines each map). A camp will be setup in the map for mining the gold. Every day, workers will go from the camp to gold mines location. In order to reduce the cost, company want to setup the camp so that the distance from it to mining location is optimized.
Let consider below 5x5 map :

- Worker can not go through the rock
- It takes 1 hour to travel a cell in map
There are two mines, from start point (camp location), it would take 4h to go to each gold mine. Let's suppose the final cost will be the highest cost among them, then if we put the camp as above figure, the final cost will be 4.
Now, we consider other example :

In the 3rd example, the final cost will be minimum (1)
[Input]
The first line of input will be the number of test case T (T ≤ 50)
In each TC :
- The first line will give the size of map N (N ≤ 20) and the number of gold mines G (2 ≤ G ≤ 4)
- The next G lines will give location R (row) & C (column) of gold mine (1-base indexed)
- The next N lines will give the map's data : 1 represents the road & gold mines, 0 represents the rock (can not go through)

[Output]
Your program should output the Minimum final cost for traveling from camp to gold mines.
Case #1
1
Case #2
2
Case #3
2
Case #4
7
Case #5
6

[Constraints]
- You can move on 4 directions (up/left/down/right)
- You can not setup the camp on gold mine location or rock
- You can go pass a gold mine then go to other gold mine
- You must place camp so that workers can go to all gold mines

#include<iostream>

using namespace std;
#define size 1000
int N, G;
int map[25][25];
int gold[4][2];
int visit[25][25];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int queueX[size];
int queueY[size];
int front=-1;
int rear=-1;
int cnt[5];
int be;
int ans=0;
bool isEmpty(){
return front==rear;
}
void push(int x, int y){
if(rear==size-1) rear=-1;
rear++;
queueX[rear]=x;
queueY[rear]=y;

}
int popX(){
if(front==size-1) front=-1;
front++;
return queueX[front];
}
int popY(){
if(front==size-1) front=-1;
front;
return queueY[front];
}

void bfs(int x, int y){
visit[x][y]=0;
front=rear =-1;
push(x, y);
while (!isEmpty())
{
int a; int b;
a=popX();
b=popY();
for(int i=0; i<4; i++){
int h=a+dx[i];
int c= b+dy[i];
if( h>=1 && h<=N && c>=1 && c <=N && map[h][c] != 0 && visit[h][c] == -1){

visit[h][c]=visit[a][b]+1;
push(h,c);
}

}

}
}

void reset(){
for(int i=1; i<=N; i++){
for(int j=1;j<=N; j++){
visit[i][j]=-1;
}
}
}

int main()
{
int T;

freopen("Text.txt", "r", stdin);
cin >> T;

for(int test_case = 1; test_case <= T; ++test_case)
{
cin>>N>>G;

for(int i=0; i<G; i++){
int a;
int b;
cin>>a;
cin>>b;
gold[i][0]=a;
gold[i][1]=b;

}

for(int i=1; i<=N;i++){
for(int j=1; j<=N; j++){
cin>>map[i][j];
}
}

for(int i= 0; i<G; i++){
map[gold[i][0]][gold[i][1]]= 2;
}

ans=10000;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++)
{
if(map[i][j] == 1) {
reset();

bfs(i,j);

int tmpMax = 0;

for(int h = 0; h<G; h++){
if (visit[gold[h][0]][gold[h][1]] > tmpMax)
{
tmpMax = visit[gold[h][0]][gold[h][1]];
}if(visit[gold[h][0]][gold[h][1]]==-1){
tmpMax=0;
break;
}

}
if (tmpMax < ans&& tmpMax!=0)
{
ans = tmpMax;
}
}
}

}

cout << "Case #" << test_case  <<endl<< ans << endl;
}
return 0;//Your program should return 0 on normal termination.
}```