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

10189 - Minesweeper

Postby alirezanoori » Fri Jan 02, 2009 1:24 pm

Hey all,
I tested all of test data with my program and every one of them is correct!!! But I still get WA! I double checked each line of my code and found nothing wrong about it! Could you please take a look at it and find my bug please?
Thanks in advance. I appreciate your help.

Code: Select all
//Removed After AC
alirezanoori
New poster
 
Posts: 26
Joined: Fri Jan 02, 2009 12:41 am

10189 - Minesweeper

Postby aliahmed » Sat Jan 17, 2009 10:20 pm

W A. Someone please help me.

#include<stdio.h>

int main()
{
int n,m,i,j,count,f=0;
char sq[200][200];
count=1;
while(scanf("%d%d",&n,&m),n,m)
{
if(f==1)
printf("\n");
f=1;
for(i=0; i<n; i++)
{
for(j=0; j<=m; j++)
{
scanf("%c",&sq[i][j]);
if(sq[i][j]=='.')
sq[i][j]='0';
}
}
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(sq[i][j]=='*')
{
if(sq[i+1][j]>=48 && sq[i+1][j]<=57)
sq[i+1][j]++;
if(sq[i-1][j]>=48 && sq[i-1][j]<=57)
sq[i-1][j]++;
if(sq[i][j+1]>=48 && sq[i][j+1]<=57)
sq[i][j+1]++;
if(sq[i][j-1]>=48 && sq[i][j-1]<=57)
sq[i][j-1]++;
if(sq[i+1][j+1]>=48 && sq[i+1][j+1]<=57)
sq[i+1][j+1]++;
if(sq[i-1][j-1]>=48 && sq[i-1][j-1]<=57)
sq[i-1][j-1]++;
if(sq[i+1][j-1]>=48 && sq[i+1][j-1]<=57)
sq[i+1][j-1]++;
if(sq[i-1][j+1]>=48 && sq[i-1][j+1]<=57)
sq[i-1][j+1]++;
}
}
}
printf("Field #%d:",count++);
for(i=0; i<n; i++)
{
for(j=0; j<=m; j++)
{
printf("%c",sq[i][j]);
}
}
printf("\n");
}
return 0;
}
aliahmed
New poster
 
Posts: 24
Joined: Fri Oct 24, 2008 8:37 pm
Location: CUET, Chittagong, Bangladesh

Re: 10189 - Minesweeper

Postby amrupam » Tue Mar 17, 2009 7:41 pm

I dont know why it does not work
anyone's help will be appriciated

Code: Select all
#include <stdio.h>
int board[100][100];
int nrow, ncol;
void init_board(int nrow,int ncol){
     int j;
     for(int i=0;i<nrow;i++)
         for(j=0;j<ncol;j++)
              board[i][j]=0;                   
}

bool isValid(int i,int j)
{
     if(i<0 || i>(nrow-1) || j<0 || j>(ncol-1)) return false;
     return true;
}

void print_board(int count)
{
     int j;
     printf("Field #%d:\n",count);
      for(int i=0;i<nrow;i++){
            for(j=0;j<ncol;j++) 
            {
                  if(board[i][j]<0) printf("*");
                  else printf("%d",board[i][j]);               
            }
            if(i<(nrow-1))printf("\n");
      }
      //printf("\n");
}

int main() {
   
   char ch;
   int j,count=0;
    //freopen ( "10189.in" , "r" , stdin ) ;
    //freopen ( "10189.out" , "w" , stdout ) ;   
   while (1) {
        scanf("%d %d",&nrow,&ncol);
        init_board(nrow,ncol);
        if(nrow==0 && ncol==0) break;
        if(count!=0)printf("\n\n");       
        count++;
        for(int i=0;i<nrow;i++){
            for(j=0;j<ncol;j++)               
            {
                                                 
                  scanf("%c",&ch);
                 
                  while(ch==10) scanf("%c",&ch);
                  if(ch=='.') continue;
                  if(ch == '*'){ board[i][j]=-100;
                      if(isValid(i-1,j-1)) board[i-1][j-1]++;
                      if(isValid(i-1,j)) board[i-1][j]++;
                      if(isValid(i-1,j+1)) board[i-1][j+1]++;
                      if(isValid(i,j-1)) board[i][j-1]++;
                      if(isValid(i,j+1)) board[i][j+1]++;
                      if(isValid(i+1,j-1)) board[i+1][j-1]++;
                      if(isValid(i+1,j)) board[i+1][j]++;
                      if(isValid(i+1,j+1)) board[i+1][j+1]++;
                  }         
            }           
        }
        print_board(count);   
   }         
}     



