Oil Wel


Problem Statement :


Mr. Road Runner bought a piece of land in the middle of a desert for a nominal amount. It turns out that the piece of land is now worth millions of dollars as it has an oil reserve under it. Mr. Road Runner contacts the ACME corp to set up the oil wells on his land. Setting up oil wells is a costly affair and the charges of setting up oil wells are as follows.

The rectangular plot bought by Mr. Road Runner is divided into r * c blocks. Only some blocks are suitable for setting up the oil well and these blocks have been marked. ACME charges nothing for building the first oil well. For every subsequent oil well built, the cost would be the maximum ACME distance between the new oil well and the existing oil wells.

If (x,y) is the position of the block where a new oil well is setup and (x1, y1) is the position of the block of an existing oil well, the ACME distance is given by

max(|x-x1|, |y-y1|)
the maximum ACME distance is the maximum among all the ACME distance between existing oil wells and new wells.

If the distance of any two adjacent blocks (horizontal or vertical) is considered 1 unit, what is the minimum cost (E) in units it takes to set up oil wells across all the marked blocks?

Input Format

The first line of the input contains two space separated integers r *c*.
r lines follow each containing c space separated integers.
1 indicates that the block is suitable for setting up an oil well, whereas 0 isn't.

r c  
M11 M12 ... M1c  
M21 M22 ... M2c  
...  
Mr1 Mr2 ... Mrc  
Constraints

1 <= r, c <= 50
Output Format

Print the minimum value E as the answer.



Solution :



title-img


                            Solution in C :

In C++ :





#include <iostream>

using namespace std;

#define M 53
#define INF 30300000

int n,m,a[M][M],s[M][M][M][M];

void read(void){
	cin>>n>>m;
	for (int i=0; i<n; ++i)
		for (int j=0; j<m; ++j)
			cin>>a[i][j];
}

int mod(int x){
	return x<0 ? -x:x;
}

int fine(int x, int y, int l, int r, int u, int d){
	return max(max(abs(l-x),abs(x-r)),max(abs(y-d),abs(y-u)));
}

void din(void){
	for (int i=0; i<n; ++i)
		for (int l=0; l+i<n; ++l)
			for (int j=0; j<m; ++j)
				for (int u=0; u+j<m; ++u){
					int r=l+i;
					int d=u+j;
					if (l==r && u==d){
						s[l][r][u][d]=0;
						continue;
					}

					int h=INF;

					if (l<r){
						int kl=0,kr=0;
						for (int j=u; j<=d; ++j){
							if (a[l][j])
								kl+=fine(l,j,l+1,r,u,d);
							if (a[r][j])
								kr+=fine(r,j,l,r-1,u,d);
						}
						h=min(h,s[l+1][r][u][d]+kl);
						h=min(h,s[l][r-1][u][d]+kr);
					}

					if (u<d){
						int ku=0,kd=0;
						for (int j=l; j<=r; ++j){
							if (a[j][u])
								ku+=fine(j,u,l,r,u+1,d);
							if (a[j][d])
								kd+=fine(j,d,l,r,u,d-1);
						}
						h=min(h,s[l][r][u+1][d]+ku);
						h=min(h,s[l][r][u][d-1]+kd);
					}

					s[l][r][u][d]=h;
					//cout<<l<<' '<<r<<' '<<u<<' '<<d<<"->"<<h<<"\n";
				}

	cout<<s[0][n-1][0][m-1]<<"\n";
}

int main(){
	//freopen("test.in","r",stdin);
	//freopen("test.out","w",stdout);
	read();
	din();
	return 0;
}









In Java :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static int dist(int x1,int y1,int x2,int y2) {
        int x = x1 - x2, y = y1 - y2;
        if (x < 0) {
            x = -x;
        }
        if (y < 0) {
            y = -y;
        }
        return (x > y)?x:y;
    }
    public static int cost(int x,int y,int x1,int y1,int x2,int y2) {
       int d1 = dist(x,y,x1,y1), d2 = dist(x,y,x2,y2);
        return (d1 > d2)?d1:d2;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int row = scanner.nextInt(), col = scanner.nextInt();
        int [][] a = new int [row][col];
        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < col; ++j) {
                a[i][j] = scanner.nextInt();
                
            }
        }
        int[][][][] result = new int [row][col][row][col];
        for (int x1 = row - 1; x1 >= 0; --x1) {
            for (int y1 = col - 1; y1 >= 0; --y1) {
                for (int x2 = x1; x2 < row; ++x2) {
                    for (int y2 = (x1 == x2)?(y1 + 1):y1; y2 < col; ++y2) {
                        result[x1][y1][x2][y2] = 1000000000;
                        if (x1 < x2) {
                            int temp = result[x1 + 1][y1][x2][y2]; 
                            for (int y = y1; y <= y2; ++y) {
                                if (a[x1][y] == 1) {
                                    temp += cost(x1, y, x1 + 1, y1, x2, y2);
                                }
                            }
                        
                        
                            if (result[x1][y1][x2][y2] > temp) {
                            
                                result[x1][y1][x2][y2] = temp;
                            }
                     
                            temp = result[x1][y1][x2 - 1][y2];
                            for (int y = y1; y <= y2; ++y) {
                                if (a[x2][y] == 1) {
                                    temp += cost(x2, y, x1 ,y1, x2 - 1, y2);
                            
                                }
                            }
                            if (result[x1][y1][x2][y2] > temp) {
                            
                                result[x1][y1][x2][y2] = temp;
                            }
                        }
                        if (y1 < y2) {
                            int temp = result[x1][y1 + 1][x2][y2];
                            for (int x = x1; x <= x2; ++x) {
                                if (a[x][y1] == 1) {
                                    temp += cost(x, y1, x1, y1 + 1, x2, y2);
                                }
                            }
                            if (result[x1][y1][x2][y2] > temp) {
                        
                                result[x1][y1][x2][y2] = temp;
                            }
                            temp = result[x1][y1][x2][y2 - 1];
                            for (int x = x1; x <= x2; ++x) {
                                if (a[x][y2] == 1) {
                                    temp += cost(x, y2, x1, y1, x2, y2 - 1);
                                }
                            }
                            if (result[x1][y1][x2][y2] > temp) {
                        
                                result[x1][y1][x2][y2] = temp;
                            }
                        
                        }
                    
                    }
                }
            }
        }
        System.out.println(result[0][0][row - 1][col - 1]);
        
        
    }
}









