## 10189 - Minesweeper

Moderator: Board moderators

Test the cases.

Input:
Code: Select all
`5 5******...**.*.**...******4 4*.*..*****...*.*0 0`

Output:
Code: Select all
`Field #1:******646**4*4**646******Field #2:*4*34*****633*3*`

Hope these help.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm

hi jan,

i change my code and it shows the right answer of the inputs that you have given. but still wrong answer.
code removed after AC
Last edited by Fuad Hassan EWU on Wed Jul 18, 2007 5:55 pm, edited 1 time in total.
Eagle er moto daana meley urbo

New poster

Posts: 38
Joined: Tue Jul 17, 2007 3:21 pm
Location: East West University

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
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 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.
Arashk_kh68
New poster

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

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
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 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.
Arashk_kh68
New poster

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

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
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm

Thanks a lot Jan, AC after removing last blank line.
Arashk_kh68
New poster

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

Sorry, but I still stuck??
Code: Select all
`#include <cstdio>#define MAX 110#define FIN "Mine.in"#define FOU "Mine.ou"#define DEBUG 0int 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;}`
Dao007forever
New poster

Posts: 2
Joined: Sun Sep 09, 2007 8:45 pm

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

lnr
Experienced poster

Posts: 134
Joined: Sat Jun 30, 2007 2:52 pm

You should a blank line between two cases, not after every case.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm

### 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[105][105];   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;}`
GeorgeDePaulo
New poster

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

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
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm

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

Posts: 21
Joined: Tue May 08, 2007 10:30 am
Location: Dhaka

Trying to compile with g++ 4.1 compiler had below error.
hr.cpp:6: error: ::main must return int

-----
RIo

rio
A great helper

Posts: 385
Joined: Thu Sep 21, 2006 5:01 pm
Location: Kyoto, Japan

PreviousNext