Find us on Google+ Kill the code: C
Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

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

Monday, 9 April 2012

Line Clipping Algorithm in C



#include<stdio.h>

#include<conio.h>

#include<graphics.h>



void main()

{

 int x1, y1, x2, y2, a1, a2, a3, a4, b1, b2, b3, b4, xmin, ymin, xmax, ymax, qa, qb;

 int gd=DETECT, gm, c1, c2, c3, c4, xint1, yint1, xint2,  yint2;

 float m;

 clrscr();

 printf("enter end-points of line : ");

 scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

 printf("enter co-ord. of screen : ");

 scanf("%d %d %d %d", &xmin, &ymin, &xmax, &ymax);

 if(x1<xmin)

 {

  a3 = 0; a4 = 1;

 }

 if(x1>xmin && x1<xmax)

 {

  a3 = 0; a4 = 0;

 }

 if(x1>xmax)

 {

  a3 = 1; a4 = 0;

 }

 if(y1<ymin)

 {

  a1 = 0; a2 = 1;

 }

 if(y1>ymin && y1<ymax)

 {

  a1 = 0; a2 = 0;

 }

 if(y1>ymax)

 {

  a1 = 1; a2 = 0;

 }

 if(x2<xmin)

 {

  b3 = 0; b4 = 1;

 }

 if(x2>xmin && x2<xmax)

 {

  b3 = 0; b4 = 0;

 }

 if(x2>xmax)

 {

  b3 = 1; b4 = 0;

 }

 if(y2<ymin)

 {

  b1 = 0; b2 = 1;

 }

 if(y2>ymin && y2<ymax)

 {

  b1 = 0; b2 = 0;

 }

 if(y2>ymax)

 {

  b1 = 1; b2 = 0;

 }

 if(a1 == 0 && a2 == 0 && a3 == 0 && a4 == 0)

 {

  qa = 5;

 }

 if(b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0)

 {

  qb = 5;

 }

 initgraph(&gd, &gm, "c://tc//bgi");

 c1 = a1&b1;

 c2 = a2&b2;

 c3 = a3&b3;

 c4 = a4&b4;

 if(c1 == 0 && c2 == 0 && c3 == 0 && c4 == 0)

 {

  if(a1 == 0 && a2 == 0 && a3 == 0 && a4 == 0 && b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0)

   printf("line is completely inside");

  else

   printf("line is partially inside");

 }

 else

  printf("line is completely outside");







if(x2-x1 == 0 || y2-y1 == 0)

{

 if(x2 - x1 == 0)

 {

  if(qa == 5)

   line(x1, y1, x1, ymax);



  if(qb == 5)

   line(x1, ymin, x2, y2);



  if(y1<=ymin && y2>=ymax)

   line(x1,ymin, x2, ymax);



 }

 if(y2 - y1 == 0)

 {



  if(qa == 5)

   line(x1, y1, xmax, y2);



  if(qb == 5)

   line(xmin, y1, x2, y2);



  if(x1<=xmin && x2>=xmax)

   line(xmin, y1, xmax, y2);



 }

}

else

{

 xint1 = ((x2-x1)*(ymin-y1)/(y2-y1))+x1;

 xint2 = ((x2-x1)*(ymax-y1)/(y2-y1))+x1;

 yint1 = ((xmin-x1)*(y2-y1)/(x2-x1))+y1;

 yint2 = ((xmax-x1)*(y2-y1)/(x2-x1))+y1;



 if(qa == 5 || qb == 5)

 {

  if(qa == 5)

  {

   if(xint2<=xmax)

   line(x1, y1, xint2, ymax);

   if(yint2<=ymax)

   line(x1, y1, xmax, yint2);

  }

  if(qb == 5)

  {

   if(xint1>=xmin)

   line(xint1, ymin, x2, y2);

   if(yint1>=ymin)

   line(xmin, yint1, x2, y2);

  }

 }

 else

 {

  if(xint1>=xmin && xint1<=xmax)

  {

   if(xint2>=xmin && xint2<=xmax)

    line(xint1, ymin, xint2, ymax);



   if(yint2>=ymin && yint2<=ymax)

    line(xint1, ymin, xmax, yint2);



  }

  if(yint1>=ymin && yint1<=ymax)

  {

   if(xint2>=xmin && xint2<=xmax)

    line(xmin, yint1, xint2, ymax);



   if(yint2>=ymin && yint2<=ymax)

    line(xmin, yint1, xmax, yint2);



  }

 }

}



 rectangle(xmin, ymin, xmax, ymax);

 getch();

}

Tuesday, 13 March 2012

Dynamic Finite Automata for String Validation in C