In C :





#include <stdio.h>

int a[55][55][55][55],b[55][55],t,i,j,k,l,m,n,r,c,dy,dx;
int z[55][55][55],v[55][55][55],u;

int main()
{

scanf("%d %d", &r, &c);

for(i=0;i<r;i++)
 for(j=0;j<c;j++)
  scanf("%d",&b[i][j]);

for(i=0;i<c;i++)
 for(j=0;j<r;j++)
 {
   z[j][j][i] = b[j][i];
   for(k=j+1;k<r;k++) z[j][k][i] = z[j][k-1][i] + b[k][i]; 
 }

for(i=0;i<r;i++)
 for(j=0;j<c;j++)
 {
   v[i][j][j] = b[i][j];
   for(k=j+1;k<c;k++) v[i][j][k] = v[i][j][k-1] + b[i][k]; 
 }


  
/*  
for(i=0;i<r;i++) 
 for(k=0;k<r;k++)
  for(j=0;j<c;j++)
   for(l=j;l<c;l++)
*/

u = -1;

for(dy=0;dy<r;dy++) 
 for(dx=0;dx<c;dx++)
  for(i=0;i+dy<r;i++)
   for(j=0;j+dx<c;j++)
/*   
for(dy=0;dy<2;dy++) 
 for(dx=0;dx<2;dx++)
  for(i=0;i+dy<2;i++)
   for(j=0;j+dx<2;j++)
*/
    {
      k = i+dy;
      l = j+dx;
    
       m = 2000000000; 
    
      if(k==i && l==j) m = 0;
    
      if(k-i >= l-j && k-i > 0)
       {
         t = a[i+1][j][k][l];  
         if(v[k][j][l]) 
           {
            t +=  v[i][j][l]*(k-i);
            if(t<m) m = t;
           }

         t = a[i][j][k-1][l];  
         if(v[i][j][l]) 
           {
            t +=  v[k][j][l]*(k-i);
            if(t<m) m = t;       
           }
       }    
    
//    printf("prve m %d\n",m);
    
      if(k-i <= l-j && l-j > 0)
       {
         t = a[i][j+1][k][l];  
         if(z[i][k][l]) 
          {
           t +=  z[i][k][j]*(l-j);
           if(t<m) m = t;
          }

         t = a[i][j][k][l-1];  
         if(z[i][k][j]) 
          {
           t +=  z[i][k][l]*(l-j);
           if(t<m) m = t;       
          }
       }    
    
      a[i][j][k][l] = m;    
   
   if(m!=2000000000 && m > u) u = m;
   
     // if(m) 
//      printf("%d %d %d %d -> %d\n",i,j,k,l,m);
    }  
/*
for(i=0;i<r;i++)
 for(j=0;j<c;j++)
  for(k=j;k<c;k++)
   printf("v %d %d %d -> %d\n", i,j,k, v[i][j][k]);
*/

/*
for(j=0;j<c;j++)
 for(i=0;i<r;i++)
  for(l=i;l<r;l++)
   printf("z %d %d %d -> %d\n", i,l,j, z[i][l][j]);
*/

//k = a[0][0][r-1][c-1];

if(u<0) u = 0;

printf("%d\n",u);

return 0;
}









In Python3 :





r, c = list(map(int, input().strip().split()))
n = max(r, c)
g = [[0]*n for i in range(n)]
for i in range(r):
	bs = list(map(int, input().strip().split()))
	for j in range(c):
		g[i][j] = bs[j]

x = [[0]*(n+1) for i in range(n+1)]
for i in range(n):
	for j in range(n):
		x[i+1][j+1] = x[i+1][j] + x[i][j+1] - x[i][j] + g[i][j]

fs  = g
fz  = [[0]*n for i in range(n)]
ans = [[0]*n for i in range(n)]
anz = [[0]*n for i in range(n)]
for d in range(1,n):
	for i in range(n-d):
		I = i + d + 1
		for j in range(n-d):
			J = j + d + 1
			total = fz[i][j] = x[I][J] - x[i][J] - x[I][j] + x[i][j]
			anz[i][j] = min(
				ans[i  ][j  ] + d*(total - fs[i  ][j  ]),
				ans[i  ][j+1] + d*(total - fs[i  ][j+1]),
				ans[i+1][j  ] + d*(total - fs[i+1][j  ]),
				ans[i+1][j+1] + d*(total - fs[i+1][j+1]),
			)
	ans, anz = anz, ans
	fs,  fz =  fz,  fs

print (ans[0][0])
                        








View More Similar Problems

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →

QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →

Jesse and Cookies

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t

View Solution →

Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →

Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →

Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

View Solution →