Page 20 of 28

Re: 10189 - Minesweeper

Posted: Fri Jan 25, 2013 9:59 pm
by brianfry713
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.

Re: 10189 - Minesweeper

Posted: Sat Jan 26, 2013 5:50 pm
by Safio
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;
        }
    }
}

Re: 10189 - Minesweeper

Posted: Mon Jan 28, 2013 10:43 pm
by brianfry713
Try replacing the ' on line 33 with "

Re: 10189 - Minesweeper

Posted: Tue Jan 29, 2013 8:02 am
by Safio
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?

Re: 10189 - Minesweeper

Posted: Tue Jan 29, 2013 11:01 pm
by brianfry713
Don't print a blank line at the start of the output. Post your updated code if you still need help.

Re: 10189 - Minesweeper

Posted: Wed Jan 30, 2013 9:59 pm
by morgan2000
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 .

Re: 10189 - Minesweeper

Posted: Thu Jan 31, 2013 9:14 pm
by brianfry713
There must be an empty line between field outputs. Don't print an extra blank line at the end.

Re: 10189 - Minesweeper

Posted: Fri Feb 01, 2013 12:30 am
by firouzyan
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;
}

Re: 10189 - Minesweeper

Posted: Fri Feb 01, 2013 8:36 am
by morgan2000
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 .

Re: 10189 - Minesweeper

Posted: Fri Feb 01, 2013 10:02 am
by brianfry713
there should be a newline at the end of the last line.

Re: 10189 - Minesweeper

Posted: Thu Mar 07, 2013 6:49 pm
by clienn
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

Re: 10189 - Minesweeper

Posted: Fri Mar 08, 2013 12:48 am
by brianfry713
There must be an empty line between field outputs. You're printing two on the sample input.

Re: 10189 - Minesweeper

Posted: Wed Apr 03, 2013 12:19 am
by Devil_Bird
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

Re: 10189 - Minesweeper

Posted: Wed Apr 03, 2013 11:02 pm
by brianfry713
There must be an empty line between field outputs. Don't print an extra blank line at the end.

Re: 10189 - Minesweeper

Posted: Fri Apr 05, 2013 1:31 pm
by Devil_Bird
thanks a lot brianfry713 ur the best :)