Here's my code:
- Code: Select all
Code removed after AC
Moderator: Board moderators
Code removed after AC
#include<iostream>
using namespace std;
int check(int i, int j, int x, int y, char b[][105]);
main()
{
int i,j,k=0,m,n,z;
char a[105][105];
while(1)
{
cin >> m >> n;
if(m==0&&n==0)
break;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin >> a[i][j];
k++;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(a[i][j]!='*')
{
z=check(i,j,m-1,n-1,a);
a[i][j]=z;
}
cout << "Field #" << k << ":\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout << a[i][j];
cout << "\n";
}
cout << "\n";
}
}
int check(int i, int j, int x, int y, char b[][105])
{
int k=i-1,l=i+1,m=j-1,n=j+1,p=0,q,w;
char a[105][105];
for(q=0;q<=x;q++)
for(w=0;w<=y;w++)
a[q][w]=b[q][w];
if(i==0)
k=0;
if(i==x)
l=i;
if(j==0)
m=0;
if(j==y)
n=j;
if(a[k][m]=='*')
{
a[k][m]=-1;
p++;
}
if(a[i][m]=='*')
{
a[i][m]=-1;
p++;
}
if(a[l][m]=='*')
{
a[l][m]=-1;
p++;
}
if(a[k][j]=='*')
{
a[k][j]=-1;
p++;
}
if(a[l][j]=='*')
{
a[l][j]=-1;
p++;
}
if(a[k][n]=='*')
{
a[k][n]=-1;
p++;
}
if(a[i][n]=='*')
{
a[i][n]=-1;
p++;
}
if(a[l][n]=='*')
{
a[l][n]=-1;
p++;
}
return (p+48);
}# include <iostream>
using namespace std;
int main()
{
int numberOfRows,numberOfColumns,counter=1;
while(cin>>numberOfRows>>numberOfColumns)
{
if (numberOfRows == 0 && numberOfColumns == 0)
break;
char board[110][110];
int MS[110][110]={0};
for (int i=0;i<numberOfRows;i++)
for (int j=0;j<numberOfColumns;j++)
cin>>board[i][j];
for(int i=0;i<numberOfRows;i++)
{
for (int j=0;j<numberOfColumns;j++)
{
int a=i+1,b=j+1;
if (board[i][j] == '*')
{
MS[a][b+1]++;
MS[a][b-1]++;
MS[a-1][b]++;
MS[a+1][b]++;
MS[a-1][b+1]++;
MS[a-1][b-1]++;
MS[a+1][b+1]++;
MS[a+1][b-1]++;
}
}
}
cout<<"Field #"<<counter<<':'<<endl;
for(int i=0;i<numberOfRows;i++)
{
for (int j=0;j<numberOfColumns;j++)
{
int a=i+1,b=j+1;
if (board[i][j] == '*')
cout<<board[i][j];
else
cout<<MS[a][b];
}
cout<<endl;
}
cout<<endl;
counter++;
}
return 0;
}There must be an empty line between field outputs.
Jan wrote:The problem states...There must be an empty line between field outputs.
But you are printing an empty line after each input set.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int r,c,k;
char **ch;
for(k=1;;k++)
{
scanf("%d %d",&r,&c);
if(r==0&&c==0)
{
break;
}
int i,j,count;
ch=(char **)malloc(r*sizeof(char*));
for(i=0;i<r;i++)
{
ch[i]=(char *)malloc(c+1);
}
for(i=0;i<r;i++)
{
scanf("%s",ch[i]);
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
count=0;
if(ch[i][j]=='.')
{
if(i+1<r&&ch[i+1][j]=='*')
{
count++;
}
if(j+1<r&&ch[i][j+1]=='*')
{
count++;
}
if(i+1<r&&j+1<c&&ch[i+1][j+1]=='*')
{
count++;
}
if(i+1<r&&j-1>=0&&ch[i+1][j-1]=='*')
{
count++;
}
if(i-1>=0&&j+1<c&&ch[i-1][j+1]=='*')
{
count++;
}
if(i-1>=0&&ch[i-1][j]=='*')
{
count++;
}
if(j-1>=0&&ch[i][j-1]=='*')
{
count++;
}
if(i-1>=0&&j-1>=0&&ch[i-1][j-1]=='*')
{
count++;
}
ch[i][j]=count+'0';
}
}
}
printf("\nField #%d:\n",k);
for(i=0;i<r;i++)
{
printf("%s\n",ch[i]);
}
for(i=0;i<r;i++)
{
free(ch[i]);
}
free(ch);
}
return 0;
}if(j+1<r&&ch[i][j+1]=='*') // why j+1<r? shouldn't it be j+1<c?int pos(int x,int y)
{
if(x>=0 && y>=0 && x<r && y<c)
if(array[x][y]=='*') return 1;
return 0;
}Users browsing this forum: No registered users and 1 guest