Problem: cf811_d
Difficulty: easy
Categories: graph
Time limit: 2000 ms, memory limit: 256 MB

D. Vladik and Favorite Game

This is an interactive problem.

Vladik has favorite game, in which he plays all his free time.

Game field could be represented as *n* × *m* matrix which consists of cells of three types:

* «.» — normal cell, player can visit it.
* «F» — finish cell, player has to finish his way there to win. There is exactly one cell of this type.
* «\*» — dangerous cell, if player comes to this cell, he loses.

Initially player is located in the left top cell with coordinates (1, 1).

Player has access to 4 buttons "U", "D", "L", "R", each of them move player up, down, left and right directions respectively.

But it’s not that easy! Sometimes friends play game and change functions of buttons. Function of buttons "L" and "R" could have been swapped, also functions of buttons "U" and "D" could have been swapped. Note that functions of buttons can be changed only at the beginning of the game.

Help Vladik win the game!

Input

First line contains two space-separated integers *n* and *m* (1 ≤ *n*, *m* ≤ 100) — number of rows and columns respectively.

Each of next *n* lines contains *m* characters describing corresponding row of field. Set of characters in field is described above.

Guaranteed that cell with coordinates (1, 1) is normal and there is at least one way from initial cell to finish cell without dangerous cells.

Interaction

You can press buttons no more than 2·*n*·*m* times.

To press a button you should print "U", "D", "L", "R" in new line. It’s necessary to print newline character and flush output. After flushing buffer you should read answer from input data. Answer is the pair of space-separated integers *x*, *y* — new position of player. In case, if there is no cell in direction of moving, position will not change. If after any move player lost, in other words player move to dangerous cell, then *x* and *y* will be equal to  - 1.

If after any move player is in finish or dangerous cell, then you should terminate your program.

To finish output buffer (i. e. for operation flush) right after printing direction and newline you should do next:

* fflush(stdout) in C++
* System.out.flush() in Java
* stdout.flush() in Python
* flush(output) in Pascal
* read documentation for other languages.

Example

Input

```
4 3  
...  
**.  
F*.  
...  
1 1  
1 2  
1 3  
1 3  
2 3  
3 3  
4 3  
4 2  
4 1  
3 1
```

Output

```
R  
L  
L  
D  
U  
U  
U  
R  
R  
D
```

Non-adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;


static long long queries = 0;
static long long query_limit = 0;



static void log_metrics() {
    if (query_limit == 0) return; 
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}


static void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}

int main(int argc, char* argv[]) {
    
    registerInteraction(argc, argv);
    
    
    atexit(log_metrics);

    
    int n = inf.readInt(1, 100, "n");
    int m = inf.readInt(1, 100, "m");
    
    vector<string> grid(n);
    for (int i = 0; i < n; i++) {
        grid[i] = inf.readToken("([.F*]+)", "grid_row");
        
        if ((int)grid[i].size() != m) {
            quitf(_fail, "Grid row %d has length %d, expected %d", i + 1, (int)grid[i].size(), m);
        }
    }
    
    
    int swap_lr = inf.readInt(0, 1, "swap_lr");
    int swap_ud = inf.readInt(0, 1, "swap_ud");

    
    query_limit = 2LL * n * m;
    long long hard_cap = max(200000LL, 10 * query_limit);
    
    
    log_metrics();

    
    cout << n << " " << m << endl;
    for (int i = 0; i < n; i++) {
        cout << grid[i] << endl;
    }
    cout.flush();

    
    
    int r = 0;
    int c = 0;

    
    while (true) {
        
        if (queries > hard_cap) {
            finish(_pe, "Hard query limit exceeded (infinite loop?)");
        }

        string token;
        
        if (!(cin >> token)) {
            finish(_wa, "Unexpected EOF (did not reach finish)");
        }

        
        if (token == "?") {
            if (!(cin >> token)) {
                finish(_wa, "Unexpected EOF after '?'");
            }
        } else if (token == "!") {
            
            
            
            
        }

        if (token.length() != 1) {
            finish(_pe, "Invalid move format: expected single character U, D, L, or R");
        }

        char move = token[0];
        int dr = 0;
        int dc = 0;

        
        if (move == 'L') {
            dc = swap_lr ? 1 : -1;
        } else if (move == 'R') {
            dc = swap_lr ? -1 : 1;
        } else if (move == 'U') {
            dr = swap_ud ? 1 : -1;
        } else if (move == 'D') {
            dr = swap_ud ? -1 : 1;
        } else {
            finish(_pe, string("Invalid move character: ") + move);
        }

        queries++;
        
        if (queries % 20 == 0) log_metrics();

        int nr = r + dr;
        int nc = c + dc;

        
        if (nr >= 0 && nr < n && nc >= 0 && nc < m) {
            char cell = grid[nr][nc];
            if (cell == '*') {
                
                cout << "-1 -1" << endl;
                finish(_wa, "Moved to dangerous cell (*)");
            }
            
            r = nr;
            c = nc;
        }
        

        
        cout << (r + 1) << " " << (c + 1) << endl;

        
        if (grid[r][c] == 'F') {
            finish(_ok, "Reached finish cell (F)");
        }
    }

    return 0;
}


Adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;


static int N, M;
static vector<string> grid;


static long long queries = 0;
static long long query_limit = 0;


struct Config {
    int s_lr; 
    int s_ud; 
};
static vector<Config> valid_configs;


static void log_metrics() {
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {}
}


static void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}


static bool is_valid(int r, int c) {
    return r >= 0 && r < N && c >= 0 && c < M;
}


static pair<int,int> get_outcome(int r, int c, string move, int s_lr, int s_ud) {
    int dr = 0, dc = 0;
    
    if (move == "U") dr = (s_ud ? 1 : -1);
    else if (move == "D") dr = (s_ud ? -1 : 1);
    else if (move == "L") dc = (s_lr ? 1 : -1);
    else if (move == "R") dc = (s_lr ? -1 : 1);
    
    int nr = r + dr;
    int nc = c + dc;
    
    
    if (!is_valid(nr, nc)) return {r, c};
    
    
    if (grid[nr][nc] == '*') return {-1, -1};
    
    
    return {nr, nc};
}

int main(int argc, char** argv) {
    registerInteraction(argc, argv);
    atexit(log_metrics);

    
    N = inf.readInt();
    M = inf.readInt();
    query_limit = 2LL * N * M;
    
    grid.resize(N);
    for(int i = 0; i < N; ++i) {
        grid[i] = inf.readToken();
    }
    
    

    
    cout << N << " " << M << endl;
    for(int i = 0; i < N; ++i) {
        cout << grid[i] << endl;
    }
    cout.flush();

    
    
    valid_configs = {{0,0}, {0,1}, {1,0}, {1,1}};
    int cur_r = 0, cur_c = 0; 

    
    log_metrics();

    
    while (true) {
        
        if (queries % 20 == 0) log_metrics();

        string move = ouf.readToken();
        queries++;

        
        if (queries > query_limit) {
            finish(_wa, "Query limit exceeded");
        }

        
        if (move != "L" && move != "R" && move != "U" && move != "D") {
            finish(_pe, "Invalid move format: " + move);
        }

        
        map<pair<int,int>, vector<Config>> outcomes;
        for (const auto& cfg : valid_configs) {
            outcomes[get_outcome(cur_r, cur_c, move, cfg.s_lr, cfg.s_ud)].push_back(cfg);
        }

        
        
        
        
        
        
        
        pair<int,int> best_pos = {-2, -2};
        int best_score = -1;
        size_t best_count = 0;

        for (auto& kv : outcomes) {
            pair<int,int> p = kv.first;
            int score = 0;
            if (p.first == -1) score = 2; 
            else if (grid[p.first][p.second] == 'F') score = 0; 
            else score = 1; 

            
            bool better = false;
            if (score > best_score) better = true;
            else if (score == best_score) {
                if (kv.second.size() > best_count) better = true;
                else if (kv.second.size() == best_count) {
                    if (p < best_pos) better = true; 
                }
            }

            if (better) {
                best_score = score;
                best_count = kv.second.size();
                best_pos = p;
            }
        }

        
        valid_configs = outcomes[best_pos];
        
        
        if (best_pos.first == -1) {
            cout << "-1 -1" << endl;
            finish(_wa, "Moved to dangerous cell");
        } else {
            cout << (best_pos.first + 1) << " " << (best_pos.second + 1) << endl;
        }
        
        
        cur_r = best_pos.first;
        cur_c = best_pos.second;

        
        if (best_score == 0) { 
            finish(_ok, "Reached finish");
        }
    }

    return 0;
}


Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

