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