10189 - Minesweeper

All about problems in Volume 101. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 » Fri Jan 25, 2013 9:59 pm

brianfry713 wrote: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
You still could be writing to game[-1][y] if x1 is 0.

Your bomb array is not going to work correctly if m>=10. Either make it two dimensional or multiply and divide by at least 101.
Check input and AC output for thousands of problems on uDebug!

Safio
New poster
Posts: 6
Joined: Tue Jan 22, 2013 5:03 pm

Re: 10189 - Minesweeper

Post by Safio » Sat Jan 26, 2013 5:50 pm

Hi, thanks alot for the reply. I tried submitting it without the comment and it worked (lol). However, I still got a wrong answer. I've tried on my local compiler and it works fine. Can I bother you to help me debug the problem again? Also, are there other things that I have to look out for when submitting to the UVa judge?

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[10001][2]; // 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][0]=c;
					bomb[bom][1]=d;
                    bom++;
                }else{
                    game[c][d]='0';
                }
            }
        }
		for(c=0;c<bom;c++){ // defining numbers based on bombs
            int x2=bomb[c][0], x1=x2-1, x3=x2+1, y2=bomb[c][1], 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
			x2==0?x1=a:x1=x2-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++<<':\n';
        for(c=0;c<a;c++){
            for(d=0;d<b;d++){
                cout<<game[c][d];
            }
            cout<<endl;
        }
    }
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 » Mon Jan 28, 2013 10:43 pm

Try replacing the ' on line 33 with "
Check input and AC output for thousands of problems on uDebug!

Safio
New poster
Posts: 6
Joined: Tue Jan 22, 2013 5:03 pm

Re: 10189 - Minesweeper

Post by Safio » Tue Jan 29, 2013 8:02 am

brianfry713 wrote:Try replacing the ' on line 33 with "
I tried but still WA. Sorry to bother you again but could you help me see why it is wrong?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 » Tue Jan 29, 2013 11:01 pm

Don't print a blank line at the start of the output. Post your updated code if you still need help.
Check input and AC output for thousands of problems on uDebug!

morgan2000
New poster
Posts: 2
Joined: Wed Jan 30, 2013 9:46 pm

Re: 10189 - Minesweeper

Post by morgan2000 » Wed Jan 30, 2013 9:59 pm

