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

GRR!

Postby asterisk » Sun Oct 10, 2004 2:16 am

GRR!!
input file has the input up there, in the page ^


look at this:
Code: Select all
------------------------------------------------------------------------------
$ gcc -o 99 code.c
$ ./99 < input
Field #1:
****11**3*
5**32356*3
**543****4
45***5*7**
**5*43*6*5
*33*212***

Field #2:
3*3
***
4*4
*3*

Field #3:
*1

Field #4:
0
0
0
1
*
*
1

Field #5:
013*20
13**41
1***4*
356*42
***6*3
******
****5*

Field #6:
*3**23*4**
*546*5*4*4
*4****334*
4*65434*5*
****11**4*

Field #7:
*4*****2
4**5*43*
**642222
4***22*2
*5*5*23*
*53*323*
**22*24*
*3112*3*

Field #8:
00

Field #9:
1101*22210
*43333**32
****5*44**
**5***22*3

Field #10:
0122
12**
1*5*
223*
*333
*3**

Field #11:
**
**
4*
*4
**

Field #12:
110001*2
*311023*
*3*333*2
233***31
*23***52
*44*6***
*3**33*3

Field #13:
01*102*
134312*
2***211
*56*532
**4****
*55*45*
2**322*
123*111

Field #14:
****31
*7**5*
**6*6*
*7**5*
****41
****30
**7*31
3**55*
13****

Field #15:
*2**

Field #16:
01*11*211
1232335*3
1*3*3****
335*546*4
**3**3**3
45545*65*
****3***2

Field #17:
***
5*4
**2
*41
*52
***
*74
***
**3

Field #18:
**432
4****
*7***
***63
25**2
02**3
0135*
001**

Field #19:
01****4***
0124***4**

Field #20:
*
*
2
*
1

$


-------------------------------


LOOK AT THIS!
it gives me a WA!!!
and the output is right! any ideas?

ill give you the code:

[c]
#include <stdio.h>

int main(void) {
char field[100][100],ch;
int a,b;
int i,j;
int m,n; /* m - linha; n - colunas */
int num=0;

while(scanf("%d %d",&m,&n) == 2 && (m != 0) || (n != 0)) {
++num;
for (i=0;i<m;i++)
for(j=0;j<=n;j++)
field[i][j] = '0';



for(i=0;i<m;i++) {
for(j=0;j<=n;j++) {
if ((ch = getchar()) == '.')
continue;
else if (ch == '*') {
field[i][j] = ch;
a = i-1;
while (a<0)
++a;
for(;a<=i+1;a++) {
b = j-1;
while (b<0)
++b;
for(;b<=j+1;b++) {
if (field[a][b] != '*')
(int) ++field[a][b];
}
}
}
else if (ch == '\n')
field[i][j] = ch;
}
}
printf("Field #%d:\n",num);
for (i=0;i<m;i++) {
for(j=1;j<=n;j++)
printf("%c",field[i][j]);

printf("\n");
}
printf("\n");

}
}[/c][/code]
asterisk
New poster
 
Posts: 1
Joined: Sun Oct 10, 2004 1:59 am

Postby Ghust_omega » Sun Oct 10, 2004 6:19 am

Hi !! asterisk I just change the indes of array
fo 100 to 105 and gives AC (P.E.) you have
to see how to avoid that and remove your code
Hope it Helps
Keep posting
User avatar
Ghust_omega
Experienced poster
 
Posts: 115
Joined: Tue Apr 06, 2004 7:04 pm
Location: Venezuela

Re: 10189 WA - Banging my head against the desk.

Postby jambon_vn » Wed Oct 13, 2004 8:16 am

