10189 - Minesweeper

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

Moderator: Board moderators

Ardeshir81
New poster
Posts: 8
Joined: Wed Jun 12, 2013 5:51 pm

Re: 10189 - Minesweeper

Post by Ardeshir81 »

brianfry713 wrote:
Ardeshir81 wrote:Thanks brian, you always answer me.

I tried putting a "cout << endl" at the end, but it didn't help
There must be an empty line between field outputs. Don't print an extra blank line at the end of the output.
Sorry I'm asking again, but I don't know where exactly that empty line should be.
May you please tell it more precisely.
I checked it with some other output conditions, but I still get WA!
This is my code :

Code: Select all

//n and m are more than 0 and less than 101 .

#include <iostream>

using std :: cout ;
using std :: cin ;
using std :: endl ;

int main ()
{
    int n , m , TC = 0 ; //n for rows and m for columns and TC for test case .
    cin >> n >> m ;
    while (n != 0 && m != 0)
    {
        TC ++ ;
        char board [n + 1] [m + 1] ; //ERROR
        cout << "Field #" << TC << ":\n" ;
        for (int i = 0 ; i < n ; i ++)
            cin >> board [i] ;
        for (int i = 0 ; i < n ; i ++)
            for (int j = 0 ; j < m ; j ++)
                if (board [i] [j] == '.')
                    board [i] [j] = '0' ;
        for (int i = 0 ; i < n ; i ++)
            for (int j = 0 ; j < m ; j ++)
                if (board [i] [j] == '*')
                {
                    if (i > 0 && j > 0 && board [i - 1] [j - 1] != '*')
                        board [i - 1] [j - 1] ++ ;
                    if (j > 0 && board [i] [j - 1] != '*')
                        board [i] [j - 1] ++ ;
                    if (i < n && j > 0 && board [i + 1] [j - 1] != '*')
                        board [i + 1] [j - 1] ++ ;
                    if (i > 0 && board [i - 1] [j] != '*')
                        board [i - 1] [j] ++ ;
                    if (i < n && board [i + 1] [j] != '*')
                        board [i + 1] [j] ++ ;
                    if (i > 0 && j < m && board [i - 1] [j + 1] != '*')
                        board [i - 1] [j + 1] ++ ;
                    if (j < m && board [i] [j + 1] != '*')
                        board [i] [j + 1] ++ ;
                    if (i < n && j < m && board [i + 1] [j + 1] != '*')
                        board [i + 1] [j + 1] ++ ;
                }
        for (int i = 0 ; i < n ; i ++)
        {
            for (int j = 0 ; j < m ; j ++)
                cout << board [i] [j] ;
            cout << endl ;
        }
        cin >> n >> m ;
    }
    return 0 ;
}
+EDIT :
My English is not very good, please if you want to explain, explain more and more precise.
Ardeshir81
New poster
Posts: 8
Joined: Wed Jun 12, 2013 5:51 pm

Re: 10189 - Minesweeper

Post by Ardeshir81 »

HI!
I still have problem.
I compared my program with two others (both of them are accepted) :
https://github.com/fjsj/programming-cha ... esweeper.c
http://p30up.ir/Bahar92/1371679179.txt

My output was exactly like them, with no difference, But I still get WA.
This is the last modified code :
http://up-comeng.ir/uploads/bb45Minesweeper.cpp
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

Ardeshir81 wrote: Sorry I'm asking again, but I don't know where exactly that empty line should be.
May you please tell it more precisely.
I checked it with some other output conditions, but I still get WA!
This is my code :

Code: Select all

//n and m are more than 0 and less than 101 .

#include <iostream>

using std :: cout ;
using std :: cin ;
using std :: endl ;

int main ()
{
    int n , m , TC = 0 ; //n for rows and m for columns and TC for test case .
    cin >> n >> m ;
    while (n != 0 && m != 0)
    {
        TC ++ ;
        char board [n + 1] [m + 1] ; //ERROR
        cout << "Field #" << TC << ":\n" ;
        for (int i = 0 ; i < n ; i ++)
            cin >> board [i] ;
        for (int i = 0 ; i < n ; i ++)
            for (int j = 0 ; j < m ; j ++)
                if (board [i] [j] == '.')
                    board [i] [j] = '0' ;
        for (int i = 0 ; i < n ; i ++)
            for (int j = 0 ; j < m ; j ++)
                if (board [i] [j] == '*')
                {
                    if (i > 0 && j > 0 && board [i - 1] [j - 1] != '*')
                        board [i - 1] [j - 1] ++ ;
                    if (j > 0 && board [i] [j - 1] != '*')
                        board [i] [j - 1] ++ ;
                    if (i < n && j > 0 && board [i + 1] [j - 1] != '*')
                        board [i + 1] [j - 1] ++ ;
                    if (i > 0 && board [i - 1] [j] != '*')
                        board [i - 1] [j] ++ ;
                    if (i < n && board [i + 1] [j] != '*')
                        board [i + 1] [j] ++ ;
                    if (i > 0 && j < m && board [i - 1] [j + 1] != '*')
                        board [i - 1] [j + 1] ++ ;
                    if (j < m && board [i] [j + 1] != '*')
                        board [i] [j + 1] ++ ;
                    if (i < n && j < m && board [i + 1] [j + 1] != '*')
                        board [i + 1] [j + 1] ++ ;
                }
        for (int i = 0 ; i < n ; i ++)
        {
            for (int j = 0 ; j < m ; j ++)
                cout << board [i] [j] ;
            cout << endl ;
        }
        cin >> n >> m ;
    }
    return 0 ;
}
Sample input:

