Meduvada: WA

I am attempting this problem: http://opc.iarcs.org.in/index.php/problems/MEDUVADA

And I am getting WA (idk why). My approach is correct but I think there is a small mistake.
My code:

#include<iostream>
#include<queue>
#include<utility>
#include<cstdlib>
using namespace std;

int mod(int a, int b)
{
    if(a < 0) return a+b;
    else return a%b;
}

int main()
{
    int r,c;
    cin >> r >> c;
    char maze[r][c];
    pair<int,int> sp;
    pair<int,int> ep;
    for(int i = 0;i < r;i++)
    {
        for(int j = 0;j < c;j++)
        {
            cin >> maze[i][j];
            if(maze[i][j] == 'M') sp = make_pair(i,j);
            if(maze[i][j] == 'D') ep = make_pair(i,j);
        }
    }
    char path[r][c];
    for(int i = 0;i < r;i++) for(int j = 0;j < c;j++) path[i][j] = '.';
    bool visit[r][c];
    for(int i = 0;i < r;i++) for(int j = 0;j < c;j++) visit[i][j] = false;
    queue< pair<int,int> > bfs;
    bfs.push(sp);
    while(!bfs.empty())
    {
        int x = bfs.front().first;
        int y = bfs.front().second;
        if(x == ep.first && y == ep.second) break;
        bfs.pop();
        int temp1, temp2;
        temp1 = mod(x+1,r);
        temp2 = mod(y,c);
        if(!visit[temp1][temp2] && maze[temp1][temp2] != '#')
        {
            visit[temp1][temp2] = true;
            path[temp1][temp2] = 'u';
            bfs.push(make_pair(temp1,temp2));
        }
        temp1 = mod(x-1,r);
        temp2 = mod(y,c);
        if(!visit[temp1][temp2] && maze[temp1][temp2] != '#')
        {
            visit[temp1][temp2] = true;
            path[temp1][temp2] = 'd';
            bfs.push(make_pair(temp1,temp2));
        }
        temp1 = mod(x,r);
        temp2 = mod(y+1,c);
        if(!visit[temp1][temp2] && maze[temp1][temp2] != '#')
        {
            visit[temp1][temp2] = true;
            path[temp1][temp2] = 'l';
            bfs.push(make_pair(temp1,temp2));
        }
        temp1 = mod(x,r);
        temp2 = mod(y-1,c);
        if(!visit[temp1][temp2] && maze[temp1][temp2] != '#')
        {
            visit[temp1][temp2] = true;
            path[temp1][temp2] = 'r';
            bfs.push(make_pair(temp1,temp2));
        }
    }
    int x = ep.first, y = ep.second;
    if(visit[x][y] == false) {
        cout << "NO" << endl;
        return 0;
    }
    for(int i = 0;i < r;i++) {for(int j = 0;j < c;j++) cout << path[i][j]; cout << endl;}
    cout << "YES" << endl;
    while(1)
    {
        if(x == sp.first && y == sp.second) break;
        maze[x][y] = 'x';
        if(path[x][y] == 'u') x = mod(x-1,r);
        else if(path[x][y] == 'd') x = mod(x+1,r);
        else if(path[x][y] == 'l') y = mod(y-1,c);
        else y = mod(y+1,c);
    }
    maze[ep.first][ep.second] = 'D';
    for(int i = 0;i < r;i++)
    {
        for(int j = 0;j < c;j++) cout << maze[i][j];
        cout << endl;
    }
    return 0;
}

Code explanation: The array ‘path’ stores the parent of that point and the rest is bfs. I have made mod function since we can go from left and reach the right most end of the gird.