This problem is not hard but anyone know the way to speed up the program. The fastest record is 0.000s. How did he do? :(
jambon_vn
New poster
 
Posts: 15
Joined: Wed Sep 29, 2004 6:03 am

10189...With WA...

Postby Wei » Sat Oct 23, 2004 5:17 am

I don't know where is wrong...
I tried to do with some datas~~~but the judge still gave WA~
Could somebody help me with this???

[c]#include <stdio.h>
#include <stdlib.h>

int main(void)
{
unsigned long n[105][105]={0},i,j,a,b,co,lead=1;
char ch[0];
scanf("%lu %lu",&a,&b);
while(a!=0&&b!=0)
{
int n[105][105]={0};
co=0;
scanf("%c",&ch);
for(i=1;i<=a;i++)
{
for(j=1;j<=b;j++)
{
scanf("%c",&ch);
if(ch[0]==42)
n[i][j]=1;
if(ch[0]==46)
n[i][j]=0;
}
scanf("%c",&ch);
}
printf("Field #%lu:\n",lead);
for(i=1;i<=a;i++)
{
for(j=1;j<=b;j++)
{
co=0;
if(n[i][j]==1)
{printf("*");}
else
{
if(n[i-1][j-1]==1)
co++;
if(n[i][j-1]==1)
co++;
if(n[i+1][j-1]==1)
co++;
if(n[i-1][j]==1)
co++;
if(n[i+1][j]==1)
co++;
if(n[i-1][j+1]==1)
co++;
if(n[i][j+1]==1)
co++;
if(n[i+1][j+1]==1)
co++;
printf("%lu",co);
}
}
printf("\n");
}
printf("\n");
scanf("%lu %lu",&a,&b);
lead++;
}
return 0;
}
[/c]
Wei
New poster
 
Posts: 23
Joined: Sat Jul 24, 2004 5:37 pm

Postby Mohammad Mahmudur Rahman » Sat Oct 23, 2004 8:05 pm

Hi,
I just changed the following line from your code
Code: Select all
 char ch[0];

to
Code: Select all
 char ch;

as I coudln't compile it (Which compiler did you use?) & then got Accepted (PE). Make sure.
You should never take more than you give in the circle of life.
Mohammad Mahmudur Rahman
Experienced poster
 
Posts: 154
Joined: Sat Apr 17, 2004 9:34 am
Location: EEE, BUET

Postby Wei » Sun Oct 24, 2004 4:45 am

Well~~Thx for the advice...
Now~~I got an AC(P.E.)...and I know where is wrong~~~
I use the Dev.C++...
Wei
New poster
 
Posts: 23
Joined: Sat Jul 24, 2004 5:37 pm

Postby smilejfu » Mon Oct 25, 2004 6:05 pm

I've tried many datas
And all got correct.
Or is my way wrong?
Here is my code: (say thanks to you first :D )
Code: Select all

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int m,n;
    char **map,ch;
    int i,j,total,count=1,a,b;

    while(scanf("%d %d",&n,&m))
     {
      if(m==0&&n==0) break;
      map=(char **)malloc((n+2)*sizeof(char *));
      for(i=0;i<(n+2);i++)
        map[i]=(char *)malloc((m+2)*sizeof(char));

      for(i=1;i<=n;i++)
         for(j=1;j<=m;j++)
           scanf(" %c", &map[i][j]);

      printf("Field #%d:\n",count++);
      for(i=1;i<=n;i++)
        {
         for(j=1;j<=m;j++)
          {
           if(map[i][j]=='*')  printf("*");
           else
             {
              total=0;
              if(map[i-1][j-1]=='*')  total++;
              if(map[i-1][j  ]=='*')  total++;
              if(map[i-1][j+1]=='*')  total++;
              if(map[i  ][j-1]=='*')  total++;
              if(map[i  ][j+1]=='*')  total++;
              if(map[i+1][j-1]=='*')  total++;
              if(map[i+1][j  ]=='*')  total++;
              if(map[i+1][j+1]=='*')  total++;
              printf("%d",total);
             }
          }
         printf("\n");
       }
      printf("\n");

      for(i=0;i<(n+2);i++)
        free(map[i]);
      free(map);
     }
    return 0;
}
 
smilejfu
New poster
 
Posts: 5
Joined: Fri Oct 08, 2004 2:29 pm

10189 - Minesweeper

Postby Fuad Hassan_IIUC(DC) » Sun Jul 10, 2005 5:26 pm

i just can't believe that i am getting WA!!!! i know i am making simple mistakes but can't find out where the mistake is :evil:
#include<stdio.h>
#include<iostream.h>
int main()
{
char str[50][50];
int i,j,count,a,b,field=1;
while(cin>>a>>b)
{
if(a==0&&b==0)
break;
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
cin>>str[i][j];

}
}
printf("Field #%d:\n",field);
for(i=0;i<a;i++)
{

for(j=0;j<b;j++)
{
count=0;
if(str[i][j]=='*')
cout<<"*";
else
{

if(str[i][j-1]=='*')
count++;
if(str[i][j+1]=='*')
count++;
if(str[i-1][j]=='*')
count++;
if(str[i+1][j]=='*')
count++;
if(str[i-1][j-1]=='*')
count++;
if(str[i+1][j-1]=='*')
count++;
if(str[i-1][j+1]=='*')
count++;
if(str[i+1][j+1]=='*')
count++;
printf("%d",count);
}
}
printf("\n");
}
field++;
}
return 0;
} :evil:
fuad
Fuad Hassan_IIUC(DC)
New poster
 
Posts: 18
Joined: Fri Jan 07, 2005 9:35 pm
Location: Bangladesh

Postby Fuad Hassan_IIUC(DC) » Sun Jul 10, 2005 5:30 pm

actually when i submitted this problem i made the size of str[105][105]
my fault is in another place
fuad
Fuad Hassan_IIUC(DC)
New poster
 
Posts: 18
Joined: Fri Jan 07, 2005 9:35 pm
Location: Bangladesh

indexing off the edges

Postby jdmetz » Tue Jul 12, 2005 8:07 pm

Why don't you worry about the boundaries? Won't your code fail on the second of these two cases:

Code: Select all
4 4
****
****
****
****

3 3
...
...
...
jdmetz
New poster
 
Posts: 25
Joined: Fri May 27, 2005 5:23 pm
Location: Ann Arbor, MI USA

Postby Fuad Hassan_IIUC(DC) » Wed Jul 13, 2005 12:09 pm

i have changed the code a bit but still getting WA. here is mu chaged code.

#include<stdio.h>
#include<iostream.h>
int main()
{
char str[105][105];
int i,j,count,a,b,field=1;
while(cin>>a>>b)
{
if(a==0&&b==0)
break;
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
cin>>str[i][j];

}
}
printf("Field #%d:\n",field);
for(i=0;i<a;i++)
{

for(j=0;j<b;j++)
{
count=0;
if(str[i][j]=='*')
cout<<"*";
else
{

if(str[i-1][j-1]=='*')
count++;
if(str[i-1][j]=='*')
count++;
if(str[i-1][j+1]=='*')
count++;
if(str[i][j-1]=='*')
count++;
if(str[i][j+1]=='*')
count++;
if(str[i+1][j-1]=='*')
count++;
if(str[i+1][j]=='*')
count++;
if(str[i+1][j+1]=='*')
count++;
printf("%d",count);
}
}
printf("\n");
}
field++;
cout<<endl;
}
return 0;
}
fuad
Fuad Hassan_IIUC(DC)
New poster
 
Posts: 18
Joined: Fri Jan 07, 2005 9:35 pm
Location: Bangladesh

still not correct..

Postby sohel » Wed Jul 13, 2005 1:13 pm

you are still not checking the boundary case..

when using i+1 or i - 1 or j+1 or j-1, make sure that coordiante lies within the grid, otherwise you get to consider points outside the grid and that may lead to error.
User avatar
sohel
Guru
 
Posts: 862
Joined: Thu Jan 30, 2003 5:50 am
Location: University of Texas at San Antonio

10189 (Minesweeper) - PE, help plz..........

Postby Roby » Fri Sep 09, 2005 5:59 am

Please help me to solve this annoying message (-->PE), why can't I perfectly solve this problem?

Below is my code:
Code: Select all
#include <stdio.h>
#include <string.h>
#define MAX 105
#define SIZE 100

char map[SIZE][MAX];
int flag[SIZE][MAX];

void initMap( void )
{
 int i = 0;

 for ( i = 0; i < SIZE; i++ )
    memset( map[i], '\0', sizeof( map[i] ) );
}