int main(int argc, char* argv[]) {
    registerGen(argc, argv, 1);
    string mode = opt<string>("mode", "non");
    long long seed_val = atoll(argv[1]);

    int n, m;
    vector<string> grid;
    int swap_lr = rnd.next(0, 1);
    int swap_ud = rnd.next(0, 1);

    
    if (seed_val == 1) {
        n = 1; m = 2;
        grid = {".F"};
    } else if (seed_val == 2) {
        n = 2; m = 1;
        grid = {".", "F"};
    } else if (seed_val == 3) {
        n = 2; m = 2;
        grid = {"..", ".F"};
    } else {
        
        
        
        int type = rnd.next(1, 2);
        
        
        if (mode == "adp") type = 1;

        
        if (seed_val < 10) {
            n = rnd.next(3, 10);
            m = rnd.next(3, 10);
        } else {
            n = rnd.next(2, 100);
            m = rnd.next(2, 100);
        }

        grid.assign(n, string(m, '*'));
        grid[0][0] = '.';

        if (type == 1) { 
            vector<pair<int,int>> frontier;
            frontier.push_back({0,0});
            
            
            while(!frontier.empty()){
                
                int idx = rnd.next((int)frontier.size());
                if (mode == "adp" && rnd.next(2)) idx = frontier.size() - 1;

                auto [r, c] = frontier[idx];
                bool extended = false;
                
                vector<int> dirs = {0, 1, 2, 3};
                shuffle(dirs.begin(), dirs.end());
                int dr[] = {-1, 1, 0, 0};
                int dc[] = {0, 0, -1, 1};

                for(int d : dirs){
                    int nr = r + dr[d] * 2;
                    int nc = c + dc[d] * 2;
                    if(nr >= 0 && nr < n && nc >= 0 && nc < m){
                        if(grid[nr][nc] == '*'){
                            grid[r + dr[d]][c + dc[d]] = '.';
                            grid[nr][nc] = '.';
                            frontier.push_back({nr, nc});
                            extended = true;
                            
                            if (rnd.next(3) == 0) break;
                        }
                    }
                }
                if (!extended) {
                    frontier.erase(frontier.begin() + idx);
                }
            }
            
            
            int noise_ops = rnd.next(n * m / 20, n * m / 5);
            for(int k=0; k<noise_ops; ++k) {
                grid[rnd.next(n)][rnd.next(m)] = '.';
            }
        } else { 
            for(int i=0; i<n; ++i) fill(grid[i].begin(), grid[i].end(), '.');
            int obstacles = rnd.next(n * m / 5, n * m / 2);
            for(int k=0; k<obstacles; ++k) {
                int r = rnd.next(n);
                int c = rnd.next(m);
                if (r == 0 && c == 0) continue;
                grid[r][c] = '*';
            }
        }

        
        vector<vector<bool>> vis(n, vector<bool>(m, false));
        queue<pair<int,int>> q;
        q.push({0,0});
        vis[0][0] = true;
        vector<pair<int,int>> reachable;

        int dr[] = {-1, 1, 0, 0};
        int dc[] = {0, 0, -1, 1};

        while(!q.empty()){
            auto [r,c] = q.front(); q.pop();
            reachable.push_back({r,c});
            
            for(int i=0; i<4; ++i){
                int nr = r + dr[i];
                int nc = c + dc[i];
                if(nr>=0 && nr<n && nc>=0 && nc<m && !vis[nr][nc] && grid[nr][nc] != '*'){
                    vis[nr][nc] = true;
                    q.push({nr, nc});
                }
            }
        }

        
        vector<pair<int,int>> candidates;
        for(auto p : reachable) {
            if (p.first != 0 || p.second != 0) candidates.push_back(p);
        }

        
        if (candidates.empty()) {
            if (n > 1) { grid[1][0] = '.'; candidates.push_back({1,0}); }
            else if (m > 1) { grid[0][1] = '.'; candidates.push_back({0,1}); }
        }

        
        int f_idx = rnd.next((int)candidates.size());
        
        if (mode == "adp" && candidates.size() > 5) {
            f_idx = rnd.next((int)candidates.size() / 2, (int)candidates.size() - 1);
        }
        grid[candidates[f_idx].first][candidates[f_idx].second] = 'F';
    }

    
    
    
    
    cout << n << " " << m << endl;
    for(int i=0; i<n; ++i) cout << grid[i] << endl;
    cout << swap_lr << " " << swap_ud << endl;

    return 0;
}