Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <sstream>
- using namespace std;
- void pongoTodoEnCero(string terreno[100][100],short,short);
- void leoElMapa(string terreno[100][100],short,short);
- void recorroElMapa(string terreno[100][100],short,short);
- void sumoAlrededor1(string terreno[100][100],short,short,short,short);
- bool esValida(short, short,short,short);
- bool esMina(string terreno[100][100],short,short);
- void sumo1(string terreno[100][100],short,short);
- int main()
- {
- //creo una matriz de MxN
- short m,n;
- string terreno[100][100];
- int campos=1;
- //char caracter;
- while((cin>>m>>n).good() and not(m==0 or n==0)){
- leoElMapa(terreno,m,n);
- pongoTodoEnCero(terreno,m,n);
- recorroElMapa(terreno,m,n);
- cout << "Field #" << campos << ":" << endl;
- for(int i=0;i<m;i++){
- for(int j=0;j<n;j++){
- cout << terreno[i][j];
- }
- cout << endl;
- }
- cout << endl;
- campos++;
- }
- return 0;
- }
- void pongoTodoEnCero(string terreno[100][100],short m,short n){
- string temporal;
- for(int i=0;i<m;i++){
- for(int j=0;j<n;j++){
- temporal=terreno[i][j];
- if (temporal!="*"){
- terreno[i][j]="0";
- }
- }
- }
- }
- void leoElMapa(string terreno[100][100],short m,short n){
- string temporal;
- for(int i=0;i<m;i++){
- cin>>temporal;
- for(int j=0;j<n;j++){
- terreno[i][j]=temporal[j];
- }
- }
- }
- void recorroElMapa(string terreno[100][100],short m,short n){
- for(int i=0;i<m;i++){
- for(int j=0;j<n;j++){
- if (terreno[i][j]=="*"){
- sumoAlrededor1(terreno,m,n,i,j);
- }
- }
- }
- }
- void sumoAlrededor1(string terreno[100][100],short m,short n,short i,short j){
- if (esValida(i-1,j-1,m,n) and not(esMina(terreno,i-1,j-1))){
- sumo1(terreno,i-1,j-1);
- } if(esValida(i,j-1,m,n)and not(esMina(terreno,i,j-1))){
- sumo1(terreno,i,j-1);
- } if(esValida(i+1,j-1,m,n)and not(esMina(terreno,i+1,j-1))){
- sumo1(terreno,i+1,j-1);
- } if(esValida(i-1,j,m,n)and not(esMina(terreno,i-1,j))){
- sumo1(terreno,i-1,j);
- } if(esValida(i+1,j,m,n)and not(esMina(terreno,i+1,j))){
- sumo1(terreno,i+1,j);
- } if(esValida(i-1,j+1,m,n)and not(esMina(terreno,i-1,j+1))){
- sumo1(terreno,i-1,j+1);
- } if (esValida(i,j+1,m,n)and not(esMina(terreno,i,j+1))){
- sumo1(terreno,i,j+1);
- } if(esValida(i+1,j+1,m,n)and not(esMina(terreno,i+1,j+1))){
- sumo1(terreno,i+1,j+1);
- }
- }
- bool esValida(short i, short j,short m,short n){
- bool sonValida=false;
- if (i>=0 and i<m and j>=0 and j<n){
- sonValida=true;
- }
- return sonValida;
- }
- bool esMina(string terreno[100][100],short i, short j){
- bool hayMina=false;
- if (terreno[i][j]=="*"){
- hayMina=true;
- }
- return hayMina;
- }
- void sumo1(string terreno[100][100],short i,short j){
- int temporal = atoi(terreno[i][j].c_str())+1;
- string s;
- stringstream out;
- out << temporal;
- s = out.str();
- terreno[i][j]=s;
- }
Add Comment
Please, Sign In to add comment