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

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →