Wednesday, August 29, 2012

Grid Traversal

There is a rectangular grid of size m * n . Bob is in location ( x, y ) inside grid. He can move in 4 directions up, down, right and left. He will die if he steps outside of rectangular grid. Find the probability that bob is alive given initial position of bob as ( x, y ) and number of steps he moves as N. (Given that Bob moves only one step at a time).

#include<iostream>
using namespace std;
int main()
{
    void fun(int,int,int,int,float*,float*,int,int);
    int m,n,x,y,N;
    float a=0.0,b=0.0,*p=&a,*q=&b;
    cin>>m>>n>>x>>y>>N;
    fun(m,n,x,y,p,q,0,N);
    cout<<(*q)/((*q)+(*p));
}
void fun(int m,int n,int x,int y,float *p,float *q,int S,int N)
{    
    if(S==N && x>=0 && y>=0 && x<m && y<n)
    {
        (*q)++;
        return;
    }
    else if((S<=N) && (x<0 || y<0 || x>=m || y>=n))
    {
        (*p)++;
        return;
    }
    else
    {
        fun(m,n,x+1,y,p,q,S+1,N);
        fun(m,n,x-1,y,p,q,S+1,N);
        fun(m,n,x,y+1,p,q,S+1,N);
        fun(m,n,x,y-1,p,q,S+1,N);
    }
}

Wednesday, August 22, 2012

Excel column header generator


#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char out[27]={'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',0};
char ans[100]={0};
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int j=0;
while(n>0)
{
int c=n%strlen(out);
//Using the remainder as the index of the character array.
ans[j]=out[c];
if(c==0)
{
n--;
}
n/=strlen(out);
j++;
}
//Reversing the answer string.
for(int i=0;i<strlen(ans)/2;i++)
{
char t=ans[i];
ans[i]=ans[strlen(ans)-i-1];
ans[strlen(ans)-i-1]=t;
}
cout<<ans<<endl;
}
return 0;
}

Sample Input: The first line is the number of test cases.
7
1
25
26
27
51
52
676

Sample Output:
A
Y
Z
AA
AY
AZ
YZ

Tuesday, August 7, 2012

C++ Program for Quicksort


#include<iostream>
using namespace std;
int main()
{
int A[7]={3,6,1,2,7,5,4};
void quicksort(int[],int,int);
quicksort(A,0,6);
for(int i=0;i<7;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
return 0;
}
void quicksort(int A[7],int low,int high)
{
if(low>=high)
{
return;
}
int p=A[low];
int i=low+1;
int j=high;
while(j>=i)
{
while(A[i]<=p)
{
i++;
}
while(A[j]>p)
{
j--;
}
if(j>i)
{
int t=A[i];
A[i]=A[j];
A[j]=t;
}
}
int t=A[j];
A[j]=A[low];
A[low]=t;
quicksort(A,low,j-1);
quicksort(A,j+1,high);
}

Monday, August 6, 2012

C++ Program for Mergesort


#include<iostream>
using namespace std;
int main()
{
void mergesort(int[],int,int);
int A[7]={1,2,5,3,7,6,4};
int low=0,high=6;
mergesort(A,0,6);
for(int i=low;i<=high;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
return 0;
}
void mergesort(int A[7],int low,int high)
{
if(low==high)
{
return;
}
int mid=(low+high)/2;
mergesort(A,low,mid);
mergesort(A,mid+1,high);
int a=low;
int b=mid+1;
int B[100],j=0;
while(a<=mid && b<=high)
{
if(A[a]<A[b])
{
B[j]=A[a];
j++;
a++;
}
else if(A[b]<A[a])
{
B[j]=A[b];
j++;
b++;
}
}
while(a<=mid)
{
B[j]=A[a];
j++;
a++;
}
while(b<=high)
{
B[j]=A[b];
j++;
b++;
}
int k=0;
for(int i=low;i<=high;i++)
{
A[i]=B[k];
k++;
}
}