Light OJ : 1238 – Power Puff Girls

0

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1238


/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long


using namespace std;

const int fx[]={+1,-1,+0,+0};
const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

bool visited[30][30];
int d[30][30];

void BFS(pii x)
{
    d[x.ff][x.ss]=0;
    queue<pii>Q;
    visited[x.ff][x.ss]=0;
    Q.push(x);
    while(!Q.empty())
    {
        pii u=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            pii v;
            v.ff=u.ff+fx[i];
            v.ss=u.ss+fy[i];
            if(!visited[v.ff][v.ss])
            {
                visited[v.ff][v.ss]=1;
                d[v.ff][v.ss]=d[u.ff][u.ss]+1;
                Q.push(v);
            }
        }
    }
}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t,m,n;
    sf(t);
    TEST_CASE(t)
    {
        sff(m,n);
        ms(visited,0);
        ms(d,0);
        pii a,b,c,h;
        char ch;
       // getchar();
        REP(i,0,m)
            REP(j,0,n)
                {
                    cin>>ch;
                    //getchar();
                    if(ch=='#' || ch=='m')
                        visited[i][j]=1;
                    else if(ch=='h')
                        h.ff=i,h.ss=j;
                    else if(ch=='a')
                        a.ff=i,a.ss=j;
                    else if(ch=='b')
                        b.ff=i,b.ss=j;
                    else if(ch=='c')
                        c.ff=i,c.ss=j;
                }
        BFS(h);
        int ans=d[a.ff][a.ss];
        ans=max(ans,d[b.ff][b.ss]);
        ans=max(ans,d[c.ff][c.ss]);
        PRINT_CASE;
        pf("%d\n",ans);
    }
    return 0;
}

Advertisements

Light OJ 1174 – Commandos

0

Problem Link: http://www.lightoj.com/volume_showproblem.php?problem=1174



/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             110
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;

int n,m,a,b,u,v;
vector<int>edge[MAX];
bool visited[MAX];
int distance1[MAX];
int distance2[MAX];

void BFS(int src,int d[])
{
    queue<int>Q;
    d[src]=0;
    Q.push(src);
    ms(visited,0);
    visited[src]=1;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        REP(i,0,SZ(edge[u]))
        {
            int v=edge[u][i];
            if(visited[v]==0)
            {
                visited[v]=1;
                Q.push(v);
                d[v]=d[u]+1;
            }
        }
    }
}

void clr()
{
    REP(i,0,n)
    {
        edge[i].clear();
    }
}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    sf(t);
    TEST_CASE(t)
    {
        sff(n,m);
        REP(i,0,m)
        {
            sff(u,v);
            edge[u].pb(v);
            edge[v].pb(u);
        }
        sff(a,b);
        ms(distance1,0);
        ms(distance2,0);
        BFS(a,distance1);
        BFS(b,distance2);
        int ans=-1;
        REP(i,0,n)
        {
            ans=max(ans,distance1[i]+distance2[i]);
        }
        PRINT_CASE;
        pf("%d\n",ans);
        clr();
    }
    return 0;
}

Light OJ 1111 – Best Picnic Ever

0

Problem Link: http://www.lightoj.com/volume_showproblem.php?problem=1111


