Saturday 2 May 2015

UVA 10010 Where's Waldorf?

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

string grid[55], word;
int m, n;
bool vis[55][55];

bool exist(int row, int col)
{
    int sl=word.size(), x, y, pos;
    //up
    x=row;
    y=col;
    pos=0;
    while(x>=0 && pos<sl && grid[x][y]==word[pos])
    {
        pos++;
        x--;
        if(pos==sl)
        {
            return true;
        }
    }
    //down
    x=row;
    y=col;
    pos=0;
    while(x<m && pos<sl && grid[x][y]==word[pos])
    {
        pos++;
        x++;
        if(pos==sl)
        {
            return true;
        }
    }
    //left
    x=row;
    y=col;
    pos=0;
    while(y>=0 && pos<sl && grid[x][y]==word[pos])
    {
        pos++;
        y--;
        if(pos==sl)
        {
            return true;
        }
    }
    //right
    x=row;
    y=col;
    pos=0;
    
    while(y<n && pos<sl && grid[x][y]==word[pos])
    {

        pos++;
        y++;
        if(pos==sl)
        {
            return true;
        }
    }
    //left-up
    x=row;
    y=col;
    pos=0;
    while(x>=0 && y>=0 && pos<sl && grid[x][y]==word[pos])
    {
        pos++;
        x--;
        y--;
        if(pos==sl)
        {
            return true;
        }
    }
    //right-down
    x=row;
    y=col;
    pos=0;
    while(x<m && y<n && pos<sl && grid[x][y]==word[pos])
    {
        pos++;
        x++;
        y++;
        if(pos==sl)
        {
            return true;
        }
    }
    //left-down
    x=row;
    y=col;
    pos=0;
    while(x<m && y>=0 && pos<sl && grid[x][y]==word[pos])
    {
        pos++;
        x++;
        y--;
        if(pos==sl)
        {
            return true;
        }
    }
    //right-up
    x=row;
    y=col;
    pos=0;
    while(x>=0 && y<n && pos<sl && grid[x][y]==word[pos])
    {
        pos++;
        x--;
        y++;
        if(pos==sl)
        {
            return true;
        }
    }
return false;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, k;
    string s;
    cin>>tc;
    while(tc--)
    {
        cin>>m>>n;
        for(int i=0;i<m;i++)
        {
            cin>>grid[i];
            for(int j=0;j<n;j++)
                grid[i][j]=tolower(grid[i][j]);
            //cout<<grid[i]<<endl;
        }
        cin>>k;
        while(k--)
        {
            cin>>word;
            for(int i=0;i<word.size();i++)
                word[i]=tolower(word[i]);
            //cout<<word<<endl;
            bool chk=false;
            int r, c;
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++)
                    if(word[0]==grid[i][j])
                        if(exist(i, j))
                        {
                            r=i+1;
                            c=j+1;
                            chk=true;
                            break;
                        }
                if(chk) break;
            }
            if(chk) cout<<r<<" "<<c<<"\n";
        }
        if(tc) cout<<"\n";

    }
return 0;
}

No comments:

Post a Comment