Re: 10189 - Minesweeper
Posted: Thu Dec 27, 2012 2:42 pm
Code: Select all
solved the problem
Code: Select all
solved the problem
Code: Select all
solved the problem
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j;
int fieldnum = 1;
int n, m;
while( scanf("%d %d", &n, &m) )
{
if( n==0 && m==0 )
break;
char **field =(char **)malloc( n*sizeof(char *) );
for( i=0; i<n; i++ )
{
field[i] = (char *)malloc( (m+1)*sizeof(char) );
}
for( i=0; i<n; i++ )
{
scanf("%s", field[i]);
}
for( i=0; i<n; i++ )
{
for( j=0; j<m; j++ )
{
if( field[i][j] == '*' )
{
continue;
}
else
{
int count = 0;
if( i-1>=0 )
{
if( j-1>=0 && field[i-1][j-1]=='*' )
{
count++;
}
if( field[i-1][j]=='*' )
{
count++;
}
if( j+1 < m && field[i-1][j+1]=='*' )
{
count++;
}
}
if( i+1 < n )
{
if( j-1>=0 && field[i+1][j-1]=='*' )
{
count++;
}
if( field[i+1][j]=='*' )
{
count++;
}
if( j+1 < m && field[i+1][j+1]=='*' )
{
count++;
}
}
{
if( j-1>=0 && field[i][j-1]=='*' )
{
count++;
}
if( field[i][j]=='*' )
{
count++;
}
if( j+1 < m && field[i][j+1]=='*' )
{
count++;
}
}
field[i][j] = count + '0';
}
}
}
printf("Field #%d:\n", fieldnum);
for( i=0; i<n; i++ )
{
printf("%s\n", field[i]);
}
printf("\n");
fieldnum++;
}
return 0;
}
Code: Select all
#include<stdio.h>
int main()
{
char inp[101][101];
int m,n,i,j,count=0;
while(1)
{
int out[101][101]={0};
scanf("%d %d",&m, &n);
if(m==0 && n==0)
return 0;
else
{
for(i=0;i<m;i++)
scanf("%s",inp[i]);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(inp[i][j]=='*')
{
out[i-1][j-1]++;
out[i-1][j]++;
out[i-1][j+1]++;
out[i][j-1]++;
out[i][j+1]++;
out[i+1][j-1]++;
out[i+1][j]++;
out[i+1][j+1]++;
}
}
}
if(count!=0)
printf("\n");
printf("Field #%d:\n",++count);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(inp[i][j]=='*')
printf("*");
else
printf("%d",out[i][j]);
}
printf("\n");
}
}
return 0;
}
Code: Select all
#include <iostream>
using namespace std;
int main(){
int a, b, c, d, f=1;
char game[101][101];
while(cin>>a>>b && a!=0 && b!=0){
int bom=0, bomb[101]; // put here so that it resets
for(c=0;c<a;c++){ // save in array
cin>>game[c];
for(d=0;d<b;d++){ // store position of bomb and rename others
if(game[c][d]=='*'){
bomb[bom]=c*10+d;
bom++;
}else{
game[c][d]='0';
}
}
}
for(c=0;c<bom;c++){ // defining numbers based on bombs
int x2=bomb[c]/10, x1=x2-1, x3=x2+1, y2=bomb[c]%10, y1=y2-1, y3=y2+1;
game[x1][y1]=='*'?:game[x1][y1]++;
game[x1][y2]=='*'?:game[x1][y2]++;
game[x1][y3]=='*'?:game[x1][y3]++;
game[x2][y1]=='*'?:game[x2][y1]++;
game[x2][y3]=='*'?:game[x2][y3]++;
game[x3][y1]=='*'?:game[x3][y1]++;
game[x3][y2]=='*'?:game[x3][y2]++;
game[x3][y3]=='*'?:game[x3][y3]++;
}
cout<<"\nField #"<<f++<<endl;
for(c=0;c<a;c++){
for(d=0;d<b;d++){
cout<<game[c][d];
}
cout<<endl;
}
cout<<endl;
}
return 0;
}
What's the difference between an empty line and a blank line? How do I create an empty line? (cldn find it on google)brianfry713 wrote:There must be an empty line between field outputs.
Don't print a blank line at the end.
Code: Select all
Field #1
*100
2210
1*10
1110
Field #2
**100
33200
1*100
Code: Select all
Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100
Code: Select all
#include <iostream>
using namespace std;
int main(){
int a, b, c, d, f=1;
char game[102][102];
while(cin>>a>>b && a!=0 && b!=0){
int bom=0, bomb[10002]; // put here so that it resets
for(c=0;c<a;c++){ // save in array
cin>>game[c];
for(d=0;d<b;d++){ // store position of bomb and rename others
if(game[c][d]=='*'){
bomb[bom]=c*10+d;
bom++;
}else{
game[c][d]='0';
}
}
}
for(c=0;c<bom;c++){ // defining numbers based on bombs
int x2=bomb[c]/10, x1=x2-1, x3=x2+1, y2=bomb[c]%10, y1, y3=y2+1;
y2==0?y1=b:y1=y2-1; // when output y1=-1 there will be error. If y=0, it will increase first row
game[x1][y1]=='*'?:game[x1][y1]++;
game[x1][y2]=='*'?:game[x1][y2]++;
game[x1][y3]=='*'?:game[x1][y3]++;
game[x2][y1]=='*'?:game[x2][y1]++;
game[x2][y3]=='*'?:game[x2][y3]++;
game[x3][y1]=='*'?:game[x3][y1]++;
game[x3][y2]=='*'?:game[x3][y2]++;
game[x3][y3]=='*'?:game[x3][y3]++;
}
cout<<"\nField #"<<f++<<":"<<endl;
for(c=0;c<a;c++){
for(d=0;d<b;d++){
cout<<game[c][d];
}
cout<<endl;
}
cout<<endl;
}
}