Code: Select all

4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0
Correct output:

Code: Select all

Field #1:
*100
2210
1*10
1110

Field #2:
**100
33200
1*100
Your output:

Code: Select all

Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100
There must be an empty line between field outputs.

For the previous code you posted:
catalan wrote:I am getting WA too. I have checked all test cases I can think of. I also checked for bllank lines between test cases. I also removed a blank line at the end. But still getting WA. Please help. I have provided my code below. I appreciate your help and thanks in advance.

Code: Select all

#include <iostream>
#include <cstring>

using namespace std;

#define SIZE 			102
#define ADDMINE(field, i, j)	++field[i-1][j-1]; ++field[i-1][j]; ++field[i-1][j+1]; 	\
				++field[i][j-1]; ++field[i][j+1];			\
				++field[i+1][j-1]; ++field[i+1][j]; ++field[i+1][j+1]

int main() {
  short field[SIZE][SIZE];
  unsigned short n, m;
  char ch;
  unsigned long x = 0;

  cin >> n >> m;
  while (1) {
    
    if (n == 0 && m == 0) break;
    cout << "Field #" << ++x << ":" << endl;

    for (unsigned short i = 1; i <= n; ++i)
      for (unsigned short j = 1; j <= m; ++j) {
	cin >> ch;
	if (ch == '.') field[i][j] = 0;
	else field[i][j] = -100;
      }

    for (unsigned short i = 1; i <= n; ++i) {
      for (unsigned short j = 1; j <= m; ++j)
	if (field[i][j] < 0) {
          ADDMINE(field, i, j);
	}
    }

    for (unsigned short i = 1; i <= n; ++i) {
      for (unsigned short j = 1; j <= m; ++j) {
	if (field[i][j] < 0) cout << '*';
	else cout << field[i][j];
      }
      cout << endl;
    }
    cin >> n >> m;
    if (n != 0 && m != 0) cout << endl;
  }
  return 0;
}
Your output from the sample input is:

Code: Select all

Field #1:
*100
2210
1*10
1110

Field #2:
**100
33200
1*100

As you can see, there is an extra blank line at the end.
Check input and AC output for thousands of problems on uDebug!
Snakib
New poster
Posts: 7
Joined: Mon May 31, 2010 6:25 am

Re: 10189 - Minesweeper

Post by Snakib »

finally AC , thanks brian. :D
sm_programmer
New poster
Posts: 10
Joined: Mon Jun 24, 2013 7:39 am

Re: 10189 - Minesweeper

Post by sm_programmer »

Hello! :)

