#include #include #include #include using namespace std ; int moveind[128] ; int moves[4] ; const int INF = 1000000000 ; int main() { int N, M ; cin >> N >> M ; string lin ; for (int i=0; i> inp ; lin.append(inp) ; lin.push_back('#') ; } for (int i=0; i> cmd ; moveind['L'] = -1 ; moveind['R'] = 1 ; moveind['U'] = -M-1 ; moveind['D'] = M+1 ; moves[0] = -1 ; moves[1] = 1 ; moves[2] = -M-1 ; moves[3] = M+1 ; int atmod = lin.size() ; vector cost(lin.size()*(cmd.size()+1), INF) ; vector thislev ; thislev.push_back(st) ; cost[st] = 0 ; int finished = -1 ; for (int d=0; finished < 0; d++) { // first, all the additional nodes we can reach for free for (int i=0; i d) { cost[st2] = d ; thislev.push_back(st2) ; } } } // now advance by additional moves; these cost vector nextlev ; for (int i=0; finished < 0 && i d + 1) { cost[nat] = d + 1 ; nextlev.push_back(nat) ; } } } swap(thislev, nextlev) ; nextlev.clear() ; if (thislev.size() == 0) break ; } cout << finished << endl ; }