## 10189 - Minesweeper

Moderator: Board moderators

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
Just check this.

Code: Select all

``if((a[i+1][j-1]=='*')&& i+1< m && j-1>= 0 )``
Suppose j = 0 then you are first trying to check a[][-1] (searching for a negative index), then you are checking whether j-1>=0 or not. So, better to use

Code: Select all

``if(j-1>= 0 && i+1<m && (a[i+1][j-1]=='*'))``
Now, your code first checks whether j-1>=0 or not. If j=0 then other conditions will not be checked.

Another Idea:

You can write a function which will return true if the position is valid and it contains a '*'. Suppose the function is valid(i,j). Then for any valid position(i,j) you can write

Code: Select all

``count = valid(i,j+1) + valid(i+1,j) + valid(i,j-1) + ...``
Then it would be easier to detect error.

Hope these help.

P.S. Dont forget to remove your previous code.
Ami ekhono shopno dekhi...
HomePage

Arashk_kh68
New poster
Posts: 6
Joined: Sun Sep 09, 2007 3:56 pm

### What's wrong?!

Hi, I have tested my code with test cases provided above, and all of them answered correctly, Can anyone please tell me what's wrong with my code?

Code: Select all

``Removed After AC.``
I know I have coded this so messy, sorry, but I have some problems with function calls, because Im new in C++ and confused with Pointers, MD-Arrays , etc.

Last edited by Arashk_kh68 on Tue Sep 11, 2007 6:45 pm, edited 1 time in total.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
There must be an empty line between field outputs
You are printing a blank line after every case. You should get PE, but I am not sure about the new judge. So, try changing accoring to this. Hope it helps.
Ami ekhono shopno dekhi...
HomePage

Arashk_kh68
New poster
Posts: 6
Joined: Sun Sep 09, 2007 3:56 pm

### Still WA

Hi , thanks for the hint, but did u mean that i change "endl" with "\n"?!
cause i did this, and still WA.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
No no. There should be a blank line between cases. But you are printing an extra blank line (the last one). So, endl or '\n' is not the problem. Try to find a way such that your code prints a blank line between cases. Hope it helps.
Ami ekhono shopno dekhi...
HomePage

Arashk_kh68
New poster
Posts: 6
Joined: Sun Sep 09, 2007 3:56 pm
Thanks a lot Jan, AC after removing last blank line.

Dao007forever
New poster
Posts: 2
Joined: Sun Sep 09, 2007 8:45 pm
Contact:
Sorry, but I still stuck??

Code: Select all

``````#include <cstdio>

#define MAX 110
#define FIN "Mine.in"
#define FOU "Mine.ou"
#define DEBUG 0

int a[MAX][MAX],b[MAX][MAX];
int m,n;

int main()
{
if (DEBUG)
{
freopen(FIN,"r",stdin);
freopen(FOU,"w",stdout);
}
int count=0;
char c;
while (1)
{
scanf("%d%d",&m,&n);
if (!m&&!n) break;

if (count) printf("\n\n");

count++;

for (int i=0;i<=m+1;i++)
for (int j=0;j<=n+1;j++)
{
a[i][j]=0;
b[i][j]=0;
}

for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++)
{
do
scanf("%c",&c);
while ((c!='*')&&(c!='.'));
a[i][j] = (c=='*');
}

}
for (int i=1;i<=m;i++)
for (int j=1;j<=n;j++)
if (!a[i][j]) b[i][j] = a[i-1][j-1] + a[i-1][j] + a[i-1][j+1] + a[i][j-1]   +           + a[i][j+1] + a[i+1][j-1] + a[i+1][j] + a[i+1][j+1];
printf("Field #%d:\n",count);
for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++)
if (!a[i][j]) printf("%d",b[i][j]);
else printf("*");
if (i!=m) printf("\n");
}
}

if (DEBUG)
{
fclose(stdin);
fclose(stdout);
}
return 0;
}
``````