My codes works perfectly with the following

iuput:

Code: Select all
6 10
****..**.*
.**.....*.
**...****.
..***.*.**
**.*..*.*.
*..*...***

4 3
.*.
***
.*.
*.*

1 2
*.

7 1
.
.
.
.
*
*
.

7 6
...*..
..**..
.***.*
...*..
***.*.
******
****.*

5 10
*.**..*.**
*...*.*.*.
*.****...*
.*.....*.*
****..**.*

8 8
*.*****.
.**.*..*
**......
.***..*.
*.*.*..*
*..*...*
**..*..*
*....*.*

1 2
..

4 10
....*.....
*.....**..
****.*..**
**.***..*.

6 4
....
..**
.*.*
...*
*...
*.**

5 2
**
**
.*
*.
**

7 8
......*.
*......*
*.*...*.
...***..
*..***..
*..*.***
*.**..*.

8 7
..*...*
......*
.***...
*..*...
**.****
*..*..*
.**...*
...*...

9 6
****..
*.**.*
**.*.*
*.**.*
****..
****..
**.*..
.**..*
..****

1 4
*.**

7 9
..*..*...
.......*.
.*.*.****
...*...*.
**.**.**.
.....*..*
****.***.

9 3
***
.*.
**.
*..
*..
***
*..
***
**.

8 5
**...
.****
*.***
***..
..**.
..**.
....*
...**

2 10
..****.***
....***.**

5 1
*
*
.
*
.
0 0


my output:
Code: Select all
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
amrupam
New poster
 
Posts: 2
Joined: Mon Mar 16, 2009 7:23 pm

Re: 10189 - Minesweeper

Postby poixp » Wed Mar 18, 2009 7:02 pm

You missed end line symbol at last line of output.
poixp
New poster
 
Posts: 20
Joined: Mon Apr 07, 2008 11:05 am

WA

Postby daichin85 » Wed Mar 25, 2009 10:10 pm

Hi all, i got WA, but i don't understand why. Please help. my code is.
Code: Select all
#include<stdio.h>

char output[100][100];

void solve(int n,int m, int i, int j){
   int p,q;
   int k,l;
   for(p = -1; p <= 1; p++)
      for(q = -1; q <= 1; q++)
         if(p != 0 || q != 0){
            k = p + i;
            l = q + j;
            if(k >= 0 && k <= n && l >= 0 && l <= m){
               if(output[k][l] == '.')
                  output[k][l] = 0;
               if(output[k][l] != '*')
                  output[k][l]++;
            }      
         }      
}

void print(int n,int m, int count){
   int i,j;
   printf("Field #%d:\n",count);
   for(i = 0; i < n; i++){
      for(j = 0; j < m; j++)
         if(output[i][j] == '*')
            printf("*");
         else
            printf("%d",output[i][j]);
         printf("\n");
   }
}

int main(){
   int n,m;
   int count = 0;
   char row[100];
   while(1){
      fgets(row, sizeof row, stdin);
      sscanf(row, "%d%d", &n, &m);
      if((n == 0)&&(m == 0))
         break;
      else{
         count++;
         int i,j;
         for(i = 0; i < n; i++)
            fgets(output[i], sizeof output, stdin);
         for(i = 0; i < n; i++)
            for(j = 0; j < m; j++){
               if(output[i][j] == '.')
                  output[i][j] = 0;
               if(output[i][j] == '*')
                  solve(n,m,i,j);
            }      
         print(n,m,count);      
      }   
   }
   return 0;
}
daichin85
New poster
 
Posts: 3
Joined: Tue Jul 01, 2008 1:29 pm

WA

Postby daichin85 » Wed Mar 25, 2009 10:15 pm

i got wrong answer. please help me. here is my code
Code: Select all

#include<stdio.h>

char output[100][100];

void solve(int n,int m, int i, int j){
   int p,q;
   int k,l;
   for(p = -1; p <= 1; p++)
      for(q = -1; q <= 1; q++)
         if(p != 0 || q != 0){
            k = p + i;
            l = q + j;
            if(k >= 0 && k <= n && l >= 0 && l <= m){
               if(output[k][l] == '.')
                  output[k][l] = 0;
               if(output[k][l] != '*')
                  output[k][l]++;
            }      
         }      
}

