#include #include #include using namespace std; struct clue{ int r; int c; int clue_num; char dir; bool operator<(const clue& other){ if(dir == 'A' && other.dir == 'D') return true; if(dir == 'D' && other.dir == 'A') return false; return clue_num < other.clue_num; } }; void Make_Clue_List(const vector >& puzzle, vector& clue_list){ int cur_num = 1; for(int i = 0; i < puzzle.size(); i++) for(int j = 0; j < puzzle[i].size(); j++){ if(puzzle[i][j] != '#'){ bool made_clue = false; if(i == 0 || puzzle[i-1][j] == '#'){ clue cur; cur.r = i; cur.c = j; cur.clue_num = cur_num; cur.dir = 'D'; clue_list.push_back(cur); made_clue = true; } if(j == 0 || puzzle[i][j-1] == '#'){ clue cur; cur.r = i; cur.c = j; cur.clue_num = cur_num; cur.dir = 'A'; clue_list.push_back(cur); made_clue = true; } if(made_clue) cur_num++; } } } bool Filled(const vector >& puzzle){ for(int i = 0; i < puzzle.size(); i++){ for(int j = 0; j < puzzle[i].size(); j++){ if(puzzle[i][j] == '.') return false; } } return true; } clue Find_Next_Clue(const vector >& puzzle, const vector& clue_list){ double best_ratio = -1; clue best_clue; for(int i = 0; i < clue_list.size(); i++){ int total_num = 0; int total_den = 0; int cur_den = 1; if(clue_list[i].dir == 'A'){ int start = clue_list[i].c; int end = start+1; while(end < puzzle[0].size() && puzzle[clue_list[i].r][end] != '#'){ end++; } end--; for(int c = end; c >= start; c--){ total_den = total_den + cur_den; if(puzzle[clue_list[i].r][c] != '.') total_num = total_num + cur_den; cur_den++; } } else{ // down int start = clue_list[i].r; int end = start+1; while(end < puzzle.size() && puzzle[end][clue_list[i].c] != '#'){ end++; } end--; for(int r = end; r >= start; r--){ total_den = total_den + cur_den; if(puzzle[r][clue_list[i].c] != '.') total_num = total_num + cur_den; cur_den++; } } double cur_ratio = (double) total_num / (double) total_den; if(cur_ratio > best_ratio && total_num != total_den){ best_ratio = cur_ratio; best_clue = clue_list[i]; } } return best_clue; } void Fill_Clue(vector >& puzzle, clue choice){ int r = choice.r; int c = choice.c; if(choice.dir == 'A'){ while(c < puzzle[r].size() && puzzle[r][c] != '#'){ puzzle[r][c] = 'x'; c++; } } else{ while( r < puzzle.size() && puzzle[r][c] != '#'){ puzzle[r][c] = 'x'; r++; } } } void Print_Puzzle(const vector >& puzzle){ for(int i = 0; i < puzzle.size(); i++){ for(int j = 0; j < puzzle.size(); j++) cout << puzzle[i][j]; cout << endl; } } void Sort(vector& clue_list){ for(int i = 0; i < clue_list.size(); i++) for(int j = 0; j < clue_list.size()-1; j++) if(clue_list[j+1] < clue_list[j]){ clue temp = clue_list[j]; clue_list[j] = clue_list[j+1]; clue_list[j+1] = temp; } } void Print_Clue_List(const vector& clue_list){ for(int i = 0; i < clue_list.size(); i++){ cout << "Clue " << clue_list[i].clue_num << clue_list[i].dir << ": (" << clue_list[i].r << "," << clue_list[i].c << ")" << endl; } } int main(){ int r, c; cin >> r >> c; vector > puzzle; puzzle.resize(r); for(int i = 0; i < puzzle.size(); i++) puzzle[i].resize(c); for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) cin >> puzzle[i][j]; vector clue_list; Make_Clue_List(puzzle, clue_list); Sort(clue_list); // Print_Clue_List(clue_list); while(!Filled(puzzle)){ clue next = Find_Next_Clue(puzzle, clue_list); Fill_Clue(puzzle, next); // Print_Puzzle(puzzle); cout << next.clue_num << next.dir << endl; } return 0; }