Code: Select all
Code removed after AC
Moderator: Board moderators
Code: Select all
Code removed after AC
Code: Select all
#include<stdio.h>
int height,width;
char map[101][101];
int caseNo=1;
void readMap(void);
void calcMap(void);
void printMap(void);
int main(int argc, char** argv){
while(1){
readMap();
if(height==0 && width==0)
break;
calcMap();
printMap();
}
return 0;
}
void readMap(void){
int i;
scanf("%d %d",&height,&width);
for(i=0;i<height;i++){
scanf("%s",&map[i]);
}
}
void calcMap(void){
int y, x, count;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
if (map[y][x] == '.') {
count = 0;
if (y == 0) {
if (x == 0) {
if (map[y][x + 1] == '*') {
count++;
}
if (map[y + 1][x] == '*') {
count++;
}
if (map[y + 1][x + 1] == '*') {
count++;
}
} else if (x < width - 1) {
if (map[y][x - 1] == '*') {
count++;
}
if (map[y][x + 1] == '*') {
count++;
}
if (map[y + 1][x - 1] == '*') {
count++;
}
if (map[y + 1][x] == '*') {
count++;
}
if (map[y + 1][x + 1] == '*') {
count++;
}
} else {
if (map[y][x - 1] == '*') {
count++;
}
if (map[y + 1][x - 1] == '*') {
count++;
}
if (map[y + 1][x] == '*') {
count++;
}
}
} else if (y < height - 1) {
if (x == 0) {
if (map[y - 1][x] == '*') {
count++;
}
if (map[y - 1][x + 1] == '*') {
count++;
}
if (map[y][x + 1] == '*') {
count++;
}
if (map[y + 1][x] == '*') {
count++;
}
if (map[y + 1][x + 1] == '*') {
count++;
}
} else if (x < width - 1) {
if (map[y - 1][x - 1] == '*') {
count++;
}
if (map[y - 1][x] == '*') {
count++;
}
if (map[y - 1][x + 1] == '*') {
count++;
}
if (map[y][x - 1] == '*') {
count++;
}
if (map[y][x + 1] == '*') {
count++;
}
if (map[y + 1][x - 1] == '*') {
count++;
}
if (map[y + 1][x] == '*') {
count++;
}
if (map[y + 1][x + 1] == '*') {
count++;
}
} else {
if (map[y - 1][x - 1] == '*') {
count++;
}
if (map[y - 1][x] == '*') {
count++;
}
if (map[y][x - 1] == '*') {
count++;
}
if (map[y + 1][x - 1] == '*') {
count++;
}
if (map[y + 1][x] == '*') {
count++;
}
}
} else {
if (x == 0) {
if (map[y - 1][x] == '*') {
count++;
}
if (map[y - 1][x + 1] == '*') {
count++;
}
if (map[y][x + 1] == '*') {
count++;
}
} else if (x < width - 1) {
if (map[y - 1][x - 1] == '*') {
count++;
}
if (map[y - 1][x] == '*') {
count++;
}
if (map[y - 1][x + 1] == '*') {
count++;
}
if (map[y][x - 1] == '*') {
count++;
}
if (map[y][x + 1] == '*') {
count++;
}
} else {
if (map[y - 1][x - 1] == '*') {
count++;
}
if (map[y - 1][x] == '*') {
count++;
}
if (map[y][x - 1] == '*') {
count++;
}
}
}
map[y][x] = count + '0';
}
}
}
}
void printMap(void){
int i,j;
if(caseNo>1)
printf("\n");
printf("Field #%d:\n",caseNo++);
for(i=0;i<height;i++){
for(j=0;j<width;j++){
printf("%c",map[i][j]);
}
printf("\n");
}
}
Code: Select all
#include <iostream>
#include <string.h>
using namespace std;
char ch[101][101];
int mat[101][101] , dir[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}} , n , m ;
void f()
{
memset( mat , 0 , sizeof( mat ) );
int i , j , k , r , s;
for( i = 0 ; i < n ; i ++ )
for( j = 0 ; j < m ; j ++ )
for( k = 0 ; k < 8 ; k ++ )
{
r = i + dir[k][0];
s = j + dir[k][1];
if( ( r >= 0 && s >= 0 && r < n && s < m ) && ch[i][j] == '*' )
mat[r][s] ++;
}
}
int main()
{
int i , j , c = 1;
while( cin >> n >> m , n || m )
{
for( i = 0 ; i < n ; i ++ )
for( j = 0 ; j < m ; j ++ )
cin >> ch[i][j];
f();
cout << "Field #" << c ++ << ':' << endl;
for( i = 0 ; i < n ; i ++ )
{
for( j = 0 ; j < m ; j ++ )
{
if( ch[i][j] == '*' )
cout << '*';
else
cout << mat[i][j];
}
cout << endl;
}
cout << endl;
}
return 0;
}
Code: Select all
#include <iostream>
using namespace std;
int main()
{
int dx[] = { 1, 1, 1, 0, 0, -1, -1, -1 };
int dy[] = { 1, -1, 0, 1, -1, 1, 0, -1 };
int n, m, x = 1;
char aux;
while(1)
{
cin >>n >> m;
if(n == 0 && m == 0)return 0;
char arr[102][102];
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
{
cin >> aux;
if(aux == '.')
arr[i][j]='0';
else
arr[i][j]= aux;
}
}
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
{
if(arr[i][j] != '*')continue;
for(int x = 0; x < 8; ++x)
{
if(arr[i+dx[x]][j+dy[x]] != '*')
arr[i+dx[x]][j+dy[x]]++;
}
}
}
cout << "Field #" << x <<':'<<endl;
++x;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
{
cout << arr[i][j];
}
cout << endl;
}
cout << endl;
}
}