Page 21 of 28

Re: 10189 - Minesweeper

Posted: Fri May 31, 2013 4:26 pm
by Snakib
What is the wrong with the code . can anybody tell me. it giving the right answer as far as i know

Code: Select all

#define i64 long long
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<cstdlib>

using namespace std;
int main()
{
    int n,m,i,j,k,l,count = 1;
    char a[104][104],b[104][104];

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

        for( i = 0 ; i < n ; i++ )
            for( j = 0 ; j < m ; j++ )
                b[i][j] = '0';

        getchar();

        for( i = 0 ; i < n ; i++ )
        {
            for( j = 0 ; j < m ; j++ )
            {
                a[i][j] = getchar();

                if( a[i][j] == '*' )
                {
                    b[i][j] = '*';

                    for( k = i-1 ; k <= i+1 ; k++ )
                        for( l = j-1 ; l <= j+1 ; l++ )
                        {
                            if( k < 0 || l < 0 || k >99 || l > 99 || a[k][l] == '*' )
                                continue;
                            else
                                b[k][l] += 1;
                        }
                }

            }

	    getchar();

        }

        if( count != 1 )
            printf("\n");
        printf("Field #%d:\n",count);
        for( i = 0 ; i < n ; i++ )
        {
            for( j = 0 ; j < m ; j++ )
                printf("%c",b[i][j]);

                printf("\n");
        }

        count++;
    }
}

Re: 10189 - Minesweeper

Posted: Tue Jun 11, 2013 4:01 am
by brianfry713
Try input:

Code: Select all

1 3
*.*
0 0

Re: 10189 - Minesweeper

Posted: Tue Jun 11, 2013 11:01 pm
by Snakib
otput

Code: Select all

Field #1:
*2*

Re: 10189 - Minesweeper

Posted: Wed Jun 12, 2013 12:18 am
by brianfry713
Try input:

Code: Select all

4 4
*...
....
.*..
....
3 5
**...
.....
.*...
1 3
*.*
0 0

Re: 10189 - Minesweeper

Posted: Wed Jun 12, 2013 1:17 pm
by Snakib
cursor shows like this

Code: Select all

4 4
*...
....
.*..
....
Field #1:
*100
2210
1*10
1110
3 5
**...
.....
.*...

Field #2:
**100
33200
1*100
1 3
*.*

Field #3:
*1*
0 0

Process returned 0 (0x0)   execution time : 8.471 s
Press any key to continue.

Re: 10189 - Minesweeper

Posted: Thu Jun 13, 2013 12:13 am
by brianfry713
Do you see the error in your code?

Re: 10189 - Minesweeper

Posted: Thu Jun 13, 2013 5:48 am
by ??????
Why i am getting Wrong Answer? Can anyone help? Here is my code

Code: Select all

