Untitled
unknown
plain_text
2 years ago
4.0 kB
8
Indexable
Gold Mining 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] On the first line : you should output the Minimum final cost for traveling from camp to gold mines. If there is no way to reach all gold mines, print -1 as answer 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 should place camp in order to Maximize the number of reachable gold mines Code: #include<iostream> #define max 1000000 using namespace std; int queuex[10000]; int queuey[10000]; int front= -1; int rear=-1; int n,m; int map[25][25]; int visit[25][25]; int d[25][25]; int toado[4][2]; int ans; int sobuoc=0; int dx[4]={1, -1, 0 ,0}; int dy[4]={0, 0 , 1, -1}; void pushq(int x,int y){ if(rear == max-1) rear =-1; rear++; queuex[rear]=x; queuey[rear]=y; } int popx(){ if(front == max-1) front =-1; front++; return queuex[front]; } int popy(){ if(front == max-1) front =-1; return queuey[front]; } bool IsEmpty(){ if(front == rear) return true; return false; } void reset(){ for(int i=1; i <= n; i++){ for(int j= 1; j <= n; j++){ visit[i][j]=0; } } front =rear =-1; } int Try(int i, int j){ int duongdi=0; int somo=0; sobuoc=0; pushq(i,j); visit[i][j] = 1; d[i][j]=0; while(!IsEmpty()){ if(somo ==m ){ for(int h=0; h < m ; h++){ if(sobuoc < d[toado[h][0]][toado[h][1]]) sobuoc = d[toado[h][0]][toado[h][1]]; } } int x1=popx(); int y1=popy(); for(int h=0; h < 4 ; h++){ int i1= x1+dx[h]; int j1 = y1+dy[h]; if(i1 >= 1 && i1 <=n && j1>=1 && j1 <=n && (map[i1][j1] == 1 || map[i1][j1] == 2) && visit[i1][j1] == 0){ d[i1][j1] = d[x1][y1] +1; for(int k =0; k<m; k++){ if(i1 == toado[k][0] && j1 == toado[k][1]){ duongdi += d[i1][j1] ; somo++; } } pushq(i1,j1); visit[i1][j1]=1; } } } if(sobuoc!=0) return sobuoc; if(sobuoc == 0) return 1000000; } int main(){ freopen("Text.txt", "r", stdin); int test; cin >> test; for(int tc= 1; tc <= test; tc++){ cin >> n>>m; for(int i=0; i < m; i++){ for(int j= 0; j < 2; j++){ cin >> toado[i][j]; } } for(int i=1; i <= n; i++){ for(int j= 1; j <= n; j++){ cin >> map[i][j]; for(int h=0; h < m ; h++){ if( i == toado[h][0] && j==toado[h][1]) map[i][j] =2; } } } ans = 1000000; int f; for(int i=1; i <= n; i++){ for(int j= 1; j <= n; j++){ reset(); if(map[i][j] == 1 ) f= Try(i,j); if(ans > f) ans = f; } } cout << "Case #" << tc << endl; cout << ans << endl; } return 0; }
Editor is loading...