This is an easy problem from uva judge http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1130
Adhoc.
How I did it
- Read the row and column size.
- Read the input matriz, set an output matriz with 0s.
- Traverse the input matriz. If the current cell is ‘*’. check if all the posible adjacent cell are diferents than ‘*’ and increment the value in the output matriz.
- Just print the output matrix.
Hints
- Print a blank line between cases. The last case does not need the blank line.
- The 8 posible adjacent cells are:
[a-1][b], [a-1][b+1], [a-1][b-1], [a+1][b+1], [a+1][b-1], [a+1][b], [a][b-1], [a][b+1].
Code
#include <iostream> #include <cstdio> #define MAX 101 using namespace std; char input[MAX][MAX]; int output[MAX][MAX]; void readMatriz(int row, int col) { string tmp = "", dataRow; for(int x=1; x<=row; x++) { cin>>dataRow; tmp+=dataRow; } int index = 0; for(int x=0; x<row; x++) for(int y=0; y<col; y++, index++) { input[x][y] = tmp[index]; output[x][y] = 0; } } void getResult(int row, int col) { for(int a=0; a<row; a++) { for(int b=0; b<col; b++) { ////////////////////////////////////////////////////////////////////////// if(input[a][b] == '*') { if(a-1 >= 0) { if(input[a-1][b] != '*') output[a-1][b]++; if(b+1 < col && input[a-1][b+1] != '*') output[a-1][b+1]++; if(b-1 >= 0 && input[a-1][b-1] != '*') output[a-1][b-1]++; } if(a+1 < row) { if(b+1 < col && input[a+1][b+1] != '*') output[a+1][b+1]++; if(b-1 >= 0 && input[a+1][b-1] != '*') output[a+1][b-1]++; if(input[a+1][b] != '*') output[a+1][b]++; } if(b-1 >= 0 && input[a][b-1] != '*') output[a][b-1]++; if(b+1 < col && input[a][b+1] != '*') output[a][b+1]++; } //////////////////////////////////////////////////////////////////////// } } } void print(int row, int col) { for(int x=0; x<row;x++) { for(int y=0; y<col; y++) { if(input[x][y] == '*') printf("*"); else printf("%d", output[x][y]); } printf("\n"); } } int main() { int row, col, casos = 0; while(scanf("%d %d", &row, &col)) { if(!row && !col) break; readMatriz(row, col); getResult(row, col); if(casos == 0) casos++; else printf("\n"); printf("Field #%d:\n", casos++); print(row, col); } return 0; }