#include <stdio.h>
#include <string.h>
int main()
{
char s[1000][1000],ch;
long long int n,m,i,j,x,y,i1,j1,t;
for(t=1;;)
{
scanf("%lld%lld",&n,&m);if(m==0&n==0) {break;}
if(n>0&&m>0&&n<=100&&m<=100)
{
for(i=0;i<n;i++)
{
for(j=0;j<m;)
{
scanf("%c",&ch);if(ch=='*'||ch=='.') { s[i][j]=ch; if(ch=='.') {s[i][j]='0';} j++; }
}
}
for(x=0;x<n;x++)
{
for(y=0;y<m;y++)
{
if(s[x][y]=='*')
{

if(x!=0 && y!=0 && x!=n-1&&y!=m-1)
{
    if(s[x-1][y-1]!='*') {s[x-1][y-1]=s[x-1][y-1]+1; }
    if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
    if(s[x-1][y+1]!='*') {s[x-1][y+1]=s[x-1][y+1]+1;}
    if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1;}
    if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
    if(s[x+1][y-1]!='*') {s[x+1][y-1]=s[x+1][y-1]+1;}
    if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1;}
    if(s[x+1][y+1]!='*') {s[x+1][y+1]=s[x+1][y+1]+1;}
}
else if(x==0 && y!=0 && y!=m-1)
{
    if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1; }
    if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
    if(s[x+1][y-1]!='*') {s[x+1][y-1]=s[x+1][y-1]+1;}
    if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1; }
    if(s[x+1][y+1]!='*') {s[x+1][y+1]=s[x+1][y+1]+1;}
}
else if(x==n-1 && y!=0 && y!=m-1)
{
    if(s[x-1][y-1]!='*') {s[x-1][y-1]=s[x-1][y-1]+1;}
    if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
    if(s[x-1][y+1]!='*') {s[x-1][y+1]=s[x-1][y+1]+1;}
    if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1;   }
    if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
}
else if(y==0 && x!=0&& x!=n-1)
{
    if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
    if(s[x-1][y+1]!='*') {s[x-1][y+1]=s[x-1][y+1]+1;}
    if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
    if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1;  }
    if(s[x+1][y+1]!='*') {s[x+1][y+1]=s[x+1][y+1]+1;}
}
else if(y==m-1 && x!=n-1&&x!=0)
{
    if(s[x-1][y-1]!='*') {s[x-1][y-1]=s[x-1][y-1]+1;}
    if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
    if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1;}
    if(s[x+1][y-1]!='*') {s[x+1][y-1]=s[x+1][y-1]+1;}
    if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1;}
}
else if(x==0&&y==0)
{
if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1;}
if(s[x+1][y+1]!='*') {s[x+1][y+1]=s[x+1][y+1]+1;}
}
else if(x==n-1&&y==0)
{
 if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
if(s[x-1][y+1]!='*') {s[x-1][y+1]=s[x-1][y+1]+1;}
if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
}
else if(x==0&&y==m-1)
{
    if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1;   }
    if(s[x+1][y-1]!='*') {s[x+1][y-1]=s[x+1][y-1]+1;}
    if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1;}
}
else if(x==n-1&&y==m-1)
{
if(s[x-1][y-1]!='*') {s[x-1][y-1]=s[x-1][y-1]+1;}
if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1;   }
}

}
}
}
printf("Field #%lld:\n",t);t++;
for(i1=0;i1<n;i1++)
{
for(j1=0;j1<m;j1++)
{
printf("%c",s[i1][j1]);
}
printf("\n");
}
printf("\n");
}
}
return 0;
}

Re: 10189 - Minesweeper

Posted: Thu Jun 13, 2013 11:27 pm
by brianfry713
There must be an empty line between field outputs, don't print an extra blank line at the end.

Re: 10189 - Minesweeper

Posted: Mon Jun 17, 2013 9:59 pm
by Ardeshir81
Hi friends!
I get WA too.
I tested the code with lots of test cases, I'm ALMOST sure there is nothing wrong with it.
I will appreciate any help and suggestion

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
        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] ++ ;
                }
        cout << "Field #" << TC << ":\n" ;
        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 ;
}
THNX

Re: 10189 - Minesweeper

Posted: Tue Jun 18, 2013 12:52 am
by brianfry713
There must be an empty line between field outputs.

Re: 10189 - Minesweeper

Posted: Tue Jun 18, 2013 1:26 am
by Ardeshir81
Thanks brian, you always answer me.

I tried putting a "cout << endl" at the end, but it didn't help, it's the modified 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
        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] ++ ;
                }
        cout << "Field #" << TC << ":\n" ;
        for (int i = 0 ; i < n ; i ++)
        {
            for (int j = 0 ; j < m ; j ++)
                cout << board [i] [j] ;
            cout << endl ;
        }
        cout << endl ;
        cin >> n >> m ;
    }
    return 0 ;
}

Re: 10189 - Minesweeper

Posted: Tue Jun 18, 2013 11:29 pm
by catalan
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

// Removed after AC

Re: 10189 - Minesweeper

Posted: Wed Jun 19, 2013 1:30 am
by brianfry713
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.

Re: 10189 - Minesweeper

Posted: Wed Jun 19, 2013 1:35 am
by brianfry713
catalan, that is AC code.

Re: 10189 - Minesweeper

Posted: Wed Jun 19, 2013 6:45 am
by catalan
I re submitted and got AC. Thank you Brian.