#include<conio.h>

#include<stdio.h>

void main()

{

 int tab[10][10], i, j, k, t=65, b, r, c, l=0;

 char s[20], t1[50];

 clrscr();

 printf("enter rows and cols : ");

 scanf("%d",&r);

 scanf("%d",&c);

 printf("enter your state transition table : ");

 scanf("%s",t1);

 for(i=0; i<r; i++)

 {

  for(j=0; j<c; j++)

  {

   tab[i][j]=t1[l];

   l++;

  }

 }

 for(i=0; i<r; i++)

 {

  for(j=0; j<c; j++)

  {

   printf("%c", tab[i][j]);

  }

  printf("\n");

 }

 printf("enter a string : ");

 scanf("%s",s);

 for(i=0; i<strlen(s); i++)

 {

  for(k=0; k<r; k++)

  {

   if(t==tab[k][0]) b=k;



  }

  if(s[i]=='0')

  {

   t=tab[b][1];

  }

  if(s[i]=='1')

  {

   t=tab[b][2];

  }

 }

 if(t==tab[r-1][0]) printf("valid");

 else printf("invalid");

 getch();

}

Saturday, 18 February 2012

Vigener Cipher in C


// WAP to implement Vignere cipher in C.

#include<conio.h>
#include<stdio.h>
void main()
{
  int i,j,b=65,c=65,d=65,t[20];
  char s[20],a[26][26];
  clrscr();
  for(i=0;i<26;i++)
  {
    for(j=0;j<26;j++)
    {
      if(b<91)
      {
        a[i][j]=b;
        b++;
      }
      else
      {
        a[i][j]=d;
        d++;


      }
    }
    c++;
    b=c;
    d=65;
  }
  for(i=0;i<26;i++)
  {
    for(j=0;j<26;j++)
    {
      printf("%c",a[i][j]);
    }
    printf("\n");
  }
  printf("\nEnter the text : ");
  gets(s);
  for(i=0;i<strlen(s);i++)
  {
    t[i]=s[i]-65;
  }
  for(i=0;i<strlen(s);i++)
  {
    s[i]=(a[t[i]][i]);
  }
  for(i=0;i<strlen(s);i++)
  {
    printf("%c",s[i]);
  }
  getch();
}


/*OUTPUT : 


Enter the text : 


D E A R
 |   |  |  |
 |   |  |  |
 |   |  |  |           // Cipher text is : 
D F C V
 |  |   |  |
 |  |   |  |
 |  |   |  |
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
C D E F G H I J K L M N O P Q R S T U V W X Y Z A B 
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C 
E F G H I J K L M N O P Q R S T U V W X Y Z A B C D 
F G H I J K L M N O P Q R S T U V W X Y Z A B C D E 
G H I J K L M N O P Q R S T U V W X Y Z A B C D E F 
H I J K L M N O P Q R S T U V W X Y Z A B C D E F G 
 I J K L M N O P Q R S T U V W X Y Z A B C D E F G H
J K L M N O P Q R S T U V W X Y Z A B C D E F G H I 
K L M N O P Q R S T U V W X Y Z A B C D E F G H I J 
L M N O P Q R S T U V W X Y Z A B C D E F G H I J K 
M N O P Q R S T U V W X Y Z A B C D E F G H I J K L 
N O P Q R S T U V W X Y Z A B C D E F G H I J K L M 
O P Q R S T U V W X Y Z A B C D E F G H I J K L M N 
P Q R S T U V W X Y Z A B C D E F G H I J K L M N O 
Q R S T U V W X Y Z A B C D E F G H I J K L M N O P 
R S T U V W X Y Z A B C D E F G H I J K L M N O P Q 
S T U V W X Y Z A B C D E F G H I J K L M N O P Q R 
T U V W X Y Z A B C D E F G H I J K L M N O P Q R S 
U V W X Y Z A B C D E F G H I J K L M N O P Q R S T 
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U 
W X Y Z A B C D E F G H I J K L M N O P Q R S T U V 
X Y Z A B C D E F G H I J K L M N O P Q R S T U V W 
Y Z A B C D E F G H I J K L M N O P Q R S T U V W X 
Z A B C D E F G H I J K L M N O P Q R S T U V W X Y 


 */

Sky effect in C

