Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE
Showing posts with label DFS. Show all posts
Showing posts with label DFS. Show all posts

Monday, August 17, 2015

COMCB-Complete Chess Boards

Complete Chess Boards

Given below c++11 code is for COMCB spoj or Complete Chess Boards spoj.

Hint :- dfs with backtracking ,
for each node apply DFS and check if traversal of chess board is possible or not . Inside send child node in lexicographical order so that final path will be lexicographically shortest.


/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 17 August 2015 (Monday) 04:58
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define mod 1000000009
int x[8] = {-1,1,-2,2,-2,2,-1,1};
int y[8] = {-2,-2,-1,-1,1,1,2,2};
int n , m;
bool valid(int row , int col){
    if(row>=0 && row< n && col >=0 && col < m)
        return true;
    return false;
}
bool dfs(vector<string> path ,int row ,int col , int count, int check[][30]){
    string temp = "";
    char c = (char)(col + 65);
    temp.pb(c);
    temp.append(to_string(row+1));
    path.pb(temp);
    if(count == m*n){
        for(int i = 0 ; i < path.size() ; i++)
            cout<<path[i];
        cout<<endl;
        return true;
    }
    int p , q;
    for(int i = 0 ; i < 8 ; i++){
        p = row + x[i] , q = col + y[i];
        if(valid(p , q) && !check[p][q]){
            check[p][q] = 1;
            if(dfs( path , p , q , count+1 , check)) return true;
            check[p][q] = 0;
        }
    }
    return false;
}
bool path(){
    for(int i = 0 ; i < n; i ++){
        for(int j = 0 ; j < m ; j++){
            int check[30][30];
            memset(check , 0 , sizeof check);
            check[i][j] = 1;
            vector<string> path;
            if(dfs(path , i , j ,1 , check )) return true;
        }
    }
    return false;
}
int main(){ 
     
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        if(!path()) cout<<-1<<endl;
    }
    
    return 0;
}

Sunday, August 9, 2015

HERDING-Herding

Herding

Given below c++ code is for HERDING spoj or Herding spoj.

Simple BFS with some backtracking. Also can be easily done with DFS.




/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 09 August 2015 (Sunday) 03:27
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mod 1000000009
char opposite[200];
bool valid(int x , int y , int n , int m){
    if(x >=0 && y>=0 && y< m && x<n)
        return true;
    return false;
}
int main(){
    int n , m;
    scanf("%d%d",&n , &m);
    char arr[n+9][m+9];
    for(int i = 0 ; i < n ; i++)
        scanf("%s",arr[i]);
    int count = 0 , check[n+9][m+9];
    memset(check , 0 , sizeof check);
    int prev = 0 , color=0;
    for(int i = 0 ; i < n ; i++){
        for(int j = 0 ; j < m ; j++){
            queue<pii >q;
            q.push(mp(i , j));
            vector<pii > path;
            path.pb(mp(i , j));
            if(!check[i][j]){
                color = prev + 1;
                check[i][j] = color;
                while(!q.empty()){
                    pii pos = q.front();
                    q.pop();
                    int x = pos.first , y = pos.second;
                    if(arr[x][y] == 'S')
                        x++;
                    else if(arr[x][y] == 'E')
                        y++;
                    else if(arr[x][y] == 'N')
                        x--;
                    else if(arr[x][y] == 'W')
                        y--;
                    if(valid(x , y , n , m) && check[x][y] == 0){
                        path.pb(mp(x , y));
                        q.push(mp(x , y));
                        check[x][y] = color;
                    } else if(valid(x , y , n , m) && check[x][y]!=0){
                        int save = check[x][y];
                        if(check[x][y] < color){
                            for(int p = 0 ; p < path.size() ; p++)
                                check[path[p].first][path[p].second] = check[x][y];
                        }
                        color =save;
                        break;
                    }
                }
                if(color>prev)
                    prev++;
            }
        }
    }
    cout<<prev<<endl;
    return 0;
}

Sunday, January 11, 2015

NAKANJ-Minimum Knight moves !!!

Minimum Knight moves !!!

Given below code is for nakanj spoj or minimum knight moves spoj.


Question is same as costly chess(CCHESS) problem of spoj.