/*
 +-+ +-+ +-+
 |R| |.| |S|
 +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define db double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define sqr(x) (x)*(x)
#define D(x) cout<<#x " = "<<(x)<<endl
#define VI vector <int>
#define DBG pf("Hi\n")
#define MOD 100007
#define MAX 1010
#define CIN ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a) (int)a.size()
#define sf(a) scanf("%d",&a)
#define sff(a,b) scanf("%d%d",&a,&b)
#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define loop(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define TEST_CASE(t) for(int z=1;z<=t;z++)
#define PRINT_CASE printf("Case %d: ",z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define ull unsigned long long

using namespace std;

vector<int>v[MAX],people;
int dfs_cnt[MAX];
bool visited[MAX];
int n,k,ans;

void clr()
{
 loop(i,n+1)
 {
 v[i].clear();
 dfs_cnt[i]=0;
 }
}

void dfs(int x)
{
 visited[x]=1;
 dfs_cnt[x]++;
 if(dfs_cnt[x]==k)
 ans++;
 loop(i,SZ(v[x]))
 {
 int u=v[x][i];
 if(!visited[u])
 dfs(u);
 }
}

int main()
{
 ///freopen("in.txt","r",stdin);
 ///freopen("out.txt","w",stdout);

 int t,m,temp;
 sf(t);
 TEST_CASE(t)
 {
 sfff(k,n,m);

 loop(i,k)
 {
 sf(temp);
 people.pb(temp);
 }

 int a,b;

 loop(i,m)
 {
 sff(a,b);
 v[a].pb(b);
 }

 ans=0;

 loop(j,SZ(people))
 {
 loop(i,n+2)
 visited[i]=0;
 dfs(people[j]);
 }

 PRINT_CASE;
 pf("%d\n",ans);
 clr();
 people.clear();
 }
 return 0;
}

UVa 11517 – Exact Change

0

Problem Link : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2512

Idea:
1. This is a classical coin change problem. here dp array represent the minimum coin need to make an amount. example – dp[100] means we need at least dp[100] coins to make 100 cents.

2. At first calculate the dp[] table as described. then start a linear search from money to forward for finding first amount of money which value is not infinity as we set before dp calculation and when we find a such value that value is the payable money and dp value of that money is the minimum number of coins.


/*
MMP""MM""YMM   db      `7MN.   `7MF'`7MMM.     ,MMF' .g8""8q.`YMM'   `MM'
P'   MM   `7  ;MM:       MMN.    M    MMMb    dPMM .dP'    `YM.VMA   ,V
     MM      ,V^MM.      M YMb   M    M YM   ,M MM dM'      `MM VMA ,V
     MM     ,M  `MM      M  `MN. M    M  Mb  M' MM MM        MM  VMMP
     MM     AbmmmqMA     M   `MM.M    M  YM.P'  MM MM.      ,MP   MM
     MM    A'     VML    M     YMM    M  `YM'   MM `Mb.    ,dP'   MM
   .JMML..AMA.   .AMMA..JML.    YM  .JML. `'  .JMML. `"bmmd"'   .JMML.
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define CIN ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a) (int)a.size()
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d%d",&a,&b)
#define siii(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define loop(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define TEST_CASE(t) for(int z=1;z<=t;z++)
#define PRINT_CASE printf("Case %d: ",z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int


using namespace std;

int dp[20100];

int coin[110];

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    si(t);
    TEST_CASE(t)
    {
        int money;
        si(money);
        int n;
        si(n);
        loop(i,n)
        {
            si(coin[i]);
        }
        //sort(coin,coin+n,test);
        ms(dp,inf);
        dp[0]=0;
        for(int i=0; i<n; i++)
        {
            for(int j=money; j>-1; j--)
            {
                dp[j+coin[i]]=min(dp[j]+1,dp[j+coin[i]]);
            }
        }

        for(int i=money; i<=30100; i++)
        {
            if(dp[i]<10000)
            {
                pf("%d %d\n",i,dp[i]);
                break;
            }
        }
    }
    return 0;
}


Another Solution using same idea. this one is from revise 😛 .



/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

int dp[1000007];
int coin[110];

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);

    int t;
    sf(t);
    TEST_CASE(t)
    {
        ms(dp,1000000);
        int money,n;
        sf(money);
        sf(n);
        loop(i,n) sf(coin[i]);
        dp[0]=0;
        for(int i=0; i<n; i++)
        {
            for(int j=money; j>=0; j--)
            {
                dp[j+coin[i]]=min(dp[j]+1,dp[j+coin[i]]);
            }
        }

        for(int i=money;; i++)
        {
            if(dp[i]<1000000 && dp[i])
            {
                pf("%d %d\n",i,dp[i]);
                break;
            }
        }

    }

    return 0;
}



1049 – One Way Roads

0

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1049


#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define CIN ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a) (int)a.size()
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d%d",&a,&b)
#define siii(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define loop(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define TEST_CASE(t) for(int z=1;z<=t;z++)
#define PRINT_CASE printf("Case %d: ",z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

bool left_pos[110],right_pos[110];

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    si(t);
    TEST_CASE(t)
    {
        int n,a,b,c;
        int left_cost=0,rigt_cost=0;
        si(n);
        ms(left_pos,0);
        ms(right_pos,0);
        loop(i,n)
        {
            siii(a,b,c);
            if(left_pos[a]==0 && right_pos[b]==0)
            {
                left_pos[a]=right_pos[b]=1;
                left_cost+=c;
            }
            else
            {
                right_pos[a]=left_pos[b]=1;
                rigt_cost+=c;
            }
        }
        PRINT_CASE;
        pf("%d\n",min(left_cost,rigt_cost));
    }

    return 0;
}

USACO : Combination Lock

0

Problem Link : http://train.usaco.org/usacoprob2?a=4Hh6zP8GJZV&S=combo


/*
PROG: combo
LANG: C++
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define CIN ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define getint2(a,b) scanf("%d%d",&a,&b)
#define getint3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define loop(i,n) for(int i=0;i<n;i++)
#define TEST_CASE(t) for(int z=1;z<=t;z++)
#define PRINT_CASE printf("Case %d: ",z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;
int n;
bool check1(int a, int b)
{
    if(abs(a-b)<=2) return 1;
    if(abs(a-b)>=n-2) return 1;
    return 0;
}

bool check(int a1,int b1,int c1,int a2,int b2,int c2)
{
    return (check1(a1,a2) && check1(b1,b2) && check1(c1,c2));
}
int main()
{
    freopen("combo.in","r",stdin);
    freopen("combo.out","w",stdout);
    int a,b,c,x,y,z;
    int cnt=0;
    cin>>n;
    cin>>a>>b>>c>>x>>y>>z;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            for(int k=1;k<=n;k++)
                if(check(i,j,k,a,b,c)||check(i,j,k,x,y,z))
                    cnt++;
    cout<<cnt<<endl;
    return 0;
}



USACO: Prime Cryptarithm

0

Problem Link : http://train.usaco.org/usacoprob2?a=dOLJGJAE7bv&S=crypt1


/*
TASK: crypt1
LANG: C++
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define CIN ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define getint2(a,b) scanf("%d%d",&a,&b)
#define getint3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define loop(i,n) for(int i=0;i<n;i++)
#define TEST_CASE(t) for(int z=1;z<=t;z++)
#define PRINT_CASE printf("Case %d: ",z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;
int ara[11];
bool mp[11];

int main()
{
    freopen("crypt1.in","r",stdin);
    freopen("crypt1.out","w",stdout);

    int n;
    cin>>n;
    loop(i,n)
        {
            cin>>ara[i];
            mp[ara[i]]=1;
        }
    int cnt=0;
    loop(a,n)
    loop(b,n)
    loop(c,n)
    {
        int abc=ara[a]*100+ara[b]*10+ara[c];
        if( abc/1000 !=0)
            continue;
        int p1,p2;
        loop(d,n)
        {
            p1=abc*ara[d];
            if( p1/1000 !=0)
            continue;
            int temp1=p1;
            bool test=0;
            while(temp1)
            {
                if(mp[temp1%10]==1)
                {
                    temp1/=10;
                }
                else
                {
                    test=1;
                    break;
                }
            }
            if(test)
                continue;
            loop(e,n)
            {
                p2=abc*ara[e];
                if( p2/1000 !=0)
                continue;
                int temp2=p2;
                bool test=0;
                while(temp2)
                {
                    if(mp[temp2%10]==1)
                    {
                        temp2/=10;
                    }
                    else
                    {
                        test=1;
                        break;
                    }
                }
                if(test)
                    continue;
                int ans=p1+p2*10;
                while(ans)
                {
                    if(mp[ans%10]==1)
                        ans/=10;
                    else
                    {
                        test=1;
                        break;
                    }
                }
                if(test==0)
                    cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}