void print(int n,int m, int count){
   int i,j;
   printf("Field #%d:\n",count);
   for(i = 0; i < n; i++){
      for(j = 0; j < m; j++)
         if(output[i][j] == '*')
            printf("*");
         else
            printf("%d",output[i][j]);
         printf("\n");
   }
}

int main(){
   int n,m;
   int count = 0;
   char row[100];
   while(1){
      fgets(row, sizeof row, stdin);
      sscanf(row, "%d%d", &n, &m);
      if((n == 0)&&(m == 0))
         break;
      else{
         count++;
         int i,j;
         for(i = 0; i < n; i++)
            fgets(output[i], sizeof output, stdin);
         for(i = 0; i < n; i++)
            for(j = 0; j < m; j++){
               if(output[i][j] == '.')
                  output[i][j] = 0;
               if(output[i][j] == '*')
                  solve(n,m,i,j);
            }      
         print(n,m,count);      
      }   
   }
   return 0;
}

daichin85
New poster
 
Posts: 3
Joined: Tue Jul 01, 2008 1:29 pm

Re: 10189 - Minesweeper

Postby mohitkanwal » Sat Apr 04, 2009 9:32 am

Hi all I keep getting WA everytime I submit the problem again and again .. I searched the entire forum .. tested all the data that I could find in the forum and they are all correct .. I wanted to know whether it's okay to store the computed hint and display later coz .. I tried to switch to 1 entry and 1 display format ...still WA .. At last when everything has crashed I have posted my code here .. Plzz help ... I have patiently calculated grids upto 20 X 20 and still find WA...


Code: Select all
//removed after AC
Last edited by mohitkanwal on Tue Apr 07, 2009 8:53 pm, edited 1 time in total.
mohitkanwal
New poster
 
Posts: 7
Joined: Sun Mar 29, 2009 6:32 pm

Re: WA

Postby mohitkanwal » Sat Apr 04, 2009 9:35 am

daichin85 wrote:i got wrong answer. please help me. here is my code
Code: Select all

#include<stdio.h>

char output[100][100];

void solve(int n,int m, int i, int j){
   int p,q;
   int k,l;
   for(p = -1; p <= 1; p++)
      for(q = -1; q <= 1; q++)
         if(p != 0 || q != 0){
            k = p + i;
            l = q + j;
            if(k >= 0 && k <= n && l >= 0 && l <= m){
               if(output[k][l] == '.')
                  output[k][l] = 0;
               if(output[k][l] != '*')
                  output[k][l]++;
            }      
         }      
}

void print(int n,int m, int count){
   int i,j;
   printf("Field #%d:\n",count);
   for(i = 0; i < n; i++){
      for(j = 0; j < m; j++)
         if(output[i][j] == '*')
            printf("*");
         else
            printf("%d",output[i][j]);
         printf("\n");
   }
}

int main(){
   int n,m;
   int count = 0;
   char row[100];
   while(1){
      fgets(row, sizeof row, stdin);
      sscanf(row, "%d%d", &n, &m);
      if((n == 0)&&(m == 0))
         break;
      else{
         count++;
         int i,j;
         for(i = 0; i < n; i++)
            fgets(output[i], sizeof output, stdin);
         for(i = 0; i < n; i++)
            for(j = 0; j < m; j++){
               if(output[i][j] == '.')
                  output[i][j] = 0;
               if(output[i][j] == '*')
                  solve(n,m,i,j);
            }      
         print(n,m,count);      
      }   
   }
   return 0;
}



Try to use larger array It might help
mohitkanwal
New poster
 
Posts: 7
Joined: Sun Mar 29, 2009 6:32 pm

Need help with WA in 10189 - Minesweeper

Postby kunalghosh » Sun Apr 26, 2009 1:08 pm