I just can't understand why I'm getting WA, even when I tried with every test I had available (and w/positive results). :(

Here's my code in C. I think I'm having problems because of the dynamic memory usage. :-?

Code: Select all

Removed after AC
Hope someone will find where I'm wrong :D . Thanks in advance.
Last edited by sm_programmer on Tue Jul 02, 2013 1:38 am, edited 3 times in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

You're missing the : at the end of the Field lines.
Check input and AC output for thousands of problems on uDebug!
sm_programmer
New poster
Posts: 10
Joined: Mon Jun 24, 2013 7:39 am

Re: 10189 - Minesweeper

Post by sm_programmer »

I've just put the ":" as you said, and all of a sudden, it worked! :o
What a mistake! :lol: I'd have never realized by myself.

Thank you very much! :D
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

Copy the sample input and output and then do a diff on your code and you'll catch mistakes like that on your own next time.
Check input and AC output for thousands of problems on uDebug!
sm_programmer
New poster
Posts: 10
Joined: Mon Jun 24, 2013 7:39 am

Re: 10189 - Minesweeper

Post by sm_programmer »

Thanks for the recommendation! :D
dreign
New poster
Posts: 3
Joined: Tue Jul 02, 2013 4:36 pm

Re: 10189 - Minesweeper

Post by dreign »

I don't know why I am getting WA .
My code works for all the test cases and my own test cases as well .
someone please help.

#include<iostream>
void update(int i,int j,int m, int n,char A[][100],int flag)
{
if(flag==1)
{
if((i-1)>=0)
{
if((j-1)>=0)
{
if(A[i-1][j-1]!='\0' && A[i-1][j-1]!='*')
{
A[i-1][j-1]=char(A[i-1][j-1]+1);
}
else if(A[i-1][j-1]=='\0')
{
A[i-1][j-1]='1';
}
else
{}
}
if(A[i-1][j]!='\0' && A[i-1][j]!='*')
{
A[i-1][j]=char(A[i-1][j]+1);
}
else if(A[i-1][j]=='\0')
{
A[i-1][j]='1';
}
if((j+1)<n)
{
if(A[i-1][j+1]!='\0' && A[i-1][j+1]!='*')
{
A[i-1][j+1]=char(A[i-1][j+1]+1);
}
else if(A[i-1][j+1]=='\0')
{
A[i-1][j+1]='1';
}
else{}
}
}
if((i+1)<m)
{
if((j-1)>=0)
{
if(A[i+1][j-1]!='\0' && A[i+1][j-1]!='*')
{
A[i+1][j-1]=char( A[i+1][j-1]+1);
}
else if(A[i+1][j-1]=='\0')
{
A[i+1][j-1]='1';
}
else{}
}
if(A[i+1][j]!='\0' && A[i+1][j]!='*')
{
A[i+1][j]=char( A[i+1][j]+1);
}
else if(A[i+1][j]=='\0')
{
A[i+1][j]='1';
}
else
{}
if((j+1)<n)
{
if(A[i+1][j+1]!='\0' && A[i+1][j+1]!='*')
{
A[i+1][j+1]=char(A[i+1][j+1]+1);
}
else if(A[i+1][j+1]=='\0')
{
A[i+1][j+1]='1';
}
else
{}
}
}
if((j-1)>=0)
{
if(A[j-1]!='\0' && A[j-1]!='*')
{
A[j-1]=char( A[j-1]+1);
}
else if(A[j-1]=='\0')
{
A[j-1]='1';
}
}
if((j+1)<n)
{
if(A[j+1]!='\0' && A[j+1]!='*')
{
A[j+1]=char( A[j+1]+1);
}
else if(A[i][j+1]=='\0')
{
A[i][j+1]='1';
}
}
A[i][j]='*';
}
else
{
if(A[i][j]=='\0')
{
A[i][j]='0';
}
}
}

int main()
{
int m,n,i,j;
char ch1;
int flag;
long counter=0;
while(std::cin>>m>>n)
{
char ch[100][100]={'\0'};
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
std::cin>>ch1;
flag= (ch1=='*') ? 1 : 0;
update(i,j,m,n,ch,flag);
}
}
if(m!=0 && n!=0)
{
counter++;
std::cout<<"Field #"<<counter<<":\n";
}
else
{
continue;
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
std::cout<<ch[i][j];
}
std::cout<<"\n";
}
std::cout<<"\n";
}
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

There must be an empty line between field outputs. Don't print an extra blank line at the end.
Check input and AC output for thousands of problems on uDebug!
dreign
New poster
Posts: 3
Joined: Tue Jul 02, 2013 4:36 pm

Re: 10189 - Minesweeper

Post by dreign »

thank you .
ssshubha
New poster
Posts: 2
Joined: Thu Jul 18, 2013 12:03 am

Re: 10189 - Minesweeper

Post by ssshubha »

i can not really find out the bug,please anyone help.here is my code:
#include<stdio.h>
int main(){
char array1[105][105];
int array2[105][105],i,j,m=0,k,l,g,h;
while(scanf("%d %d",&i,&j)==2){
if(i==0 && j==0)break;
if(m)printf("\n");
m+=1;

for(k=0;k<i;k++){
for(l=0;l<j;l++){
array2[k][l]=0;
}
}
for(k=0;k<i;k++){
for(l=0;l<j;l++){
scanf("%c",&array1[k][l]);
}
}
for(k=0;k<i;k++){
for(l=0;l<j;l++){
if(array1[k][l]=='*'){


for(g=k-1;g<=k+1;g++){
for(h=l-1;h<=l+1;h++){
if(g<0 || g>=i || h<0 || h>=j || (g==k && h==l))continue;
array2[g][h]+=1;
}
}
}
}
}
printf("Field #%d:\n",m);
for(k=0;k<i;k++){
for(l=0;l<j;l++){
if(array1[k][l]=='*')printf("%c",array1[k][l]);
else printf("%d",array2[k][l]);
}
printf("\n");
}
}
return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

Doesn't match the sample I/O. Change your input parsing to work with the newline characters in the input.
Check input and AC output for thousands of problems on uDebug!
ssshubha
New poster
Posts: 2
Joined: Thu Jul 18, 2013 12:03 am

Re: 10189 - Minesweeper

Post by ssshubha »

thanks sir,you are great,brianfry sir. my code has got accepted now.
Post Reply

Return to “Volume 101 (10100-10199)”