void initFlag( void )
{
 int i = 0, j = 0;

 for ( i = 0; i < SIZE; i++ )
    for ( j = 0; j < MAX; j++ )
       flag[i][j] = 0;
}

void countMine( const int m, const int n )
{
 int i = 0, j = 0;

 for ( i = 0; i < m; i++ )
    for ( j = 0; j < n; j++ )
       if ( map[i][j] == '*' )
       {
          flag[i][j] = 100;

          if ( map[i - 1][j] == '.' && i != 0 ) // Up
             flag[i - 1][j]++;

          if ( map[i - 1][j + 1] == '.' && i != 0 && j != n ) // Up-right
             flag[i - 1][j + 1]++;

          if ( map[i][j + 1] == '.' && j != n ) // Right
             flag[i][j + 1]++;

          if ( map[i + 1][j + 1] == '.' && i != m && j != n ) // Down-right
             flag[i + 1][j + 1]++;

          if ( map[i + 1][j] == '.' && i != m ) // Down
             flag[i + 1][j]++;

          if ( map[i + 1][j - 1] == '.' && i != m && j != 0 ) // Down-left
             flag[i + 1][j - 1]++;

          if ( map[i][j - 1] == '.' && j != 0 ) // Left
             flag[i][j - 1]++;

          if ( map[i - 1][j - 1] == '.' && i != 0 && j != 0 ) // Up-left
             flag[i - 1][j - 1]++;
       }
}

void printAnswer( const int m, const int n )
{
 int i = 0, j = 0;

 for ( i = 0; i < m; i++ )
 {
    for ( j = 0; j < n; j++ )
       if ( flag[i][j] == 100 )
          printf( "*" );
       else
          printf( "%d", flag[i][j] );

    printf( "\n" );
 }

 printf( "\n" );
}

int main()
{
 int m = 0, n = 0, test = 1;
 int i = 0;

 while ( scanf( "%d %d\n", &m, &n ) == 2 )
 {
    if ( m == 0 && n == 0 )
       break;

    initMap();
    initFlag();

    for ( i = 0; i < m; i++ )
       gets( map[i] );

    printf( "Field #%d:\n", test++ );

    countMine( m, n );
    printAnswer( m, n );
 }

 return 0;
}