Code: Select all
#include<iostream>
int const SIZE=110;
class matrix{
    char field[SIZE][SIZE];
    int row,col;
    matrix *link;

public:
    matrix(){
        row=col=0;
        link=NULL;
    }
    void set_row_col(int r,int c){
        row=r;col=c;link=NULL;
    }   
    matrix* ret_link(){
        return(link);
    }
    void process_field(){
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(field[i][j]=='*'){
                    for(int k=-1;k<=1;k++){
                        for(int l=-1;l<=1;l++){
                            if(is_point_in_field(i+k,j+l)){
                                if(field[i+k][j+l]=='*')
                                    continue;
                                else
                                    field[i+k][j+l]++;
                            }
                        }
                    }
                }
            }
        }
    }                       
    void get_field(){
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                std::cin>>field[i][j];
                if(field[i][j]=='.')
                    field[i][j]='0';
            }
        }
        process_field();
    }
    void disp_field(){
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                std::cout<<field[i][j];
            }
            std::cout<<std::endl;
        }
    }
   
    int is_point_in_field(int r,int c){
        return((r>=0 && r<row)&&(c>=0 && c<col));
    }

    void set_link(matrix *a){
        link=a;
    }
};

int main(int argc, char **argv){
    matrix *field1=NULL,*temp=NULL;
    int r,c;

    std::cin>>r>>c;
    while(r!=0 || c!=0 && ((r>=0 && r<=9)&&(c>=0 && c<=9))){
       
        matrix *a=new matrix;
        a->set_row_col(r,c);
       
        if(NULL==field1){
            temp=field1=a;
        }
        else{
            temp->set_link(a);
            temp=temp->ret_link();
        }
        a->get_field();
        std::cin>>r>>c;

        if(!((r>=0 && r<=9)&&(c>=0 && c<=9)))
            return(0);//return(1);
    }
    temp=field1;
    int i=1;

    while(NULL!=temp){
        std::cout<<"Field #"<<i++<<":"<<std::endl;
        temp->disp_field();
        temp=temp->ret_link();
        if(NULL!=temp)
            std::cout<<std::endl;
    }   
    return(0);
}


Guys i have been trying out the problem for some time now. Earlier i was getting Compile time error, for the return(1) i commented out later, now i am getting wrong answer ! i guess my answer conforms to the output required . i need help with where i might have got wrong.
regards
kunalghosh
New poster
 
Posts: 1
Joined: Sun Apr 26, 2009 12:02 pm

Re: 10189 - Minesweeper

Postby Flame » Thu Jul 02, 2009 10:49 pm

I got wrong answer. Please help me.
Code: Select all
/* 10189 - Minesweeper */
#include <stdio.h>

char Field[102][102];

void Check(int, int);
void Reset(void);

int main()
{
   
   int n, m, i, j, t;

   t = 0;   
   scanf("%d %d", &n, &m);
   while(n || m)
   {
      Reset();
      for(i = 1; i <= n; ++i)
         scanf("%s",&Field[i][1]);
      ++t;
      printf("\nField #%d:\n", t);
      for(i = 1; i <= n; ++i){
         for(j = 1; j <= m; ++j)
            Check(i, j);
         printf("\n");
      }
      printf("\n");
      scanf("%d %d", &n, &m);
   }
   
   return 0;
}

void Check(int n, int m)
{
   char ch = '0';
   if (Field[n][m] == '*')
      ch = '*';
   else {
      if (Field[n-1][m-1] == '*')
         ++ch;
      if (Field[n-1][m] == '*')
         ++ch;
      if (Field[n-1][m+1] == '*')
         ++ch;
      if (Field[n][m-1] == '*')
         ++ch;
      if (Field[n][m+1] == '*')
         ++ch;
      if (Field[n+1][m-1] == '*')
         ++ch;
      if (Field[n+1][m] == '*')
         ++ch;
      if (Field[n+1][m+1] == '*')
         ++ch;
   }

   printf("%c", ch);
}

void Reset(void)
{
   int i, j;
   for(i = 0; i <= 101; ++i)
      for(j = 0; j <= 101; ++j)
         Field[i][j] = ' ';
}
Flame
New poster
 
Posts: 1
Joined: Thu Jul 02, 2009 10:46 pm

Re: 10189 - Minesweeper

Postby aznin » Thu Jul 23, 2009 9:04 am

i got it wrong but it can compiled. what's wrong with the code?
Code: Select all

#include<iostream>
using namespace std;

