Sunday, 31 January 2016

The Great Wall Game (UVA 1045, UVALive 3276, World Finals >> 2005 - Shanghai)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100000

int dist[16][16][16], n, dp[(1<<15)+2][16];
vector<int>xx, yy;

void calculateDist(int pos, int x, int y)
{
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        {
            dist[pos][i][j] = abs(x-i)+abs(y-j);
            //cout<<pos<<" "<<i<<" "<<j<<" "<<dist[pos][i][j]<<endl;
        }
return;
}

int func(int mask, int pos)
{
    if(pos>=n) return 0;

    int &ret=dp[mask][pos];
    if(ret!=-1) return ret;

    ret=1e9;
    for(int i=0;i<n;i++)
    {
        if(!(mask & (1<<i)))
        {
            ret = min(ret, dist[i][xx[pos]][yy[pos]]+func(mask | (1<<i), pos+1));
        }
    }
return ret;
}


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, x, y, mn;
    while(cin>>n && n)
    {
        mn=1e9;
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            x--, y--;
            calculateDist(i, x, y);
        }
        //row
        for(int row=0;row<n;row++)
        {
            xx.clear();
            yy.clear();
            for(int col=0;col<n;col++)
            {
                xx.push_back(row);
                yy.push_back(col);
            }
            SET(dp);
            mn=min(mn, func(0, 0));
            //cout<<mn<<endl;
        }
        //col
        for(int col=0;col<n;col++)
        {
            xx.clear();
            yy.clear();
            for(int row=0;row<n;row++)
            {
                xx.push_back(row);
                yy.push_back(col);
            }
            SET(dp);
            mn=min(mn, func(0, 0));
            //cout<<mn<<endl;
        }
        //diagonal1
        xx.clear();
        yy.clear();
        for(int row=0, col=0;row<n;row++, col++)
        {
            xx.push_back(row);
            yy.push_back(col);
        }
        SET(dp);
        mn=min(mn, func(0, 0));
        //diagonal2
        xx.clear();
        yy.clear();
        for(int row=0, col=n-1;row<n;row++, col--)
        {
            xx.push_back(row);
            yy.push_back(col);
        }
        SET(dp);
        mn=min(mn, func(0, 0));

        cout<<"Board "<<kk++<<": "<<mn<<" moves required.\n\n";
    }
return 0;
}

File Input Output sample program C/C++

#include <stdio.h>

int main ()
{
  freopen ("numbers.txt","r",stdin);
  freopen ("summation.txt","w",stdout);
  int a, b, sum;
  scanf("%d%d", &a, &b);
  sum = a+b;
  printf("%d\n", sum);
  return 0;
}

Wednesday, 20 January 2016

Seinfeld (POJ 3991, UVALive 4733, HDU 3351, SPOJ ANARC09A, Regionals 2009 >> Africa/Middle East - Africa and Arab)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100000


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    string s;
    while(cin>>s)
    {
        if(s[0]=='-') break;
        int sl=s.size(), balance=0, ans=0;
        for(int i=0;i<sl;i++)
        {
            if(s[i]=='}') balance--;
            else balance++;

            if(balance==-1)
            {
                ans++;
                balance=1;
            }
        }
        ans+=balance/2;
        cout<<kk++<<". "<<ans<<"\n";
    }

return 0;
}

Jollo (UVA 12247, UVALive 4814, Regionals 2010 >> Latin America)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100000


int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, girl[5], boy[5];
    while(cin>>girl[0]>>girl[1]>>girl[2]>>boy[0]>>boy[1])
    {
        if(girl[0]==0) break;
        sort(girl, girl+3);
        sort(boy, boy+2);

        if(boy[1]>girl[2] && boy[0]>girl[2])
        {
            int lowest = 1;
            while(boy[0]==lowest || boy[1]==lowest || girl[0]==lowest || girl[1]==lowest || girl[2]==lowest)
                lowest++;
            if(lowest>52) lowest=-1;

            cout<<lowest<<"\n";
        }
        else if(boy[1]>girl[2])
        {
            int lowest;
            if(boy[0]<girl[1])
                lowest = girl[2]+1;
            else lowest = girl[1]+1;
            while(boy[0]==lowest || boy[1]==lowest || girl[0]==lowest || girl[1]==lowest || girl[2]==lowest)
                lowest++;
            if(lowest>52) lowest=-1;
            cout<<lowest<<"\n";
        }
        else if(boy[0]>girl[1])
        {
            int lowest = girl[1]+1;
            while(boy[0]==lowest || boy[1]==lowest || girl[0]==lowest || girl[1]==lowest || girl[2]==lowest)
                lowest++;
            if(lowest>52) lowest=-1;
            cout<<lowest<<"\n";
        }
        else cout<<"-1\n";
    }

return 0;
}

Palindrom Numbers (UVALive 2389, ZOJ 1078, Regionals 2001 >> Latin America - South America)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100000

/*long long toDecimal(string s,int baseFrom)
{
    int cnt=0;
    long long res=0;
    for(int i=s.length()-1;i>=0;i--)
    {
        if ( s[i] > 47 && s[i] < 58 ) res+= (pow(baseFrom,cnt)*(s[i]-'0'));
        else  res+= (pow(baseFrom,cnt)*(s[i]-55));
        cnt++;
    }
    return res;
}*/

string deciamlTo(int num, int baseTo)
{
    string s="";
    while(num)
    {
        int tmp=num%baseTo;
        if(tmp<10)   s+=tmp+'0';
        else   s+= char (tmp+55);
        num/=baseTo;
    }
    if(s=="")    return "0";
    reverse(s.begin(), s.end());
    return s;
}

bool isPalindrome(string s)
{
    int sl=s.size()-1, st=0;
    while(st<sl)
    {
        if(s[st]!=s[sl]) return false;
        st++;
        sl--;
    }
    return true;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    string tmp;
    while(cin>>n && n)
    {
        vector<int>v;
        for(int i=2;i<=16;i++)
        {
            tmp = deciamlTo(n, i);
            if(isPalindrome(tmp))
                v.push_back(i);
        }
        if(v.size()==0) cout<<"Number "<<n<<" is not palindrom\n";
        else
        {
            cout<<"Number "<<n<<" is palindrom in basis";
            for(int i=0;i<v.size();i++)
                cout<<" "<<v[i];
            cout<<"\n";
        }
    }

return 0;
}