#include <bits/stdc++.h>
using namespace std;
#define MP make_pair
int dist[100][100];
map<int , pair<int , int > > mp;
map<pair<int , int > , int > m;
void fill_distance(int v){
    int path[8][2];
    int x = mp[v].first , y = mp[v].second;
    path[0][0] = x+2 , path[0][1] = y+1;
    path[1][0] = x+2 , path[1][1] = y-1;
    path[2][0] = x-2 , path[2][1] = y+1;
    path[3][0] = x-2 , path[3][1] = y-1;
    path[4][0] = x+1 , path[4][1] = y+2;
    path[5][0] = x+1 , path[5][1] = y-2;
    path[6][0] = x-1 , path[6][1] = y+2;
    path[7][0] = x-1 , path[7][1] = y-2;

    for(int i = 0 ; i < 8 ; i ++)
    {
        if(path[i][0] >= 0 && path[i][0] < 8 && path[i][1] >= 0 && path[i][1] < 8){
            dist[v][m[MP(path[i][0] , path[i][1])]] = 1;
        }
    }
}
void pre(){
    int k = 0;
    for(int i = 0 ; i < 64 ; i++)
        for(int j = 0 ; j < 64 ; j++){
            dist[i][j] = 9999999;
            dist[j][j] = 0;
        }
    for(int i = 0 ; i<64 ; i++){
        fill_distance( i);
    }
    for(int k = 0 ; k < 64 ; k++)
        for(int i = 0 ; i < 64 ; i++)
            for(int j = 0 ; j < 64 ; j++)
                    dist[i][j] = min(dist[i][j] , dist[i][k] + dist[k][j]);
}
int main()
{
    int k = 0;
    for(int i = 0 ; i < 8 ; i++)
        for(int j = 0 ; j < 8 ; j++){
            mp[k] = MP(i , j);
            m[MP(i , j)] = k;
            k++;
        }
    pre();
    int t;
    scanf("%d ",&t);
    while(t--){
        char inp[10];
        gets(inp);
        int x, y , u ,v;
        x = inp[0] - 97;
        y = inp[1] - 49;
        u = inp[3] - 97;
        v = inp[4] - 49;
        int source = m[MP(x,y)], dest = m[MP(u,v)];
        printf("%d\n",dist[source][dest]) ;
    }
    return 0;
}

CCHESS-COSTLY CHESS

COSTLY CHESS

Given below code is for cchess spoj or costly chess spoj.



In this question I apply Floyd Warshall's algorithm to find the minimum cost between all pair of blocks of chess, and then for given input I just print out that value from table.




#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MP make_pair
LL dist[100][100];
map<int , pair<int , int > > mp;
map<pair<int , int > , int > m;
void fill_distance(int v){
    int path[8][2];
    int x = mp[v].first , y = mp[v].second;
    path[0][0] = x+2 , path[0][1] = y+1;
    path[1][0] = x+2 , path[1][1] = y-1;
    path[2][0] = x-2 , path[2][1] = y+1;
    path[3][0] = x-2 , path[3][1] = y-1;
    path[4][0] = x+1 , path[4][1] = y+2;
    path[5][0] = x+1 , path[5][1] = y-2;
    path[6][0] = x-1 , path[6][1] = y+2;
    path[7][0] = x-1 , path[7][1] = y-2;

    for(int i = 0 ; i < 8 ; i ++)
    {
        if(path[i][0] >= 0 && path[i][0] < 8 && path[i][1] >= 0 && path[i][1] < 8){
            dist[v][m[MP(path[i][0] , path[i][1])]] = x*path[i][0] + y*path[i][1];
        }
    }
}
void pre(){
    int k = 0;
    for(int i = 0 ; i < 64 ; i++)
        for(int j = 0 ; j < 64 ; j++){
            dist[i][j] = INT_MAX;
            dist[j][j] = 0;
        }
    for(int i = 0 ; i<64 ; i++){
        fill_distance( i);
    }
    for(int k = 0 ; k < 64 ; k++)
        for(int i = 0 ; i < 64 ; i++)
            for(int j = 0 ; j < 64 ; j++)
                    dist[i][j] = min(dist[i][j] , dist[i][k] + dist[k][j]);
}
int main()
{
    int k = 0;
    for(int i = 0 ; i < 8 ; i++)
        for(int j = 0 ; j < 8 ; j++){
            mp[k] = MP(i , j);
            m[MP(i , j)] = k;
            k++;
        }
    pre();
    int x, y , u ,v;
    while(scanf("%d%d%d%d",&x , &y , &u ,&v)!=EOF){
        int source = m[MP(x,y)], dest = m[MP(u,v)];
        dist[source][dest] == INT_MAX ? printf("-1\n") : printf("%lld\n",dist[source][dest]) ;
    }
    return 0;
}

