Friday 15 January 2016

Solution of URI 1855 :: Maester's Map


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
#include <string.h>
int main()
{
    int m,n,i,j,mn,x=0,k=0,l=0,M,G;
    char fao;
    scanf("%d%d", &m, &n);
    char image[n][m+1];

    for(i=0; i<n; i++)
        scanf("%s", image[i]);
    mn=m*n;
    for(M=1; M<=mn; )
    {
        if(image[k][l]=='>')
        {
            l++; M++; G=1;
            if(l>=m) break;
        }
        else if(image[k][l]=='v')
        {
            k++; M++; G=2;
            if(k>=n) break;
        }
        else if(image[k][l]=='<')
        {
            l--; M++; G=3;
            if(l<0) break;
        }
        else if(image[k][l]=='^')
        {
            k--; M++; G=4;
            if(k<0) break;
        }
        else if(image[k][l]=='*')
        {
            M=1000;
            break;
        }
        else if(image[k][l]=='.')
        {
            if(G==1)
            {
                l++; M++;
                if(l>=m) break;
            }
            else if(G==2)
            {
                k++; M++;
                if(k>=n) break;
            }
            else if(G==3)
            {
                l--; M++;
                if(l<0) break;
            }
            else if(G==4)
            {
                k--; M++;
                if(k<0) break;
            }
        }
    }
    if(M==1000) printf("*\n");
    else printf("!\n");

    return 0;
}

1 comment:

  1. bro bangla e akto explain kora jabe je ki chaise problem ta te

    ReplyDelete

Note: only a member of this blog may post a comment.