hi friends .
i solve this problem with this simple code and i test a lot of test cases an i get correct answer for them but uva and programming challenges judges give wrong answer to me :(
if i can't solve this simple problem ...
here is my code in c :

Code: Select all

#include <stdio.h>

int main()
{
    char arr[10100], ch[101];    // array of field squares and ch for getting char from user
    int  n, m, i, j, count, max; // n for hor row and m for ver row and i for array index and j and count for counter and max for maximum squares
    count = 1;
    while(1)
    {
        scanf("%d %d", &n, &m);
        if(n == 0 && m == 0) break;
        max = (m * n);
        i = 0;
        for(n; n > 0; n --)
        {
            scanf("%s", ch);
            j = 0;
            for(j; j < m; j ++)
            {
                if(ch[j] == '*') arr[i] = '*';
                else arr[i] = '0';
                i ++;
            }
        }
        printf("\n");
        i = 0;
        for(i; i < max; i ++)
        {
            if(arr[i] == '*')
            {
                if((i + 1) % m == 0)
                {
                    // min
                    if(i - m - 1 > -1 && arr[i - m - 1] != '*') arr[i - m - 1] += 1;
                    if(i - m > -1 && arr[i - m] != '*') arr[i - m] += 1;
                    if(i - 1 > -1 && arr[i - 1] != '*') arr[i - 1] += 1;
                    // max
                    if(i + m - 1 < max && arr[i + m - 1] != '*') arr[i + m - 1] += 1;
                    if(i + m < max && arr[i + m] != '*') arr[i + m] += 1;
                }
                else if(i % m == 0)
                {
                    // min
                    if(i - m > -1 && arr[i - m] != '*') arr[i - m] += 1;
                    if(i - m + 1 > -1 && arr[i - m + 1] != '*') arr[i - m + 1] += 1;
                    // max
                    if(i + 1 < max && arr[i + 1] != '*') arr[i + 1] += 1;
                    if(i + m < max && arr[i + m] != '*') arr[i + m] += 1;
                    if(i + m + 1 < max && arr[i + m + 1] != '*') arr[i + m + 1] += 1;

                }
                else
                {
                    // min
                    if(i - m - 1 > -1 && arr[i - m - 1] != '*') arr[i - m - 1] += 1;
                    if(i - m > -1 && arr[i - m] != '*') arr[i - m] += 1;
                    if(i - m + 1 > -1 && arr[i - m + 1] != '*') arr[i - m + 1] += 1;
                    if(i - 1 > -1 && arr[i - 1] != '*') arr[i - 1] += 1;
                    // max
                    if(i + 1 < max && arr[i + 1] != '*') arr[i + 1] += 1;
                    if(i + m - 1 < max && arr[i + m - 1] != '*') arr[i + m - 1] += 1;
                    if(i + m < max && arr[i + m] != '*') arr[i + m] += 1;
                    if(i + m + 1 < max && arr[i + m + 1] != '*') arr[i + m + 1] += 1;
                }
            }
        }
        i = 0;
        printf("Field #%d:\n", count);
        for(i; i < max; i ++)
        {
            printf("%c", arr[i]);
            if((i + 1) % m == 0) printf("\n");
        }
        printf("\n");
        count ++;
    }
	return 0;
}
thanks a lot .
sorry for bad english grammar .
i don't know english enough .
have a nice day .

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 » Thu Jan 31, 2013 9:14 pm

There must be an empty line between field outputs. Don't print an extra blank line at the end.
Check input and AC output for thousands of problems on uDebug!

firouzyan
New poster
Posts: 1
Joined: Fri Feb 01, 2013 12:24 am

Re: 10189 - Minesweeper

Post by firouzyan » Fri Feb 01, 2013 12:30 am

Hello every one!
I write a code for 10189 - Minesweeper but every time i submit it said "Wrong Answer"!
I don't know what to do?
This is a code:

Code: Select all

#include <stdio.h>
int main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	int i,j,count;
	char graph[102][102];
	count=1;
	int loop1,loop2,loop3,loop4;	
	scanf("%d %d\n",&i,&j);
A:
	for (loop1=1;loop1<=i;loop1++){
		for(loop2=1;loop2<=j;loop2++){
			scanf("%c",&graph[loop1][loop2]);
			if (graph[loop1][loop2]==46) graph[loop1][loop2]=48;
		}
		scanf("\n");
	}
	for (loop1=1;loop1<=i;loop1++){
			for(loop2=1;loop2<=j;loop2++){
				if (graph[loop1][loop2]=='*'){
					for (loop3=loop1-1;loop3<=loop1+1;loop3++){
						for (loop4=loop2-1;loop4<=loop2+1;loop4++){
							if (graph[loop3][loop4]=='*') continue;
							graph[loop3][loop4]++;
						}
					}
				}
			}
		}
	printf("Field #%d:\n",count);
	for (loop1=1;loop1<=i;loop1++){
		for (loop2=1;loop2<=j;loop2++){
			printf("%c",graph[loop1][loop2]);
		}
			if (loop1!=i) printf("\n");
	}
	scanf("%d %d",&i,&j);
	if ((i!=0)||(j!=0)){
		printf("\n\n");
		scanf("\n");
		count++;
		goto A;
	}
	return 0;
}

morgan2000
New poster
Posts: 2
Joined: Wed Jan 30, 2013 9:46 pm

Re: 10189 - Minesweeper

Post by morgan2000 » Fri Feb 01, 2013 8:36 am

brianfry713 wrote:There must be an empty line between field outputs. Don't print an extra blank line at the end.
i don't know where to change :(
i do some edit but again i get wrong answer .
please help !
thanks a lot .

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 » Fri Feb 01, 2013 10:02 am

there should be a newline at the end of the last line.
Check input and AC output for thousands of problems on uDebug!

clienn
New poster
Posts: 1
Joined: Thu Mar 07, 2013 6:44 pm

Re: 10189 - Minesweeper

