Page 19 of 28

Re: 10189 - Minesweeper

Posted: Thu Dec 27, 2012 2:42 pm
by badc0re

Code: Select all

solved the problem
Got WA, any hints or ideas?

Re: 10189 - Minesweeper

Posted: Sat Dec 29, 2012 9:57 pm
by brianfry713
line 13: error: ‘memset’ was not declared in this scope, include <string.h>
line 15: warning: left-hand operand of comma has no effect
line 33: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’

There must be an empty line between field outputs.
Don't print a blank line at the end.

Re: 10189 - Minesweeper

Posted: Sun Dec 30, 2012 12:48 am
by badc0re

Code: Select all

solved the problem
Changed the code a bit but still no changes RA.

Re: 10189 - Minesweeper

Posted: Sun Dec 30, 2012 9:03 am
by brianfry713
There must be an empty line between field outputs.
Don't print a blank line at the end.

10189: Wrong answer

Posted: Thu Jan 10, 2013 8:41 am
by Sylla Zhang
my code is:

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;
}

I don't know why I got "wrong answer"?

Re: 10189: Wrong answer

Posted: Thu Jan 10, 2013 10:55 pm
by brianfry713
There must be an empty line between field outputs.
Don't print a blank line after the last field.

10189 - Minesweeper

Posted: Fri Jan 18, 2013 9:26 pm
by enamsustcse
#include <stdio.h>
#define sz 150

int graph[sz][sz];
char input[sz][sz];

int main()
{
int caseno=1, i, j, k, m, n, p, q;
scanf("%d %d", &m, &n);
while(m&&n)
{
for (i=0; i<m; i++)
for (j=0; j<n; j++)
graph[j] = 0;

for (i=0; i<m; i++)
{
getchar();
for (j=0; j<n; j++)
scanf("%c", &input[j]);

}
printf("\n");

for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
if(input[j]=='*')
{
for (p = i-1; p<=(i+1); p++)
{
for (q = j-1; q<=j+1; q++)
{
if(p>=0&&q>=0)
{
++graph[p][q];
}
}
}
}
}
}
printf("Field #%d:\n", caseno++);
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
if(input[j] == '*') printf("*");
else printf("%d", graph[j]);
}
printf("\n");
}
scanf("%d %d", &m, &n);
if(m&&n) printf("\n");
}
return 0;
}

Why getting WA???? brainfry guru or anyother expert, pleeeeeeeeeeeeeeeease, help me!!!!!!!!!!!!!
:oops: :oops: :oops:

Re: 10189: Wrong answer

Posted: Fri Jan 18, 2013 9:32 pm
by brianfry713
There must be one empty line between field outputs, you're printing two.

10189 - Minesweeper: Runtime Error

Posted: Mon Jan 21, 2013 4:50 pm
by adityaarun1
I wrote the following code for the problem and every time I submit to the judge, it gives me runtime error, though the same code was accepted in http://www.programming-challenges.com (http://www.programming-challenges.com/p ... ormat=html). :-? Can anyone please tell me what is going wrong. Thanks

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;
}

Re: 10189 - Minesweeper: Runtime Error

Posted: Mon Jan 21, 2013 10:16 pm
by brianfry713
If i=0 don't write to out[i-1][j-1]

Re: 10189 - Minesweeper

Posted: Tue Jan 22, 2013 5:09 pm
by Safio
I'm kinda new to c++. Anyway, I've been trying out UVa and tried solving problems. However, I received a runtime error when I submitted it. I tried to find the error but could not. Could someone help me debug the error? Your help would be much appreciated.

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;
}

Re: 10189 - Minesweeper

Posted: Tue Jan 22, 2013 10:45 pm
by brianfry713
There must be an empty line between field outputs.
Don't print a blank line at the end.

On a 100 by 100 field there could be up to 10000 mines, you only have room for 101 in your bomb array.

Also make sure you're not reading or writing game[-1][-1]

Re: 10189 - Minesweeper

Posted: Wed Jan 23, 2013 7:08 am
by Safio
brianfry713 wrote:There must be an empty line between field outputs.
Don't print a blank line at the end.
What's the difference between an empty line and a blank line? How do I create an empty line? (cldn find it on google)

Re: 10189 - Minesweeper

Posted: Thu Jan 24, 2013 2:43 am
by brianfry713
An empty line and a blank line are the same thing. To print one you can use cout << endl;

For the sample input, you're printing:

Code: Select all

Field #1
*100
2210
1*10
1110


Field #2
**100
33200
1*100

The correct output is:

Code: Select all

Field #1:
*100
2210
1*10
1110

Field #2:
**100
33200
1*100

Re: 10189 - Minesweeper

Posted: Fri Jan 25, 2013 3:45 pm
by Safio
I have done what you've suggested but still received runtime error. Could you advice on how I can make it accepted? Here is my modified code:

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;
    }
}