lnr
Experienced poster
Posts: 142
Joined: Sat Jun 30, 2007 2:52 pm

Code: Select all

``````Removed.
Thanks Jan.
``````
Last edited by lnr on Fri Oct 12, 2007 3:41 pm, edited 1 time in total.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
You should a blank line between two cases, not after every case.
Ami ekhono shopno dekhi...
HomePage

GeorgeDePaulo
New poster
Posts: 1
Joined: Fri Oct 19, 2007 8:12 pm
Location: Brazil

### I hav a problem like this in 10189

HI, i am tring to solve the problem 10189 but get only WA. My output is the same Jan has benn posted but l i got WA, any one, please help me to find the bug. This is my code:

Code: Select all

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

int main(){
int mineField;
int m, n, i, j, size, cont, campo=1, flag=1;
char field;

while ( scanf("%d %d", &m, &n) == 2 && m != 0 && n !=0){
size = m*n;
for ( i=0; i < m; i++){
for(j=0; j < n; j++ )mineField[i][j]=0;
}
cont=0;
i=0;
j=0;
while ( i < m  && j < n ){
scanf("%c",&field);
if ( field=='*' || field=='.'){
j = cont%n;
if ( field == '*' ){
mineField[i][j]=-10;
if ( i != 0){
mineField[i-1][j]++;
if( j != 0 )mineField[i-1][j-1]++;
if( j != n-1)mineField[i-1][j+1]++;
}
if ( i != m-1){
mineField[i+1][j]++;
if(j != 0)mineField[i+1][j-1]++;
if(j != n-1)mineField[i+1][j+1]++;
}
if ( j != 0 )mineField[i][j-1]++;
if ( j != n-1)mineField[i][j+1]++;
}
if ( j == n-1) i++;
cont++;

}
}
if(flag != 1){ printf("\n\n");}else flag=0;
printf("Field #%d:\n",campo);
for (i=0;i<m;i++){
for(j=0;j<n;j++){
if( mineField[i][j] < 0 )printf("*");else
printf("%d",mineField[i][j]);
}
if(i != m-1) printf("\n");
}
campo++;
}
return 0;
}
``````

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
Your code looks correct to me. Try updating the line..

Code: Select all

``scanf(" %c",&field); // note that there is a space before %c``
Hope it helps.
Ami ekhono shopno dekhi...
HomePage

hridoy
New poster
Posts: 21
Joined: Tue May 08, 2007 10:30 am
Location: Dhaka
Contact:

### acm-10189

CAN any one please tell me why I m getting CE??

Code: Select all

``````#include<iostream>
using namespace std;

int check(int i, int j, int x, int y, char b[]);

void main(void)
{
int i,j,k=0,m,n,z;
char a;
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[])
{
int k=i-1,l=i+1,m=j-1,n=j+1,p=0,q,w;
char a;

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);
}``````

rio
A great helper
Posts: 385
Joined: Thu Sep 21, 2006 5:01 pm
Location: Kyoto, Japan
Trying to compile with g++ 4.1 compiler had below error.
hr.cpp:6: error: ::main must return int
-----
RIo

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

### HELP!!

Can somebody help me with my code? I've got a RTE and just can't fix it!!
Here's my code:

Code: Select all

``````Code removed after AC
``````
Last edited by jackpigman on Sat Jan 05, 2008 8:42 am, edited 2 times in total.

Samiul
New poster
Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm
Your array indexing is not ok. In if(e == '*') condition, you are using three if's, among which one should be accesed. Now guess when a = 100 and b = 100, a * b - 1 = 9999. Now in the three if's you used some array indexing like in the first if c[d + b], in the second if also c[d + b] and in the third if c[d + 1]. Now do you see that when d = 9999, you are going out of boundary of c(0 - 9999). This is giving you RTE.

And another thing, every first time for an input set, you would try to read e, you would read the 10 after a, b. So you better use the line scanf("%d %d\n" , &a , &b).

I don't know what you think but I think using a 2 dimwnsional array in such problems would be easier.