Find us on Google+ Kill the code

Friday, 29 March 2013

Water Shield : Google Code Jam


PROBLEM DESCRIPTION : 

Geologists sometimes divide an area of land into different regions based on where rainfall flows down to. These regions are called drainage basins.
Given an elevation map (a 2-dimensional array of altitudes), label the map such that locations in the same drainage basin have the same label, subject to the following rules.
  • From each cell, water flows down to at most one of its 4 neighboring cells.
  • For each cell, if none of its 4 neighboring cells has a lower altitude than the current cell's, then the water does not flow, and the current cell is called a sink.
  • Otherwise, water flows from the current cell to the neighbor with the lowest altitude.
  • In case of a tie, water will choose the first direction with the lowest altitude from this list: North, West, East, South.
Every cell that drains directly or indirectly to the same sink is part of the same drainage basin. Each basin is labeled by a unique lower-case letter, in such a way that, when the rows of the map are concatenated from top to bottom, the resulting string is lexicographically smallest. (In particular, the basin of the most North-Western cell is always labeled 'a'.)

SOLUTION:







public class water_shield1 {
    
    static int val1 = 0,row = 3,col = 3;
    /*static int input[][] = {{1,2,3,4,5},{2,9,3,9,6},{3,3,0,8,7},{4,9,8,9,8},{5,6,7,8,9}};
    /*static int input1[][] = {{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
    static int input2[][] = {{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};*/
    static int input[][] = {{9,6,3},{5,9,6},{3,5,9}};
    /*static int input[][] = {{1,2,3},{4,5,6},{7,8,9}};*/
    static int input1[][] = {{0,0,0},{0,0,0},{0,0,0}};
    static int input2[][] = {{0,0,0},{0,0,0},{0,0,0}};
    /*static int input[][] = {{8,8,8,8,8,8,8,8,8,8,8,8,8},{8,8,8,8,8,8,8,8,8,8,8,8,8}};
    static int input1[][] = {{0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0}};
    static int input2[][] = {{0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0}};*/
    public static void main(String args[]) {
        //water_shield1 w = new water_shield1();
        int max = input[0][0],min = input[0][0];
        int i1 = 0,j1 = 0,count = 0;
        for(int i=0;i input[i][j]) {
                    min= input[i][j];
                }
            }
        }
        
        for(int t=0;t=0) {
            min_ard = input[i][j-1];
            a = i;
            b = j-1;
        }
        if((i-1)>=0 && min_ard > input[i-1][j]) {
            min_ard = input[i-1][j];
            a = i-1;
            b = j;
        }
        if((i+1) input[i+1][j]) {
            min_ard = input[i+1][j];
            a = i+1;
            b = j;
        }
        if((j+1) input[i][j+1]) {
            min_ard = input[i][j+1];
            a = i;
            b = j+1;
        }
        //System.out.println("a = " + a + " b = " + b);
        //System.out.println("input1[a][b]" + input1[a][b] + " input1[i][j] = " + input1[i][j]);
        if(input1[a][b] == 0) {
            if(input1[i][j] != 0) {
                input1[a][b] = input1[i][j];
                //System.out.println("assign...");
                //replace(input1[a][b], input1[i][j]);
            }
            else {
            input1[a][b] = input1[i][j] = ++val1;
            //System.out.println("val1" + val1 + " i=" + i + " j = " + j + " a = " + a + " b = " + b);
            }
        }
        else if(input[a][b] != input[i][j]) {
            //System.out.println("input[i][j] = " + input1[i][j] + "input[a][b] = " + input1[a][b]);
            int temp = input1[i][j];
            input1[i][j] = input1[a][b];
            if(input1[a][b] < temp) replace(temp, input1[a][b]);
            else replace(input1[a][b],temp);
            //System.out.println("replace");
        }
        else {
            input1[i][j] = ++val1;
            //System.out.println("");
        } 
    }
    static void replace(int a,int b) { //a is original value, to be replaced with b.
        for(int i=0;i

Sunday, 24 February 2013

Rail Fence Cipher in JAVA

In the rail fence cipher, the plaintext is written downwards and diagonally on successive "rails" of an imaginary fence, then moving up when we reach the bottom rail. When we reach the top rail, the message is written downwards again until the whole plaintext is written out. The message is then read off in rows.
 
This is program for Rail Fence Cipher JAVA.

public class railfence {
    public static void main(String args[])
    {
        String input = "inputstring";
        String output = "";
        int len = input.length(),flag = 0;

        System.out.println("Input String : " + input);
        for(int i=0;i<len;i+=2) {
           
            output += input.charAt(i);
        }
        for(int i=1;i<len;i+=2) {
           
            output += input.charAt(i);
        }
       
        System.out.println("Ciphered Text : "+output);
    }
}







Saturday, 2 February 2013

How to create child process using fork() in C in Unix



Every running instance of a program is known as a process. The concept of processes is fundamental to the UNIX / Linux operating systems. A process has its own identity in form of a PID or a process ID. This PID for each process is unique across the whole operating system. Also, each process has its own process address space where memory segments like code segment, data segment, stack segment etc are placed. The concept of process is very vast and can be broadly classified into process creation, process execution and process termination.



The fork() Function

The fork() function is used to create a new process by duplicating the existing process from which it is called. The existing process from which this function is called becomes the parent process and the newly created process becomes the child process. As already stated that child is a duplicate copy of the parent but there are some exceptions to it.
The child has a unique PID like any other process running in the operating system.
The child has a parent process ID which is same as the PID of the process that created it.
Resource utilization and CPU time counters are reset to zero in child process.
Set of pending signals in child is empty.
Child does not inherit any timers from its parent

Note that the above list is not exhaustive. There are a whole lot of points mentioned in the man page of fork(). I’d strongly recommend readers of this article to go through those points in the man page of fork() function.
The Return Type

Fork() has an interesting behavior while returning to the calling method. If the fork() function is successful then it returns twice. Once it returns in the child process with return value ’0′ and then it returns in the parent process with child’s PID as return value. This behavior is because of the fact that once the fork is called, child process is created and since the child process shares the text segment with parent process and continues execution from the next statement in the same text segment so fork returns twice (once in parent and once in child).

Here is the example of fork() which shows you the how child process is created.



Here is the output file :