10189 - Minesweeper

Moderator: Board moderators

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.
jackpigman
New poster

Posts: 8
Joined: Fri Jan 04, 2008 5:57 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.
Samiul
New poster

Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm

Thanks a lot Samiul !! Got an AC after changing the code.
jackpigman
New poster

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

I got WA,plz help

I've tried all the data but i still got WA
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.
ChainRule
New poster

Posts: 4
Joined: Sun Nov 18, 2007 9:07 am

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.
Samiul
New poster

Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm

Still WA

Well,
I've changed the code,
but I still get WA
Code: Select all
`remove after AC`
Last edited by ChainRule on Wed Jan 09, 2008 5:12 pm, edited 1 time in total.
ChainRule
New poster

Posts: 4
Joined: Sun Nov 18, 2007 9:07 am

Increase the size of Temp
Samiul
New poster

Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm

Thx

I go AC
Thx
ChainRule
New poster

Posts: 4
Joined: Sun Nov 18, 2007 9:07 am

424(WA)

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

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

Gtting WA for my Code

Hello,
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[i] = a + m *i;

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

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

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

if(j>=1)
{
if(mw[i][j-1] !='*')
mw[i][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[i][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;
}
karthikeyan1171
New poster

Posts: 3
Joined: Fri Feb 29, 2008 11:39 am

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

i get WA and i didn't know my error
help me pleaaas
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;}`

Mohamed Abd El-Monem
New poster

Posts: 15
Joined: Mon Mar 31, 2008 1:20 am
Location: Egypt

Re: 10189 - Minesweeper

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

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

Re: 10189 - Minesweeper

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
Mohamed Abd El-Monem
New poster

Posts: 15
Joined: Mon Mar 31, 2008 1:20 am
Location: Egypt

Re: 10189 - Minesweeper

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;}`
Mohiuddin
New poster

Posts: 6
Joined: Fri Apr 25, 2008 12:09 pm

Re: 10189 - Minesweeper

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

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