Created
November 27, 2013 01:33
-
-
Save agam/7669304 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cmath> | |
| #include <cstdint> | |
| #include <cstdio> | |
| #include <cstdlib> | |
| #include <iostream> | |
| #include <limits> | |
| #include <string> | |
| #include <vector> | |
| using namespace std; | |
| void PrintGrid(const vector<vector<char>>& grid) { | |
| for (int i = 0; i < grid.size(); ++i) { | |
| for (int j = 0; j < grid.size(); ++j) { | |
| cout << grid[i][j]; | |
| } | |
| cout << endl; | |
| } | |
| } | |
| bool GridFall(vector<vector<char>>* grid) { | |
| bool changed = false; | |
| // Skip row above ground | |
| for (int i = grid->size() - 2; i >= 0; --i) { | |
| auto& current_row = grid->at(i); | |
| auto& next_row = grid->at(i + 1); | |
| for (int j = 0; j < grid->size(); ++j) { | |
| if (current_row[j] == '.' && next_row[j] == ' ') { | |
| changed = true; | |
| current_row[j] = ' '; | |
| next_row[j] = '.'; | |
| } | |
| } | |
| } | |
| return changed; | |
| } | |
| int main() { | |
| int grid_size; | |
| cin >> grid_size; | |
| vector<vector<char>> grid(grid_size); | |
| char dummy_newline; | |
| cin.get(dummy_newline); | |
| cin >> noskipws; | |
| for (int i = 0; i < grid_size; ++i) { | |
| for (int j = 0; j < grid_size; ++j) { | |
| char grid_char; | |
| cin.get(grid_char); | |
| grid[i].push_back(grid_char); | |
| } | |
| cin.get(dummy_newline); | |
| } | |
| do { | |
| // Uncomment to view/debug transitory steps. | |
| //cout << "Intermediate grid state :- " << endl; | |
| //PrintGrid(grid); | |
| } while (GridFall(&grid)); | |
| PrintGrid(grid); | |
| return 0; | |
| } | |
| /* | |
| * Input Description | |
| * On standard console input, you will be given an integer N which represents | |
| * the N x N grid of ASCII characters. This means there will be N-lines of | |
| * N-characters long. This is the starting grid of your simulated world: the | |
| * character ' ' (space) means an empty space, while '.' (dot) means sand, and | |
| * '#' (hash or pound) means stone. Once you parse this input, simulate the | |
| * world until all particles are settled (e.g. the sand has fallen and either | |
| * settled on the ground or on stone). "Ground" is defined as the solid surface | |
| * right below the last row. | |
| * Output Description | |
| * Print the end result of all particle positions using the input format for | |
| * particles. | |
| * Sample Inputs & Outputs | |
| * Sample Input | |
| * 5 | |
| * ..... | |
| * # | |
| * # | |
| * | |
| * . | |
| * Sample Output | |
| * . | |
| * . # | |
| * # | |
| * . | |
| * . .. | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment