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

Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise » Sun Aug 24, 2003 6:26 pm

Well, don't care too much about PE, if adding or taking off a newline doesn't solve it. It might be that the judge output has itself a PE compared to the theorical output (it already happened) :)

zilnus
New poster
Posts: 9
Joined: Sat Mar 08, 2003 11:59 am

10189 Why WA?

Post by zilnus » Thu Sep 04, 2003 9:20 am

[c]
int main()
{
int i,j,k,l,m,a,b;
int win[200][200];
int nextx[8] = {-1,-1,-1,0,0,1,1,1};
int nexty[8] = {-1,0,1,-1,1,-1,0,1};
char string[150];
int kasus = 1;

while ((i != 0)&&(j != 0))
{
scanf("%d %d",&i,&j);

for (a=0;a<105;a++)
for (b=0;b<105;b++)
win[a] = 0;

for (k=1;k<=i;k++) {
scanf("%s",string);
for (l=0;l<j;l++) {
if (string[l] == '*') win[k][l+1] = -1;
}
}

for (k=1;k<=i;k++){
for (l=1;l<=j;l++){
for (m=0;m<8;m++) {
if (win[k][l] != -1) {
if (win[k + nextx[m] ][l + nexty[m] ] == -1) win[k][l] = win[k][l] + 1;
}
}
}
}

if ((i==0)&&(j==0)) exit(-1);
printf ("Field #%d:\n",kasus);
kasus++;
for (k=1;k<=i;k++) {
for (l=1;l<=j;l++) {
if (win[k][l] == -1) printf ("*");
else printf ("%d",win[k][l]);
}
printf ("\n");
}
}

return 0;
}[/c]

Anyone please help me ? Why wrong answer ???

titid_gede
Experienced poster
Posts: 187
Joined: Wed Dec 11, 2002 2:03 pm
Location: Mount Papandayan, Garut

Post by titid_gede » Thu Sep 04, 2003 2:52 pm

hi zilnus... you're from bandung, rite?

looking at the code.. when i == 0 and j == 0, your code doesnt immediately break, but read input first, perhaps it can cause error..

regards,
titid
Kalo mau kaya, buat apa sekolah?

Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan » Fri Sep 05, 2003 9:35 am

