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

jackpigman
New poster
Posts: 8
Joined: Fri Jan 04, 2008 5:57 pm

Post by jackpigman »

Thanks a lot Samiul :D !! Got an AC after changing the code.
ChainRule
New poster
Posts: 4
Joined: Sun Nov 18, 2007 9:07 am

I got WA,plz help

Post by ChainRule »

I've tried all the data but i still got WA :cry:
plz help me

Code: Select all

remove after AC
Last edited by ChainRule on Wed Jan 09, 2008 5:12 pm, edited 1 time in total.
Samiul
New poster
Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm

Post by Samiul »

Instead of [0][0] to [n - 1][n - 1] , put values from [1][1] to [n][n]. Then you woudn't need to check the boundary conditions, it would reduce the chance of making mistakes.
ChainRule
New poster
Posts: 4
Joined: Sun Nov 18, 2007 9:07 am

Still WA

Post by ChainRule »

Well,
I've changed the code,
but I still get WA :cry:

Code: Select all

remove after AC
Last edited by ChainRule on Wed Jan 09, 2008 5:12 pm, edited 1 time in total.
Samiul
New poster
Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm

Post by Samiul »

Increase the size of Temp
ChainRule
New poster
Posts: 4
Joined: Sun Nov 18, 2007 9:07 am

Thx

Post by ChainRule »

I go AC
Thx 8)
hridoy
New poster
Posts: 21
Joined: Tue May 08, 2007 10:30 am
Location: Dhaka
Contact:

424(WA)

Post by hridoy »

WHy am i getting WA?? any help

Code: Select all


#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);
}
karthikeyan1171
New poster
Posts: 3
Joined: Fri Feb 29, 2008 11:39 am

Gtting WA for my Code

Post by karthikeyan1171 »

Hello,
I am getting Wrong Answer for this code. Don't know what is the problem.Please help.
and I tested all the cases which are present in this forum. It has passed all of them.
<Code>
#include<stdio.h>
#include<stdlib.h>
#define MAXFIELD_SIZE 100

struct field {
unsigned int fno;
unsigned int n;
unsigned int m;
};

int main()
{
unsigned int **mw; /* Minesweeper */
unsigned int *a;
unsigned int n,m,i,j,inchar,c=0,x=0;
unsigned int ***wf;

struct field *fp;

fp = (struct field*)malloc(MAXFIELD_SIZE*sizeof(struct field));

scanf("%u %u",&fp[x].n,&fp[x].m);

wf = (unsigned int***) malloc(MAXFIELD_SIZE* sizeof(unsigned int**));

while(fp[x].n != 0 && fp[x].m != 0)
{
fp[x].fno = x; /* Assigning the field no */
n = fp[x].n;
m = fp[x].m;

if (n<=0 && m>100)
exit -1;

a = (unsigned int *) malloc(n * m * sizeof(unsigned int));
if(a == NULL)
{
printf("No memory for Minesweeper\n");
exit -2;
}

mw = (unsigned int**) malloc(n * sizeof(unsigned int*));

if(mw == NULL)
{
printf("No memory for Minesweeper\n");
exit -2;
}

for(i=0;i<n;i++)
mw = a + m *i;

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
while((inchar = getchar()) == ' ' || inchar == '\n' || inchar == '\t');
mw[j] = inchar;
}
}

for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(mw[j] == '.')
mw[j] = 0;
}

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if (mw[j] == '*')
{
if(i>=1)
{
if(mw[i-1][j]!='*')
mw[i-1][j]++;
}

if(i>=1 && j<m-1)
{
if(mw[i-1][j+1]!='*')
mw[i-1][j+1]++;
}

if(i>=1 && j>=1)
{
if(mw[i-1][j-1]!='*')
mw[i-1][j-1]++;
}

if(j<m-1)
{
if(mw[j+1] !='*')
mw[j+1]++;
}

if(j>=1)
{
if(mw[j-1] !='*')
mw[j-1]++;
}

if(i<n-1)
{
if(mw[i+1][j] !='*')
mw[i+1][j]++;
}

if(i<n-1 && j<m-1)
{
if(mw[i+1][j+1] != '*')
mw[i+1][j+1]++;
}

if(i<n-1 && j>=1)
{
if(mw[i+1][j-1]!= '*')
mw[i+1][j-1]++;
}

}
}
}
wf[x]=mw;
x++;
scanf("%u %u",&fp[x].n,&fp[x].m);
}