// WAP to implement sky effect in C...


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
int d=DETECT,m,i;
clrscr();
initgraph(&d,&m,"c:\\tc\\bgi");
for(i=0;i<150;i++)
{
putpixel(rand()%1000,rand()%1000,rand()%1000);
putpixel(rand()%1000,rand()%1000,rand()%1000);
putpixel(rand()%1000,rand()%1000,rand()%1000);
putpixel(rand()%1000,rand()%1000,rand()%1000);
putpixel(rand()%1000,rand()%1000,rand()%1000);
putpixel(rand()%1000,rand()%1000,rand()%1000);
putpixel(rand()%1000,rand()%1000,rand()%1000);
putpixel(rand()%1000,rand()%1000,rand()%1000);
delay(50);
}
getch();
closegraph();
}

Friday, 17 February 2012

Rail Fence Cipher in C



#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k=0,l=0,m=0;
char s[20],a[10],b[10];
clrscr();
printf("enter a string:");
scanf("%s",s);
for(i=0;i<strlen(s);i++)
{
if(i%2==0)
{
a[k]=s[i];
k++;
}
else
{
b[l]=s[i];
l++;
}
}


for(i=0;i<k;i++)
{
printf("%c ",a[i]);
s[m]=a[i];
m++;
}
printf("\n");
for(i=0;i<l;i++)
{
printf(" %c",b[i]);
s[m]=b[i];
m++;
}
printf("\n\ncipher text is %s",s);
getch();
}

Thursday, 9 February 2012

DDA Line Drawing Algorithm in C


#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm, i;
float dx, dy, a, b, c, d, m, t;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("enter starting and ending point : ");
scanf("%f %f %f %f",&a,&b,&c,&d);
if(a>c)
{
t=a;
a=c;
c=t;
t=b;
b=d;
d=t;
}
dx=c-a;
dy=d-b;
m=dy/dx;
putpixel(a,b,100);
if(abs(m)<=1)
{
for(i=0; i<dx; i++)
{
a=a+1;
b=b+m;
putpixel(a,b,100);
}
}
if(abs(m)>1)
{
for(i=0; i<dx; i++)
{
a=a+(1/m);
b=b+1;
putpixel(a,b,100);
}
}
getch();
closegraph();
}

Wednesday, 8 February 2012

Mid-Point Circle program in C



#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int xc, yc, x=0, y, i=0, gd=DETECT, gm, p[20];
clrscr();
initgraph(&gd, &gm, "c:\\tc\\bgi");
printf("enter the center of the circle : ");
scanf("%d",&xc);
scanf("%d",&yc);
printf("enter the radius : ");
scanf("%d",&y);
p[0]=1-y;
putpixel(xc+x,yc+y,100);
putpixel(xc+x,yc-y,100);
putpixel(xc+y,yc+x,100);
putpixel(xc+y,yc-x,100);
putpixel(xc-x,yc+y,100);
putpixel(xc-x,yc-y,100);
putpixel(xc-y,yc+x,100);
putpixel(xc-y,yc-x,100);



while(x<=y)
{
if(p[i]<0)
{
p[i+1]=p[i]+2*x+1;
x=x+1;
i++;
}
else
{
p[i+1]=p[i]+2*x-2*y+1;
x=x+1;
y=y-1;
i++;
}
putpixel(xc+x,yc+y,100);
putpixel(xc-x,yc+y,100);
putpixel(xc+x,yc-y,100);
putpixel(xc-x,yc-y,100);
putpixel(xc+y,yc+x,100);
putpixel(xc+y,yc-x,100);
putpixel(xc-y,yc+x,100);
putpixel(xc-y,yc-x,100);
}
getch();
closegraph();
}

Monday, 6 February 2012

Play-Fair Cipher in C


/* Play Fair Cipher in C... */



