#include #include using namespace std; const int MAX = 50; const char BLACK = '#'; const char BLANK = '.'; const char ACROSS = 'A'; const char DOWN = 'D'; struct square { int num; char type; } grid[MAX+2][MAX+2]; struct clue { int number; char type; int row, col; int len; double rank; }; void calcAcrossRank(clue &cl) { int r = cl.row; int c = cl.col; cl.rank = 0.0; for(int i=0; i clues) { int best; double bestVal = -1; for(int i=0; i bestVal || bestVal == -1) { bestVal = c.rank; best = i; } else if (c.rank == bestVal && clues[best].type == DOWN && c.type == ACROSS) best = i; } return best; } int main() { int numr, numc; cin >> numr >> numc; for(int r=0; r<=numr+1; r++) { grid[r][0].type = BLACK; grid[r][numc+1].type = BLACK; } for(int c=1; c<=numc; c++) { grid[0][c].type = BLACK; grid[numr+1][c].type = BLACK; } for(int r=1; r<= numr; r++) for(int c=1; c<=numc; c++) cin >> grid[r][c].type; vector clues; clue cl; int number = 0; for(int r=1; r<=numr; r++) { for(int c=1; c<=numc; c++) { if (grid[r][c].type == BLACK) continue; if (grid[r][c-1].type == BLACK) { cl.number = ++number; cl.type = ACROSS; cl.row = r; cl.col = c; cl.len = 1; int cc = c+1; while (grid[r][cc].type != BLACK) { cl.len++; cc++; } calcAcrossRank(cl); clues.push_back(cl); } if (grid[r-1][c].type == BLACK) { if (grid[r][c-1].type == BLACK) cl.number = number; else cl.number = ++number; cl.type = DOWN; cl.row = r; cl.col = c; cl.len = 1; int rr = r+1; while (grid[rr][c].type != BLACK) { cl.len++; rr++; } calcDownRank(cl); clues.push_back(cl); } } } // for (clue c : clues) { // cout << c.number << c.type << ' ' << c.row << ',' << c.col << ' ' << c.len << ' ' << c.rank << endl; // } /**/ while (true) { int index = getNextBestClue(clues); clue cl = clues[index]; if (cl.rank == -1) break; cout << cl.number << cl.type << endl; clues[index].rank = -1; if (cl.type == ACROSS) { int r = cl.row; int cc = cl.col; while (grid[r][cc].type != BLACK) { grid[r][cc].type = 'A'; cc++; } for(int i=0; i