Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright (c) 2013 xerpi
- */
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <cstdlib>
- #include <unistd.h>
- using namespace std;
- typedef vector<char> Row;
- typedef vector<Row> Matrix;
- void read_matrix(Matrix& m, int rows, int columns);
- void print_matrix(Matrix& m, int rows, int columns);
- void fire_forest(Matrix& m, int rows, int columns);
- void random_matrix(Matrix& m, int rows, int columns);
- int main()
- {
- int n, m;
- while (cin >> n >> m) {
- Matrix forest(n, Row(m));
- cout << "\033[2J\033[1;1H";
- //read_matrix(forest, n, m);
- random_matrix(forest, n, m);
- fire_forest(forest, n, m);
- cout << "\033[2J\033[1;1H";
- print_matrix(forest, n, m);
- cout << endl;
- }
- }
- void fire_forest(Matrix& m, int rows, int columns)
- {
- int burned_trees = 1;
- while (burned_trees != 0) {
- cout << "\033[2J\033[1;1H";
- burned_trees = 0;
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < columns; ++j) {
- if (m[i][j] == 'F') {
- //Dalt
- if (i > 0) {
- if (m[i-1][j] == 'A') {
- m[i-1][j] = 'B';
- ++burned_trees;
- }
- }
- //Baix
- if (i < (rows-1)) {
- if (m[i+1][j] == 'A') {
- m[i+1][j] = 'B';
- ++burned_trees;
- }
- }
- //Esquerra
- if (j > 0) {
- if (m[i][j-1] == 'A') {
- m[i][j-1] = 'B';
- ++burned_trees;
- }
- }
- //Dreta
- if (j < (columns-1)) {
- if (m[i][j+1] == 'A') {
- m[i][j+1] = 'B';
- ++burned_trees;
- }
- }
- }
- }
- }
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < columns; ++j) {
- if (m[i][j] == 'B') {
- m[i][j] = 'F';
- }
- }
- }
- print_matrix(m, rows, columns);
- usleep(250 * 1000);
- }
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < columns; ++j) {
- if (m[i][j] == 'F') {
- m[i][j] = '.';
- }
- }
- }
- }
- void random_matrix(Matrix& m, int rows, int columns)
- {
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < columns; ++j) {
- int n = rand()%100;
- if (n < 10) {
- m[i][j] = 'F';
- } else if (n >= 10 and n < 70) {
- m[i][j] = 'A';
- } else {
- m[i][j] = '.';
- }
- }
- }
- }
- void print_matrix(Matrix& m, int rows, int columns)
- {
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < columns; ++j) {
- cout << m[i][j];
- }
- cout << endl;
- }
- }
- void read_matrix(Matrix& m, int rows, int columns)
- {
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < columns; ++j) {
- cin >> m[i][j];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement