10189 - Minesweeper

All about problems in Volume CI. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

HELP!!

Postby jackpigman » Fri Jan 04, 2008 6:02 pm

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

Postby Samiul » Fri Jan 04, 2008 11:48 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

Postby jackpigman » Sat Jan 05, 2008 8:39 am

Thanks a lot Samiul :D !! 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

Postby ChainRule » Tue Jan 08, 2008 5:44 pm

I've tried all the data but i still got WA :cry:
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

Postby Samiul » Tue Jan 08, 2008 6:09 pm

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

Postby ChainRule » Wed Jan 09, 2008 3:28 pm

Well,
I've changed the code,
but I still get WA :cry:
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

Postby Samiul » Wed Jan 09, 2008 4:36 pm

Increase the size of Temp
Samiul
New poster
 
Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm

Thx

Postby ChainRule » Wed Jan 09, 2008 5:00 pm

I go AC
Thx 8)
ChainRule
New poster
 
Posts: 4
Joined: Sun Nov 18, 2007 9:07 am

424(WA)

Postby hridoy » Wed Jan 09, 2008 9:41 pm

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

Postby karthikeyan1171 » Mon Mar 03, 2008 7:19 pm

Hello,
I am getting Wrong Answer for this code. Don't know what is the problem.Please help.
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!!!!!!

Postby Mohamed Abd El-Monem » Sat Apr 12, 2008 4:00 am

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


thanx in advance
Mohamed Abd El-Monem
New poster
 
Posts: 15
Joined: Mon Mar 31, 2008 1:20 am
Location: Egypt

Re: 10189 - Minesweeper

Postby Jan » Sun Apr 13, 2008 12:06 am

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
Location: Dhaka, Bangladesh

Re: 10189 - Minesweeper

Postby Mohamed Abd El-Monem » Fri Apr 18, 2008 4:15 am

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 :D
I waiot your advice in the other thread thanx very much
Mohamed Abd El-Monem
New poster
 
Posts: 15
Joined: Mon Mar 31, 2008 1:20 am
Location: Egypt

Re: 10189 - Minesweeper

Postby Mohiuddin » Fri Apr 25, 2008 12:18 pm

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

Postby Jan » Fri Apr 25, 2008 4:04 pm

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
Location: Dhaka, Bangladesh

PreviousNext

Return to Volume CI

Who is online

Users browsing this forum: No registered users and 1 guest