You just have to change the part:
[c]
while ((i != 0)&&(j != 0))
{
scanf("%d %d",&i,&j);
[/c]

with

[c]
while(scanf("%i %i",&i,&j)){
if(!i&&!j) break;
[/c]

That'll solve it (but very possibly with P.E.)!! :wink: :wink:
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.

Eva
New poster
Posts: 14
Joined: Thu Apr 10, 2003 11:31 am
Location: Bombay
Contact:

10189 trouble

Post by Eva » Thu Sep 18, 2003 7:56 pm

[cpp]
#include<iostream.h>


bool mnsp[101][101];
int m,n;


inline void set(void)
{
memset(mnsp,0,sizeof(mnsp));
}


int check(int a,int b)
{
int t = 0;

if(mnsp[a-1][b-1] == 1)
t++;
if(mnsp[a-1] == 1)
t++;
if(mnsp[a-1][b+1] == 1)
t++;
if(mnsp[a][b-1] == 1)
t++;
if(mnsp[a][b+1] == 1)
t++;
if(mnsp[a+1][b-1] == 1)
t++;
if(mnsp[a+1] == 1)
t++;
if(mnsp[a+1][b+1] == 1)
t++;

return t;
}



int main()
{
int count = 1;
cin >> m >> n;

while(m || n)
{
set();

cout << "Field #" << count << ':' << endl;


for(int i=1;i<=m;++i)
{
for(int j=1;j<=n;++j)
{
char ch;
cin >> ch;

if(ch == '*')
mnsp[j] = 1;
}
}

for(int i=1;i<=m;++i)
{
for(int j=1;j<=n;++j)
if(mnsp[j] == 0)
cout << check(i,j);
else
cout << '*';

cout << endl;
}

cout << endl;

cin >> m >> n;
count++;
}

return 0;
}[/cpp]
My own quote:

We are here as Adam and Eve were here!

Per
A great helper
Posts: 429
Joined: Fri Nov 29, 2002 11:27 pm
Location: Sweden

Post by Per » Fri Sep 19, 2003 12:34 am

You'll have trouble when the minefield is 100x100, your matrix is too small.

Eva
New poster
Posts: 14
Joined: Thu Apr 10, 2003 11:31 am
Location: Bombay
Contact:

Post by Eva » Fri Oct 03, 2003 10:53 am

Sorry for my late reply :(

Actually it was Compile Error,
and I am not sure why :(
My own quote:

We are here as Adam and Eve were here!

Subeen
Experienced poster
Posts: 127
Joined: Tue Nov 06, 2001 2:00 am
Location: Bangladesh
Contact:

Post by Subeen » Sat Oct 04, 2003 10:57 am

If you use the function memset() you have to include <string.h> header file.

Eva
New poster
Posts: 14
Joined: Thu Apr 10, 2003 11:31 am
Location: Bombay
Contact:

Post by Eva » Sat Oct 04, 2003 6:43 pm

Subeen wrote:If you use the function memset() you have to include <string.h> header file.
Thank you Subeen, but in my compiler (Borland Cpp) it's ok.
Are you talking about gcc ?
My own quote:

We are here as Adam and Eve were here!

Subeen
Experienced poster
Posts: 127
Joined: Tue Nov 06, 2001 2:00 am
Location: Bangladesh
Contact:

Post by Subeen » Sun Oct 05, 2003 5:06 am

Yes, I am talking about gcc. ( and I also use BC++ :))

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Why WA?

Post by sumankar » Thu Oct 09, 2003 7:26 pm

Code: Select all

[c]
#include<stdio.h>

int main()
{
	int n,m,c,i,j,ncase=0;
	char brd[100][100];

	while( scanf("%d %d",&n,&m) == 2 ) {
		ncase++;
		if( n == 0 && m == 0 )
			exit(0);
		for( i = 0 ; i < n ; i++ )
			for( j = 0 ; j < m ;  )
				if( (c = getchar()) != '\n' ) 
					if( c == '*' || c == '.' )
						brd[i][j++] = c=='.'?'0':c;

		for( i = 0 ; i < n ; i++ ) {
			for( j = 0 ; j < m ; j++ )
				if( brd[i][j] == '*' ){					
					if( i < n-1 && j > 0 && brd[i+1][j-1] != '*' ) brd[i+1][j-1]++;
					if( i < n-1 && j < n-1 && brd[i+1][j+1] != '*' ) brd[i+1][j+1]++;
					if( i < n-1 && brd[i+1][j] != '*' ) brd[i+1][j]++;
					if( i > 0 && brd[i-1][j] != '*' ) brd[i-1][j]++;
					if( i > 0 && j > 0 && brd[i-1][j-1] != '*' ) brd[i-1][j-1]++;
					if( i > 0 && j < n-1 && brd[i-1][j+1] != '*' )brd[i-1][j+1]++;
					if( j < n-1 && brd[i][j+1] != '*' ) brd[i][j+1]++;
					if( j > 0 && brd[i][j-1] != '*' ) brd[i][j-1]++;
				}
		}
		printf("Field #%d:\n",ncase);
		for( i = 0 ; i < n ; i++ ) {
			for( j = 0 ; j < m ; j++ )
				printf("%c",brd[i][j]);
			printf("\n");
		}
		printf("\n");
	}
	return 0;
}
[/c]
Any help is appreciated .
Suman.

r.z.
Learning poster
Posts: 56
Joined: Thu Jun 05, 2003 1:57 pm

Post by r.z. » Sun Nov 16, 2003 3:36 pm

I still don't know why is my code resulting WA

the sampe input and output is correct, maybe I missed something?

[c]
#include<stdio.h>
#include<string.h>

void main()
{
int n,m;
int i,j;
int k,l;
int ctr;
int num;
char mat[100][100];

num=1;

/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/

do
{

memset(mat,'0',sizeof(mat));
scanf("%d %d\n",&n,&m);
if(n==0 && m==0)
break;


for(i=0;i<n;i++)
{
gets(mat);
}

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
ctr=0;
if(mat[j]!='*')
{
for(k=i-1;k<=i+1;k++)
{
for(l=j-1;l<=j+1;l++)
{
if(mat[k][l]=='*')
ctr++;
}
}
mat[j]=ctr+'0';
}
}
}

printf("Field #%d:\n",num);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%c",mat[j]);
}
printf("\n");
}
num++;
printf("\n");

}while(n!=0 && m!=0);

/*
fcloseall();
*/

}

[/c]

Hisoka
Experienced poster
Posts: 120
Joined: Wed Mar 05, 2003 10:40 am
Location: Indonesia

Post by Hisoka » Sun Nov 16, 2003 9:29 pm

maybe like this :

1. array [100][101] => because of string => "aaaa\0" => NULL character
2. when i || j == 0 => k=-1 || l=-1.
3. when i || j == 100 => k=101 || l=101.

I just found this...

r.z.
Learning poster
Posts: 56
Joined: Thu Jun 05, 2003 1:57 pm

Post by r.z. » Mon Nov 17, 2003 1:46 am

thanks got accepted
I forgot about '\0'

mooseelkdog
New poster
Posts: 18
Joined: Fri Oct 10, 2003 8:46 am
Location: Airway Heights

10189 Comile errer?

Post by mooseelkdog » Wed Dec 03, 2003 10:44 am

I keep getting a compile error, but I can compile and run my program with no error, can anyone explain why (I use g++)?

Thank you,

med
PS - I can send the code if anyone wants me to

Post Reply

Return to “Volume 101 (10100-10199)”