Any idea................................................. :(

Before God, we are all equally wise and equally foolish
User avatar
Roby
Experienced poster
 
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia

Postby Rocky » Tue Sep 13, 2005 4:14 pm

Check out the problem statment

There must be an empty line between field outputs


So the empty line must print between two test case not after each test case your programm print empty line after each test case with the last
terminate case that is 0 0.
now i modiify your programme and got acc without pe. see it

Code: Select all
#include <stdio.h>
#include <string.h>
#define MAX 105
#define SIZE 100

char map[SIZE][MAX];
int flag[SIZE][MAX];

void initMap( void )
{
 int i = 0;

 for ( i = 0; i < SIZE; i++ )
    memset( map[i], '\0', sizeof( map[i] ) );
}

void initFlag( void )
{
 int i = 0, j = 0;

 for ( i = 0; i < SIZE; i++ )
    for ( j = 0; j < MAX; j++ )
       flag[i][j] = 0;
}

void countMine( const int m, const int n )
{
 int i = 0, j = 0;

 for ( i = 0; i < m; i++ )
    for ( j = 0; j < n; j++ )
       if ( map[i][j] == '*' )
       {
          flag[i][j] = 100;

          if ( map[i - 1][j] == '.' && i != 0 ) // Up
             flag[i - 1][j]++;

          if ( map[i - 1][j + 1] == '.' && i != 0 && j != n ) // Up-right
             flag[i - 1][j + 1]++;

          if ( map[i][j + 1] == '.' && j != n ) // Right
             flag[i][j + 1]++;

          if ( map[i + 1][j + 1] == '.' && i != m && j != n ) // Down-right
             flag[i + 1][j + 1]++;

          if ( map[i + 1][j] == '.' && i != m ) // Down
             flag[i + 1][j]++;

          if ( map[i + 1][j - 1] == '.' && i != m && j != 0 ) // Down-left
             flag[i + 1][j - 1]++;

          if ( map[i][j - 1] == '.' && j != 0 ) // Left
             flag[i][j - 1]++;

          if ( map[i - 1][j - 1] == '.' && i != 0 && j != 0 ) // Up-left
             flag[i - 1][j - 1]++;
       }
}

void printAnswer( const int m, const int n )
{
 int i = 0, j = 0;

 for ( i = 0; i < m; i++ )
 {
    for ( j = 0; j < n; j++ )
       if ( flag[i][j] == 100 )
          printf( "*" );
       else
          printf( "%d", flag[i][j] );

    printf( "\n" );
 }

}

int main()
{
 int m = 0, n = 0, test = 1;
 int i = 0,end=0;

 while ( scanf( "%d %d\n", &m, &n ))
 {
   if(m==0&&n==0)
      break;
   if(end)
      printf("\n");

    initMap();
    initFlag();

    for ( i = 0; i < m; i++ )
       gets( map[i] );

    printf( "Field #%d:\n", test++ );

    countMine( m, n );
    printAnswer( m, n );
   end = 1;
 }

 return 0;
}


GOOD LUCK
Rocky
User avatar
Rocky
Experienced poster
 
Posts: 124
Joined: Thu Oct 14, 2004 9:05 am

Postby trance8 » Wed Oct 05, 2005 12:26 pm

Code: Select all
#include <iostream.h>

int main(int argc, char* argv[])
{
 int n,m,p1,p2,licz;
 char *tab,*tab2;

 while(1)
 {
  cin>>n>>m;
  if ((n==0) && (m==0)) break;
  if (licz) cout<<endl;
  tab = new char [n*m];
  tab2 = new char [n*m];
  for (p1=0;p1<n*m;++p1)
  {
   tab2[p1]='0';
  }
  for (p1=0;p1<n;++p1)
  {
   for (p2=0;p2<m;++p2)
   {
    cin>>tab[p1*m+p2];
   }
  } // for p1

  for (p1=0;p1<n;++p1)
  {
   for (p2=0;p2<m;++p2)
   {
    if (tab[p1*m+p2]=='*')
    {
     tab2[p1*m+p2]='*';

     if (p1>0)
     {
      if (p2>0)
      {
       if (tab2[(p1-1)*m+p2-1]!='*') tab2[(p1-1)*m+p2-1]++;
      }
      if (p2<m-1)
      {
       if (tab2[(p1-1)*m+p2+1]!='*') tab2[(p1-1)*m+p2+1]++;
      }

      if (tab2[(p1-1)*m+p2]!='*') tab2[(p1-1)*m+p2]++;
     } // if (p1>0)

     if (p1<n-1)
     {
      if (p2>0)
      {
       if (tab2[(p1+1)*m+p2-1]!='*') tab2[(p1+1)*m+p2-1]++;
      }
      if (p2<m-1)
      {
       if (tab2[(p1+1)*m+p2+1]!='*') tab2[(p1+1)*m+p2+1]++;
      }

      if (tab2[(p1+1)*m+p2]!='*') tab2[(p1+1)*m+p2]++;
     }

     if (p2>0)
     {
      if (tab2[p1*m+p2-1]!='*') tab2[p1*m+p2-1]++;
     }
     if (p2<m-1)
     {
      if (tab2[p1*m+p2+1]!='*') tab2[p1*m+p2+1]++;
     }
     if (tab2[p1*m+p2]!='*') tab2[p1*m+p2]++;

    }

   } // for p2
  } // for p1
  cout<<"Field #"<<++licz<<":"<<endl;
  for (p1=0;p1<n;++p1)
  {
   for (p2=0;p2<m;++p2)
   {
    cout<<tab2[p1*m+p2];;
   }
   cout<<endl;
  }

  delete [] tab;
  delete [] tab2;
 }
 return 0;
}



Why WA??

edit: licz=0 -> I forgot :) Now is OK.
trance8
New poster
 
Posts: 2
Joined: Mon Jan 03, 2005 8:04 pm

PreviousNext

Return to Volume CI

Who is online

Users browsing this forum: No registered users and 0 guests