Post by clienn » Thu Mar 07, 2013 6:49 pm

I have tried all the test inputs I found here and everything seems to be working fine but I still get WA. Can anyone help me pinpoint the problem?
Here is my code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

#define MINE '*'

int countMines(int x, int y, int w, int h);
char grid[105][105] = {'\0'};

int main()
{
	int w, h, i, j, nField = 1;
	
	while(scanf("%d %d\r", &h, &w) != EOF && w != 0 && h != 0) {
		for (i = 0; i < h; ++i) {
			for (j = 0; j < w; ++j) {
				scanf("%c\r", &grid[i][j]);
			}
		}
		
		if (nField > 1) printf("\n\n");
		
		printf("Field #%d:\n", nField++);
		for (i = 0; i < h; ++i) {
			for (j = 0; j < w; ++j) {
				if (grid[i][j] == MINE) {
					printf("*");
				} else {
					printf("%d", countMines(j, i, w, h));
				}
			}
			printf("\n");
		}
	}
	return 0;
}

int countMines(int x, int y, int w, int h)
{
	int i, n = 0;
	
	int coord[8][2] = {
		{y - 1, x - 1},
		{y - 1, x},
		{y - 1, x + 1},
		{y, x - 1},
		{y, x + 1},
		{y + 1, x - 1},
		{y + 1, x},
		{y + 1, x + 1},
	};
	
	for (i = 0; i < 8; ++i) {
		if ((coord[i][0] >= 0 && coord[i][0] < h) && (coord[i][1] >= 0 && coord[i][1] < w)) {
			if (grid[coord[i][0]][coord[i][1]] == MINE) {
				++n;
			}
		}
	}
	return n;
}
Thanks :p

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 » Fri Mar 08, 2013 12:48 am

There must be an empty line between field outputs. You're printing two on the sample input.
Check input and AC output for thousands of problems on uDebug!

Devil_Bird
New poster
Posts: 10
Joined: Sun Mar 17, 2013 12:02 am

Re: 10189 - Minesweeper

Post by Devil_Bird » Wed Apr 03, 2013 12:19 am

exscuse me Im gettin WA too whit this code:

Code: Select all

#include<iostream>
#include <stdio.h>
using namespace std;
int main()
{
	int n,m;
	char a[200][200],b[200][200];

	int count=0;
	while(cin>>n>>m && ++count)
	{
		if(n==0 && m==0)
			return 0;

		for(int j=0;j<m+2;++j) 
			a[0][j]='a';
		for(int i=0;i<n+2;++i) 
			a[i][0]='a';
		for(int j=0;j<m+2;++j) 
			a[n+1][j]='a';
		for(int i=0;i<n+2;++i) 
			a[i][m+1]='a';

		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				cin>>a[i][j];

		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				if(a[i][j]=='*') b[i][j]='*';
				else
				{
					int mine=0;
					if(a[i+1][j]=='*') mine++;
					if(a[i-1][j]=='*') mine++;
					if(a[i][j+1]=='*') mine++;
					if(a[i+1][j+1]=='*') mine++;
					if(a[i-1][j+1]=='*') mine++;
					if(a[i][j-1]=='*') mine++;
					if(a[i+1][j-1]=='*') mine++;
					if(a[i-1][j-1]=='*') mine++;

					sprintf(&b[i][j],"%d",mine);
				}
			}

			printf("Field #%d:\n",count);
			for(int i=1;i<=n;++i,printf("\n"))
				for(int j=1;j<=m;++j)
					printf("%c",b[i][j]);
			printf("\n");

	}

}
actually I shield the inputs whit character 'a' and for each character '.' checking the square around it
help plz

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 » Wed Apr 03, 2013 11:02 pm

There must be an empty line between field outputs. Don't print an extra blank line at the end.
Check input and AC output for thousands of problems on uDebug!

Devil_Bird
New poster
Posts: 10
Joined: Sun Mar 17, 2013 12:02 am

Re: 10189 - Minesweeper

Post by Devil_Bird » Fri Apr 05, 2013 1:31 pm

thanks a lot brianfry713 ur the best :)

Post Reply

Return to “Volume 101 (10100-10199)”