#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,isI=0,l,k=0,start=0,same=0,k1=0;
int i1,j1,i2,j2,i11,jj;
char alph[5][5],chr[25],chr2[25],character,chr1='a',chr_ex[25];
char txt[25],txt2[25];
clrscr();
printf("\nEnter the text : ");
scanf("%s",chr);
for(i=0;i<strlen(chr2);i++) /*TO PREVENT STRING FROM BEING PRINTED WITH GARBAGE CHARACTER...*/
{
chr_ex[i]='-';
}
for(i=0;i<strlen(chr);i++)
{
printf("\nchr[%d] = %c",i+1,chr[i]);
chr_ex[i]=chr[i];
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
alph[i][j]='-';
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
character=chr[++k];
for(l=k-1;l>=0;l--) //FOR REPEATING CHARACTERS...
{
if(character==chr[l])
{
chr[k]='-';
break;
}
}
}
}
k=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(start++<strlen(chr))
if(chr[k]!='-') //FOR i AND j
{
if(chr[k]=='i' || chr[k]=='j')
{
if(isI==0)
{
alph[i][j]=chr[k];
isI=1;
}
else
{
if(chr[k]=='i') chr[k]='i';
if(chr[k]=='j') chr[k]='j';
j--;
}
}
else alph[i][j]=chr[k];
}
else
{
chr[k]='-';
j--;
}
k++;
}
}
chr1--;
k1=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(alph[i][j]=='-')
{
same=0;
chr1++;
for(k1=0;k1<strlen(chr);k1++)
{
if(chr1==chr[k1])
{
same=1; //else same=0;
j--;
break;
}
}
if(same!=1)
{
if(chr1=='i' || chr1=='j')
{
if(isI==0)
{
alph[i][j]=chr1;
isI=1;
}
else j--;
}
else alph[i][j]=chr1;
}
}
}
}
printf("\n\n\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%c ",alph[i][j]);
}
printf("\n");
}
j=0;
for(i=0;i<strlen(chr);i++)
{
if(chr[i]=='-')
{
for(j=i;j<strlen(chr);j++)
{
chr[j]=chr[j+1];
}
}
}
printf("\n\nEnter the plain text : ");
scanf("%s",txt);
j=0;
for(i=0;i<strlen(txt);i++)
{
txt2[j++]=txt[i];
if(txt[i]==txt[i+1])
{
txt2[j++]='x';
}
}
jj=j;
for(i11=0;i11<strlen(txt2);i11+=2)
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(txt2[i11]==alph[i][j])
{
i1=i;
j1=j;
}
if(txt2[i11+1]==alph[i][j])
{
i2=i;
j2=j;
}
}
}
if(i1==i2)
{
if((j1+1)>=5) j1=-1;
if((j2+1)>=5) j2=-1;
//if(j2>j1)
//{
txt2[i11]=alph[i1][j1+1];
txt2[i11+1]=alph[i2][j2+1];
//}
//else
//{
// txt2[i11]=alph[i1][j2+1];
// txt2[i11+1]=alph[i2][j1+1];
//}
}
else if(j1==j2)
{
if((i1+1)>=5) i1=-1;
if((i2+1)>=5) i2=-1;

//if(i1>i2)
//{
// txt2[i11]=alph[i1+1][j1];
// txt2[i11+1]=alph[i2+1][j2];
//}
//else
//{
txt2[i11]=alph[i1+1][j1];
txt2[i11+1]=alph[i2+1][j2];
//}
}
else
{
if(i2>i1)
{
txt2[i11+1]=alph[i2][j1];
txt2[i11]=alph[i1][j2];
}
else
{
txt2[i11]=alph[i2][j1];
txt2[i11+1]=alph[i1][j2];
}
}
}
printf("\nFinal string       : ");
for(i=0;i<jj;i++)
{
printf("%c",txt2[i]);
}
//printf("\nFinal string         : %s",txt2);
getch();
}



/* OUTPUT :

Enter the text : playfair

p l a y f
i r b c d
e g h k m
n o q s t
u v w x z

Enter the plain text : nisargmehtay

Final String : ueyqgoegmqfp 

OUTPUT Sequence :

ni = ue


p l a y f
i r b c d
e g h k m
n o q s t
u v w x z


sa = yq



p l a y f
i r b c d
e g h k m
n o q s t
u v w x z

rg = go


p l a y f
i r b c d
e g h k m
n o q s t
u v w x z

me = eg


p l a y f
i r b c d
e g h k m
n o q s t
u v w x z


ht = mq


p l a y f
i r b c d
e g h k m
n o q s t
u v w x z

ay = fp


p l a y f
i r b c d
e g h k m
n o q s t
u v w x z

Hill Cipher in C

Just paste this program in your Turbo C/C++ compiler...



#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,ans[25][1],sum=0,mtrx[25][25],end;
char txt[25];
clrscr();
printf("\nEnter the string : ");
scanf("%s",txt);
//clrscr();
for(i=0;i<25;i++)
{
if(txt[i]>=97 && txt[i]<122) {}
else
{
end=i;
break;
}
}
for(i=0;i<end;i++)
{
//printf("initial : %d ",txt[i]);
txt[i]=txt[i]-'a';
//printf("final : %d ",txt[i]);
//printf("\n\n");
}
clrscr();
printf("\nEnter matrix...\n");
for(i=0;i<end;i++)
{
for(j=0;j<end;j++)
{
scanf("%d",&mtrx[i][j]);
}
}
for(i=0;i<end;i++)
{
sum=0;
for(j=0;j<end;j++)
{
sum+=mtrx[i][j]*(int)txt[j];
}
ans[i][0]=sum;
}
for(i=0;i<end;i++)
{
printf(" %c",((ans[i][0])%26)+97);
}
getch();
}