UVa 12101 - Prime Path

/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.

*/

// Accepted

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back

typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;

const int mod = 1000007;
const int high = 10003;

int prime[(high >> 6) + 1] , cost[high] , st , desti;
vii prm , adj[high];
bool visited[high];

#define setbit(n) (prime[n >> 6] |= (1 << ( (n >> 1) & 31 )))
#define checkbit(n) (prime[n >> 6] & (1 << ( (n >> 1) & 31 )))
#define qrt 317

void sieve()
{
    int i , j;

    for(i=3; i<=qrt; i+=2)
    {
        if(!checkbit(i))
        {
            for(j=i*i; j<high; j+=(2*i))
            {
                setbit(j);
            }
        }
    }

    for(i=3; i<high; i+=2)
    {
        if(!checkbit(i) && (i>999 && i<high))
        {
            prm.push_back(i);
        }
    }

    //for(i=0; i<100; i++) cout << prm[i] << "; ";
}

void init()
{
    int x , y , i , j , plen = prm.size() , op1 , op2 , cnt = 0;

    for(i=0; i<plen; i++)
    {
        x = prm[i];

        for(j=0; j<plen; j++)
        {
            y = prm[j];

            op1 = x;
            op2 = y;
            cnt = 0;

            while(op1 && op2)
            {
                if((op1 % 10) != (op2 % 10)) cnt++;

                op1/=10;
                op2/=10;
            }

            if(cnt == 1) adj[x].psb(y);
        }
    }

//    for(i=0; i<plen; i++)
//    {
//        x = prm[i];
//
//        cout << x << ": ";
//
//        for(j=0; j<adj[x].size(); j++)
//        {
//            cout << adj[x][j] << "; ";
//        }
//    }
}

int BFS(int s)
{
    int u , v , i;

    if(s == desti) return 1;

    visited[s] = true;
    cost[s] = 1;

    queue<int>Q;
    Q.push(s);

    while(!Q.empty())
    {
        u = Q.front();

        Q.pop();

        for(i=0; i<adj[u].size(); i++)
        {
            v = adj[u][i];

            if(!visited[v])
            {
                Q.push(v);
                cost[v] = cost[u] + 1;
                visited[v] = true;
            }

            if(v == desti) return cost[v];
        }
    }

    return -1;
}

int main()
{
    fast;
    sieve();
    init();
    int test;
    cin >> test;
    while(test--)
    {
        mset(cost , 0);
        mset(visited , false);

        cin >> st >> desti;

        if(st > desti) swap(st , desti);

        int ans = BFS(st);

        if(ans > -1) cout << ans-1 << "\n";
        else cout << "Impossible\n";
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number