#include using namespace std; struct oblasti { int points, dogs; oblasti() { points = dogs = 0; } }; struct cats { int r, c, power; cats() {} cats (int R, int C, int Power) { r = R; c = C; power = Power; } }; struct kletki { int n, r, c; string path; kletki() { n = 0; } kletki (int Nomer, string Path) { path = Path; n = Nomer; } kletki (int R, int C, int Nomer, string Path) { r = R; c = C; n = Nomer; path = Path; } }; struct obl { int score, ob, r, c; string path; obl() { score = 0; path = ""; } }; vector c; char m[100][100]; kletki tekKotki[100][100]; vector v; bool used[100][100]; bool glused[100][100]; vector res; bool cmp(obl a, obl b) { return a.score > b.score; } void initused() { for (int i = 0; i < 100; i ++) { for (int j = 0; j < 100; j ++) { used[i][j] = 0; } } } void initglused() { for (int i = 0; i < 100; i ++) { for (int j = 0; j < 100; j ++) { glused[i][j] = 0; } } } int SOblast(int R, int C, int N, int M, int obla) { string s; int score = 0; queue cts; /// cts - cats cats CurrCat; queue k; /// k - kletki, fr - front kletki fr; cats tmp; tmp.r = R; tmp.c = C; tmp.power = (int)(m[R][C] - '0'); cts.push(tmp); initglused(); while (!cts.empty()) { CurrCat = cts.front(); cts.pop(); score += (int)(m[CurrCat.r][CurrCat.c] - '0'); k.push(kletki(CurrCat.r, CurrCat.c, tekKotki[CurrCat.r][CurrCat.c].n, "")); int NomNaK = 1; used[CurrCat.r][CurrCat.c] = 1; glused[CurrCat.r][CurrCat.c] = 1; initused(); while (!k.empty()) { fr = k.front(); k.pop(); if (fr.r + 1 < N) { if (m[fr.r+1][fr.c] != '#' && used[fr.r+1][fr.c] == 0) { if (isdigit(m[fr.r+1][fr.c]) && glused[fr.r+1][fr.c] == 0) { cts.push(cats(fr.r+1, fr.c, (m[fr.r+1][fr.c] - '0'))); s += fr.path + "D"; used[fr.r+1][fr.c] = 1; glused[fr.r+1][fr.c] = 1; break; } else { k.push(kletki(fr.r+1, fr.c, tekKotki[fr.r+1][fr.c].n, fr.path + "D")); used[fr.r+1][fr.c] = 1; } } } if (fr.r - 1 >= 0) { if (m[fr.r-1][fr.c] != '#' && used[fr.r-1][fr.c] == 0) { if (isdigit(m[fr.r-1][fr.c]) && glused[fr.r-1][fr.c] == 0) { cts.push(cats(fr.r-1, fr.c, (m[fr.r-1][fr.c] - '0'))); s += fr.path + "U"; glused[fr.r-1][fr.c] = 1; used[fr.r-1][fr.c] = 1; break; } else { k.push(kletki(fr.r-1, fr.c, tekKotki[fr.r-1][fr.c].n, fr.path + "U")); used[fr.r+1][fr.c] = 1; } } } if (fr.c + 1 < M) { if (m[fr.r][fr.c+1] != '#' && used[fr.r][fr.c+1] == 0) { if (isdigit(m[fr.r][fr.c+1]) && glused[fr.r][fr.c+1] == 0) { cts.push(cats(fr.r, fr.c+1, (m[fr.r][fr.c+1] - '0'))); s += fr.path + "R"; glused[fr.r][fr.c+1] = 1; used[fr.r][fr.c+1] = 1; break; } else { k.push(kletki(fr.r, fr.c+1, tekKotki[fr.r][fr.c+1].n, fr.path + "R")); used[fr.r][fr.c+1] = 1; } } } if (fr.c - 1 >= 0) { if (m[fr.r][fr.c-1] != '#' && used[fr.r][fr.c-1] == 0) { if (isdigit(m[fr.r][fr.c-1]) && glused[fr.r][fr.c-1] == 0) { cts.push(cats(fr.r, fr.c-1, (m[fr.r][fr.c-1] - '0'))); s += fr.path + "L"; glused[fr.r][fr.c-1] = 1; used[fr.r][fr.c-1] = 1; break; } else { k.push(kletki(fr.r, fr.c-1, tekKotki[fr.r][fr.c-1].n, fr.path + "L")); used[fr.r][fr.c-1] = 1; } } } } } obl temp; temp.ob = obla; temp.c = C + 1; temp.r = R + 1; temp.path = s; temp.score = score - temp.path.size(); res.push_back(temp); return 1; } void Solve(int F, int K) { int Ni, Mi, Bi, zones; /// Ni - CurrRow, Mi - CurrColumn, Bi - CurrBonus if all the cats are dead, zones - zones in the front for (int i = 0; i < F; i ++) { zones = 0; cin >> Ni >> Mi >> Bi; for (int R = 0; R < Ni; R ++) { for (int C = 0; C < Mi; C ++) { cin >> m[R][C]; } } for (int R = 0; R < Ni; R ++) { for (int C = 0; C < Mi; C ++) { if (isdigit(m[R][C])) { SOblast(R, C, Ni, Mi, i + 1); } } } } } int main() { freopen("catsdogs.out", "w", stdout); int F, K; /// F - front lines, K - dogs cin >> F >> K; Solve (F, K); sort(res.begin(), res.end(), cmp); int i; for (i = 0; i < K; i ++) { if (res.size() == i) break; if (res[i].score > 0) { if (res[i].path == "") { cout << "STAY" << endl; continue; } cout << res[i].ob << endl; cout << res[i].r << " " << res[i].c << endl; cout << res[i].path << endl; } else { cout << 0 << endl; } } for ( ; i < K; i ++) { cout << 0 << endl; } fclose(stdout); return 0; }