Created
March 8, 2024 14:04
-
-
Save tungurlakachakcak/0326dbb3d436a6b9f6b38b1d973268ee to your computer and use it in GitHub Desktop.
Simulation of metal piece rusting
This file contains 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 <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <unordered_set> | |
static const int dim = 10; | |
std::string readLine(std::istream& in) { | |
std::string str; | |
std::getline(in, str); | |
return str; | |
} | |
class Metal final { | |
public: | |
void fill(std::istream& in) { | |
for (size_t i = 0; i < dim * dim; i++) { | |
char c; | |
in >> c; | |
if (c == '\n') continue; | |
cells[i / dim][i % dim] = c; | |
} | |
} | |
void rust() { | |
for (size_t i = 0; i < dim; i++) { | |
for (size_t j = 0; j < dim; j++) { | |
char c = cells[i][j]; | |
if (c == rustParticle) { | |
// Spread up | |
if (i > 0) { | |
cells[i - 1][j] != rustresist && cells[i - 1][j] != rustParticle ? cells[i - 1][j] = rustCreepsIn : 0; | |
} | |
// Spread right | |
if (j < dim - 1) { | |
cells[i][j + 1] != rustresist && cells[i][j + 1] != rustParticle ? cells[i][j + 1] = rustCreepsIn : 0; | |
} | |
// spread down | |
if (i < dim - 1) { | |
cells[i + 1][j] != rustresist && cells[i + 1][j] != rustParticle ? cells[i + 1][j] = rustCreepsIn : 0; | |
} | |
if (j > 0) { | |
cells[i][j - 1] != rustresist && cells[i][j - 1] != rustParticle ? cells[i][j - 1] = rustCreepsIn : 0; | |
} | |
} | |
} | |
} | |
for (size_t i = 0; i < dim; i++) { | |
for (size_t j = 0; j < dim; j++) { | |
if (cells[i][j] == rustCreepsIn) { | |
cells[i][j] = rustParticle; | |
} | |
} | |
} | |
} | |
void print(std::ostream& out) const { | |
for (size_t i = 0; i < dim; i++) { | |
for (size_t j = 0; j < dim; j++) { | |
out << cells[i][j]; | |
} | |
out << '\n'; | |
} | |
} | |
private: | |
const char rustParticle = '!'; | |
const char rustresist = '#'; | |
const char rustCreepsIn = '%'; | |
char cells[dim][dim]; | |
}; | |
int main() | |
{ | |
Metal m; | |
m.fill(std::cin); | |
int n = 0; | |
std::cin >> n; | |
for (size_t i = 0; i < n; i++) { | |
m.rust(); | |
} | |
m.print(std::cout); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment