Guest User

Untitled

a guest
Feb 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. void pongoTodoEnCero(string terreno[100][100],short,short);
  9. void leoElMapa(string terreno[100][100],short,short);
  10. void recorroElMapa(string terreno[100][100],short,short);
  11. void sumoAlrededor1(string terreno[100][100],short,short,short,short);
  12. bool esValida(short, short,short,short);
  13. bool esMina(string terreno[100][100],short,short);
  14. void sumo1(string terreno[100][100],short,short);
  15.  
  16. int main()
  17. {
  18.     //creo una matriz de MxN
  19.     short m,n;
  20.     string terreno[100][100];
  21.     int campos=1;
  22.     //char caracter;
  23.  
  24.  
  25.  
  26.     while((cin>>m>>n).good() and not(m==0 or n==0)){
  27.  
  28.         leoElMapa(terreno,m,n);
  29.  
  30.         pongoTodoEnCero(terreno,m,n);
  31.  
  32.         recorroElMapa(terreno,m,n);
  33.  
  34.  
  35.         cout << "Field #" << campos << ":" << endl;
  36.         for(int i=0;i<m;i++){
  37.             for(int j=0;j<n;j++){
  38.                 cout <<  terreno[i][j];
  39.             }
  40.             cout << endl;
  41.         }
  42.         cout << endl;
  43.         campos++;
  44.  
  45.     }
  46.     return 0;
  47. }
  48.  
  49. void pongoTodoEnCero(string terreno[100][100],short m,short n){
  50.     string temporal;
  51.     for(int i=0;i<m;i++){
  52.         for(int j=0;j<n;j++){
  53.             temporal=terreno[i][j];
  54.             if (temporal!="*"){
  55.                 terreno[i][j]="0";
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61.  
  62. void leoElMapa(string terreno[100][100],short m,short n){
  63.     string temporal;
  64.     for(int i=0;i<m;i++){
  65.         cin>>temporal;
  66.  
  67.         for(int j=0;j<n;j++){
  68.             terreno[i][j]=temporal[j];
  69.         }
  70.     }
  71. }
  72.  
  73.  
  74. void recorroElMapa(string terreno[100][100],short m,short n){
  75.     for(int i=0;i<m;i++){
  76.         for(int j=0;j<n;j++){
  77.             if (terreno[i][j]=="*"){
  78.                 sumoAlrededor1(terreno,m,n,i,j);
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. void sumoAlrededor1(string terreno[100][100],short m,short n,short i,short j){
  85.     if (esValida(i-1,j-1,m,n) and not(esMina(terreno,i-1,j-1))){
  86.         sumo1(terreno,i-1,j-1);
  87.     } if(esValida(i,j-1,m,n)and not(esMina(terreno,i,j-1))){
  88.         sumo1(terreno,i,j-1);
  89.     } if(esValida(i+1,j-1,m,n)and not(esMina(terreno,i+1,j-1))){
  90.         sumo1(terreno,i+1,j-1);
  91.     } if(esValida(i-1,j,m,n)and not(esMina(terreno,i-1,j))){
  92.         sumo1(terreno,i-1,j);
  93.     } if(esValida(i+1,j,m,n)and not(esMina(terreno,i+1,j))){
  94.         sumo1(terreno,i+1,j);
  95.     } if(esValida(i-1,j+1,m,n)and not(esMina(terreno,i-1,j+1))){
  96.         sumo1(terreno,i-1,j+1);
  97.     } if (esValida(i,j+1,m,n)and not(esMina(terreno,i,j+1))){
  98.         sumo1(terreno,i,j+1);
  99.     } if(esValida(i+1,j+1,m,n)and not(esMina(terreno,i+1,j+1))){
  100.         sumo1(terreno,i+1,j+1);
  101.     }
  102.  
  103. }
  104.  
  105. bool esValida(short i, short j,short m,short n){
  106.     bool sonValida=false;
  107.     if (i>=0 and i<m and j>=0 and j<n){
  108.         sonValida=true;
  109.     }
  110.  
  111.     return sonValida;
  112.  
  113. }
  114.  
  115. bool esMina(string terreno[100][100],short i, short j){
  116.     bool hayMina=false;
  117.     if (terreno[i][j]=="*"){
  118.         hayMina=true;
  119.     }
  120.  
  121.     return hayMina;
  122. }
  123.  
  124. void sumo1(string terreno[100][100],short i,short j){
  125.     int temporal = atoi(terreno[i][j].c_str())+1;
  126.     string s;
  127.     stringstream out;
  128.     out << temporal;
  129.     s = out.str();
  130.  
  131.     terreno[i][j]=s;
  132. }
Add Comment
Please, Sign In to add comment