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

Sunday, January 11, 2015

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;
}

No comments:

Post a Comment

Your comment is valuable to us