#include #include #include #include #include #include #include #include using namespace std; fstream in("catsdogs.in"); ofstream out("catsdogs.out"); const int moveY[] = {1, 0, -1, 0}; const int moveX[] = {0, 1, 0, -1}; const char dir[] = {'D', 'R', 'U', 'L'}; int myX, myY; struct path { int y, x; vector c; path(int _y, int _x , vector _c) { y = _y; x = _x; c = _c; } }; struct area { //x y stoinost area int x, y, s, a, p = 0; vector moves; area(int _y, int _x, int _a) { x = _x; y = _y; a = _a; } bool operator<(area other) const { return s < other.s; } }; struct field { //h w bonus nomer int h, w, b, s = 0, k = 0, c; vector> m; vector> visited; vector> visited3; vector> visited4; vector areas; field(int _h, int _w, int _b, int _c) { h = _h; w = _w; b = _b; c = _c; m.resize(h); visited.resize(h); visited3.resize(h); visited4.resize(h); for(int i = 0; i < h; i++) { m[i].resize(w); visited[i].resize(w); visited3[i].resize(w); visited4[i].resize(w); for(int j = 0; j < w; j++) { in >> m[i][j]; //cin >> m[i][j]; } } } void fix() { visited = vector>(); visited.resize(h); for(int i = 0; i < h; i++) { visited[i].resize(w); } } }; vector fields; vector bestAreas; int f,k; void readinput() { in >> f >> k; //scanf("%d %d", &f, &k); for(int i = 0; i < f; i++) { int h, w, b; in >> h >> w >> b; //scanf("%d %d %d", &h, &w, &b); field current = field(h, w, b, i); fields.push_back(current); } } bool dfs(int y, int x, int cF) { if(x >= fields[cF].w || x < 0 || y >= fields[cF].h || y < 0 || fields[cF].visited[y][x] || fields[cF].m[y][x] == '#') { return false; } else { fields[cF].visited[y][x] = true; for(int i = 0; i < 4; i++) { dfs(moveY[i] + y, moveX[i] + x, cF); } return 1; } } void getAllAreas() { for(int t = 0; t < f; t++) { for(int i = 0; i < fields[t].h; i++) { for(int j = 0; j < fields[t].w; j++) { if(!fields[t].visited[i][j]/*&& fields[t].m[i][j] != '.'*/) { bool temp = dfs(i, j, t); if(temp) { fields[t].areas.push_back(area(i, j, t)); } } } } fields[t].fix(); } } void traverse(int y, int x, int t, int aI, queue& q, char current) { if(x < 0 || x >= fields[t].w || y < 0 || y >= fields[t].h || fields[t].m[y][x] == '#' || fields[t].visited[y][x]) { return; } myX = x; myY = y; fields[t].visited[y][x] = true; if(fields[t].m[y][x] != '.') fields[t].areas[aI].p += fields[t].m[y][x] - 48; while(!q.empty()) { fields[t].areas[aI].moves.push_back(q.front()); q.pop(); } if(current != 'S') { if(fields[t].areas[aI].moves.size() > 19500) return; fields[t].areas[aI].moves.push_back(current); } for(int i = 0; i < 4; i++) { traverse(y + moveY[i], x + moveX[i], t, aI, q, dir[i]); } switch(current) { case 'S' : break; case 'D' : q.push('U'); break; case 'U' : q.push('D'); break; case 'L' : q.push('R'); break; case 'R' : q.push('L'); break; } } bool bfs(int y, int x, int t, int aI) { queue q; q.push(path(y, x, vector())); while(!q.empty()) { path c = q.front(); q.pop(); int cY = c.y, cX = c.x; if(cY < 0 || cY >= fields[t].h || cX < 0 || cX >= fields[t].w || fields[t].m[cY][cX] == '#' || fields[t].visited3[cY][cX]) { continue; } fields[t].visited3[cY][cX] = true; if(!fields[t].visited[cY][cX] && fields[t].m[cY][cX] != '.') { myX = cX; myY = cY; for(int i = 0; i < c.c.size(); i++) { //printf("%c", c.c[i]); fields[t].areas[aI].moves.push_back(c.c[i]); } //printf("\n"); return true; } for(int i = 0; i < 4; i++) { vector v = c.c; v.push_back(dir[i]); q.push(path(cY + moveY[i], cX + moveX[i], v)); } } return false; } void getSumOfAreas() { for(int i = 0; i < f; i++) { for(int j = 0; j < fields[i].areas.size(); j++) { queue q; traverse(fields[i].areas[j].y, fields[i].areas[j].x, i, j, q, 'S'); /*while(bfs(myY, myX, i, j)) { q = stack(); traverse(myY, myX, i, j, q, 'S'); }*/ fields[i].areas[j].s = fields[i].areas[j].p; //fields[i].areas[j].s = (2 * fields[i].areas[j].p) - fields[i].areas[j].moves.size(); //printf("%d\n", fields[i].areas[j].s); } } } void getBestAreas() { for(int i = 0; i < f; i++) { sort(fields[i].areas.begin(), fields[i].areas.end()); } int g = k; while(g) { int max = -1,z; for(int i = 0; i < f; i++) { if(fields[i].areas.empty()) continue; if(max < fields[i].areas.back().s) { max = fields[i].areas.back().s; z = i; } } if(max == -1) { break; } g--; bestAreas.push_back(fields[z].areas.back()); fields[z].areas.pop_back(); } } void printPath() { for(int i = 0; i < bestAreas.size(); i++) { out << bestAreas[i].a + 1 << endl; out << bestAreas[i].y + 1 << " " << bestAreas[i].x + 1 << endl; //printf("%d\n", bestAreas[i].a + 1); //printf("%d %d\n", bestAreas[i].y + 1, bestAreas[i].x + 1); for(int j = 0; j < bestAreas[i].moves.size(); j++) { out << bestAreas[i].moves[j]; //printf("%c", bestAreas[i].moves[j]); } out << endl; //printf("\n"); } for(int i = 0; i < k - bestAreas.size(); i++) { out << "0\n"; //printf("0\n"); } } int main() { readinput(); getAllAreas(); getSumOfAreas(); getBestAreas(); printPath(); return 0; }