UVa 429 - Word Transformation

/*
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 = 205;

vs dic;
int diclen=0;
mpsi visited , cost;
int cnt = 0;

int BFS(string a, string b)
{
    visited[a] = 1;
    cost[a] = 0;
    //cout << "bfs = " << a ;
    diclen = dic.size();

    string u , v;

    queue<string>Q;
    Q.push(a);

    while(!Q.empty())
    {
        u = Q.front();
        //cout << "U = " << u << "\n";

         if(u == b) return cost[u];

//        if(u == b)
//        {
//            cost[u] = cost[u]+1;
//            return;
//        }

        Q.pop();

        for(int i=0; i<diclen; i++)
        {
            v = dic[i];
            //cout << "v = " << v << "; ";

            if(u.length() == v.length() && visited[v] == 0)
            {
                int miss = 0;

                for(int j=0; j<u.length(); j++)
                {
                    if(u[j] != v[j]) miss+=1;
                }

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

int main()
{
    int test;
    bool nline=false;
    string s;
    cin >> test;
    while(test--)
    {
        while(cin >> s && s!="*")
        {
            dic.push_back(s);
        }

        string mstr;

        if(nline) cout << "\n";

        nline = true;

        getline(cin, mstr);

        while(getline(cin, mstr))
        {
            if (mstr=="") break;
            //outn(tmp);
            cnt = 0;
            int space = 0;
            visited.clear();
            cost.clear();

            for(int i=0; i<mstr.length(); i++)
            {
                if(mstr[i] == ' ')
                {
                    space = i;
                    break;
                }
            }

            string start = mstr.substr(0, space);
            string desti = mstr.substr(space+1, mstr.length());
            //cout << start << "-" << desti << "\n";
            cout << start << " " << desti;

            int ans = BFS(start , desti);

            cout << " " << ans << "\n";
//            for(int i=0; i<diclen; i++)
//            {
//                cout << cost[dic[i]] << "; ";
//            }
        }
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number