## 10189 - Minesweeper

Moderator: Board moderators

Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:
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?

[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]

titid_gede
Experienced poster
Posts: 187
Joined: Wed Dec 11, 2002 2:03 pm
Location: Mount Papandayan, Garut
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
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.)!!
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

[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
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:

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
Contact:
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:
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
Contact:
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?

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
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
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
thanks got accepted
mooseelkdog
New poster
Posts: 18
Joined: Fri Oct 10, 2003 8:46 am
Location: Airway Heights

### 10189 Comile errer?

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