int main (void){
    char table[100][100];
    int n, m;
    int value;
    int counter=0;
   
    do{
        cin>>n>>m;
        counter=counter+1;
        if(n==0 && m==0)
        break;
       
        for(int a=0; a<n; a++){
                for (int b=0; b<m; b++){
                    cin>>table[a][b];
                    }
        }
       
        for(int i=0; i<n; i++){
                for(int j=0; j<m; j++){
                        if(table[i][j] != '*'){
                                       value=0;
                                       for(int x=i-1; x<=i+1; x++){
                                               for(int y=j-1; y<=j+1; y++){
                                                   if(table[x][y]=='*')
                                                   value++;
                                                                           }
                                                                   }
                                               
                                               table[i][j]=value+'0';
                                               }
                                       }
                               }
                               
                               
        cout<<"Field #"<<counter<<":"<<endl;
        for(int i=0; i<n; i++){
                for(int j=0; j<m; j++){
                        cout<<table[i][j];
                                       }
                        cout<<endl;
                               }
        cout<<endl;
        }
       
        while(n!=0 && m!=0);
       

        return 0;
        }
                             
                                                                       
                                                                       
                                                             
aznin
New poster
 
Posts: 1
Joined: Tue Apr 21, 2009 12:34 pm

Re: 10189 - Minesweeper

Postby blittman » Mon Aug 31, 2009 1:17 am

If your test cases work but you're getting WA, make sure your output is in this format (put a blank line ONLY BETWEEN output fields). It got accepted for me in Java:

IRO:Minesweeper Brian$ java Main
4 4
*...
....
..*.
....
3 3
*..
.*.
...
0 0
Field #1:
*100
1211
01*1
0111

Field #2:
*21
2*1
111
IRO:Minesweeper Brian$
blittman
New poster
 
Posts: 2
Joined: Mon Aug 31, 2009 1:13 am

Re: 10189 - Minesweeper

Postby Taman » Fri Oct 30, 2009 12:40 am

I think new posters should read the earlier discussions first. If they fail to help you then you should ask for help. I have seen that questions in this forum are being repeated!!! But why will you waste your time waiting for another reply? Just check it out, may be you would be able to get the required information you are looking for on the earlier posts! :D
Taman
New poster
 
Posts: 32
Joined: Sun Oct 11, 2009 8:59 pm
Location: Southeast University

Re: 10189 - Minesweeper

Postby behnam.hamidi » Sat Feb 06, 2010 7:44 pm

hello
i get a WA again and again but idont lnow where my mistake is
please help me
plz mail me =behnam.hamidi1369@gmail.com
this is my code in c++
//soalo khoob bekhoon
#include <iostream>
using namespace std;
int main()
{
int m,n,i,j,counter=0,flag=0;
char a[1000][1000]={'0'};
char ch;
while(cin>>m>>n)
{
if(n<0||n>100||m<0||m>100)continue;
if(n==0&&m==0){flag=1;
return 1;}
counter++;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
// scanf("%c",&a[i][j]);
cin>>a[i][j];
}

}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!='*')
a[i][j]=48;
}
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]=='*')
{
if(a[i+1][j]!='*'&&i!=m-1)
a[i+1][j]++;
if(a[i-1][j]!='*'&&i!=0)
a[i-1][j]++;
if(a[i][j+1]!='*'&&j!=n-1)
a[i][j+1]++;
if(a[i][j-1]!='*'&&j!=0)
a[i][j-1]++;
if(a[i+1][j-1]!='*'&&j!=0&&i!=m-1)
a[i+1][j-1]++;
if(a[i+1][j+1]!='*'&&i!=m-1&&j!=n-1)
a[i+1][j+1]++;
if(a[i-1][j+1]!='*'&&i!=0&&j!=m-1)
a[i-1][j+1]++;
if(a[i-1][j-1]!='*'&&i!=0&&j!=0)
a[i-1][j-1]++;
}
}
}
cout<<"Field #"<<counter<<":"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
}
// if(i!=m-1)
// cout<<endl;
if(flag!=1)cout<<endl;
}
if(flag!=1)cout<<endl;
}

return 0;
}
help me plz i confused :oops: :oops:
behnam.hamidi
New poster
 
Posts: 2
Joined: Sat Feb 06, 2010 6:53 pm

Re: 10189 - Minesweeper

Postby The_Madman » Tue Mar 02, 2010 6:03 pm

To : behnam.hamidi

1. remove al && conditions from if statements
2. output a blank line after each field, currently your program output 2 blank lines at end of file.

hope this helps
The_Madman
New poster
 
Posts: 12
Joined: Fri May 23, 2008 10:24 pm

PreviousNext

Return to Volume CI

Who is online

Users browsing this forum: No registered users and 0 guests