printf("\n");

/* Printing the output */
for(c=0;c<x;c++)
{
printf("Field #%u:\n",c+1);

mw=wf[c];
n = fp[c].n;
m = fp[c].m;

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mw[j] == '*')
printf("%c",mw[i][j]);
else
printf("%u",mw[i][j]);
}
printf("\n");
}
printf("\n");
}

/*freeing the allocated memory*/
for(c=0;c<x;c++)
free(wf[c]);

free(wf);
free(fp);
return 0;
}
Mohamed Abd El-Monem
New poster
Posts: 15
Joined: Mon Mar 31, 2008 1:20 am
Location: Egypt
Contact:

Re: 10189 awful really awful WA!!!!!!

Post by Mohamed Abd El-Monem »

i get WA and i didn't know my error
help me pleaaas :cry:
this is my code

Code: Select all

# 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;
}
thanx in advance
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 10189 - Minesweeper

Post by Jan »

The problem states...
There must be an empty line between field outputs.
But you are printing an empty line after each input set.
Ami ekhono shopno dekhi...
HomePage
Mohamed Abd El-Monem
New poster
Posts: 15
Joined: Mon Mar 31, 2008 1:20 am
Location: Egypt
Contact:

Re: 10189 - Minesweeper

Post by Mohamed Abd El-Monem »

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.
thanx Jan i get AC in Minesweeper but now I get WA in Mine Sweeper :D
I waiot your advice in the other thread thanx very much
Mohiuddin
New poster
Posts: 6
Joined: Fri Apr 25, 2008 12:09 pm

Re: 10189 - Minesweeper

Post by Mohiuddin »

I can't understand why i m getting w a?
PLZZ help...

here's the code:

Code: Select all

#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;
}
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 10189 - Minesweeper

Post by Jan »

You are not checking the 8 directions correctly. For example,

Code: Select all

if(j+1<r&&ch[i][j+1]=='*') // why j+1<r? shouldn't it be j+1<c?
And of course you can simplify the method by describing a function like

Code: Select all

int pos(int x,int y)
{
    if(x>=0 && y>=0 && x<r && y<c)
        if(array[x][y]=='*') return 1;
    return 0;
}
Now just call the function for all 8 directions. Hope these help.
Ami ekhono shopno dekhi...
HomePage
Mohiuddin
New poster
Posts: 6
Joined: Fri Apr 25, 2008 12:09 pm

Re: 10189 - Minesweeper

Post by Mohiuddin »

still getting w a...what can i do further?where's the mistake? is that in output format?

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

int pos(int x,int y,char **ch,int r,int c)
{
if(x>=0 && y>=0 && x<r && y<c)
{
if(ch[x][y]=='*')
return 1;
}
return 0;
}


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=(char *)malloc(c+1);
}

for(i=0;i<r;i++)
{
scanf("%s",ch);
}

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
count=0;

if(ch[j]=='.')
{
if(pos(i+1,j,ch,r,c))
{
count++;
}
if(pos(i,j+1,ch,r,c))
{
count++;
}
if(pos(i+1,j+1,ch,r,c))
{
count++;
}
if(pos(i+1,j-1,ch,r,c))
{
count++;
}
if(pos(i-1,j+1,ch,r,c))
{
count++;
}
if(pos(i-1,j,ch,r,c))
{
count++;
}
if(pos(i,j-1,ch,r,c))
{
count++;
}
if(pos(i-1,j-1,ch,r,c))
{
count++;
}

ch[j]=count+'0';
}
}
}

printf("\nField #%d:\n",k);

for(i=0;i<r;i++)
{

printf("%s\n",ch);
}

for(i=0;i<r;i++)
{
free(ch);
}

free(ch);
}
return 0;
}
Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10189 - Minesweeper

Post by Obaida »

You are printing a blank line after each input. But this shouldn't be...
There should be blank line after each test case.
It should be like this...

Code: Select all

5 5
                   <<-------a blank line(I used, but you didn't)
*****
*...*
*.*.*
*...*
*****             <<---------no blank line(you are using blank line, but I didn't)
Field #1:
*****
*646*
*4*4*
*646*
*****
4 4

*.*.
.***
**..
.*.*
Field #2:
*4*3
4***
**63
3*3*

Remove the code After Accepted. :D
try_try_try_try_&&&_try@try.com
This may be the address of success.
Post Reply

Return to “Volume 101 (10100-10199)”