Tuesday, August 19, 2014

PT07Z-Longest path in a tree

Longest path in a tree

Given below code is for PTZ07Z spoj or Longest path in a tree spoj.

You can solve this using DFS of applying BFS twice.

For BFS twice 
In first bfs you have to find maximum length of node from root then in second bfs consider that node as root and find maximum distance from that .That will be our answer.

1-> Using DFS.

#include <bits/stdc++.h>
using namespace std;
#define MAX 100009
bool check[MAX]={false};
int total=0;
int dfs(vector<int> v[],int root)
{
    int m,m1=-1,m2=-1;
    check[root]=1;
    for(int i=0;i<v[root].size();i++)
    {
        if(!check[v[root][i]]){
            m = dfs(v,v[root][i]);
            if(m>=m1)
            {
                m2= m1;
                m1 = m;
            }
            else if(m>m2)
                m2=m;
        }
    }
    total = max(total , m1+m2+2);
    return (m1 + 1);
}
int main()
{
    int n,a,b;
    cin>>n;
    vector<int> v[n+9];
    for(int i=0;i<n-1;i++){
        scanf("%d%d",&a,&b);
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(v,1);
    cout<<total<<endl;
}

1-> Using Double BFS .



#include <bits/stdc++.h>
using namespace std;
#define MAXN 10000
vector < int > g[MAXN + 1];
int maxWt[MAXN + 1];
bool check[MAXN + 1];
void bfs(int n)
{
    queue <pair<int, int> > q;
    q.push(make_pair(n, 0));
    while (!q.empty())
    {
        int root = q.front().first;
        int wt = q.front().second;
        check[root] = true;
        for(int i = 0; i<g[root].size(); i++)
        {
            if (!check[g[root][i]])
            {
                q.push(make_pair(g[root][i], wt + 1));
            }
        }
        maxWt[root] = wt;
        q.pop();
    }
}
int main()
{
    int n,a,b;
    cin>>n;
    vector<pair<int,int> > v[n+9];
    for(int i=0;i<n-1;i++)
    {
        cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    bfs(1);
    int maxRoot = 0;
    for(int i = 1; i<= n; i++)
        maxRoot = maxWt[maxRoot] < maxWt[i] ? i : maxRoot; 
    memset(maxWt, 0, sizeof(maxWt));
    memset(check, 0, sizeof(check));
    bfs(maxRoot);
    maxRoot = 0;
    for(int i = 1; i<= n; i++)
        maxRoot = max(maxRoot, maxWt[i]);
    cout<<maxRoot<<endl;
    return 0;
}

Monday, August 18, 2014

PARADOX-Paradox

Paradox

Given below code is for PARADOX spoj.

Hint:- DFS
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while (1)
    {
        scanf("%d",&n);
        if (n==0)
            return  0;
        char type[10];
        int array[n+1],actual[n+1],flag=0;
        
        for (int i=1; i<n+1; i++)
        {
            scanf("%d %s",&array[i],type);
            if (type[0]=='f')
                actual[i]=0;
            else
                actual[i]=1;
        }
        for (int i=1; i<=n; i++)
        {
            bool ans[n+1],mark[n+1];
            memset(ans,0,sizeof(ans));
            memset(mark,0,sizeof(ans));
            bool save;
           // if (mark[i]==0)
            {
                mark[i]=1;
                ans[i]=1;
                int temp=array[i];
                bool temp1=ans[i];
                save=actual[i];
                while (!mark[temp])
                {
                    if (temp1==1)
                        ans[temp]=save;
                    else
                        ans[temp]=!save;

                    mark[temp]=1;
                    temp1=ans[temp];
                    save=actual[temp];
                    temp=array[temp];
                }
                if (temp1==1 && save!=ans[temp])
                    flag=1;
                else if(temp1==0 && save==ans[temp])
                    flag=1;
            }
            if (flag==1)
                break;
        }
        if (flag==0)
            printf("NOT PARADOX\n");
        else
            printf("PARADOX\n");
    }
}