Page 23 of 28

Re: 10189 - Minesweeper

Posted: Sun Aug 04, 2013 6:03 pm
by KaziInzamamMahmud
I am getting wrong answer though the code is showing correct output for sample input......Is the extra new line at the end creating problem ? If i have to remove it then how ?
but the problem says : " There must be an empty line between field outputs. "
problem : http://uva.onlinejudge.org/index.php?op ... oblem=1130
my code : http://pastebin.com/d8inZWWJ#

Re: 10189 - Minesweeper

Posted: Tue Aug 06, 2013 3:26 am
by brianfry713
line 14 of your code won't compile.

Re: 10189 - Minesweeper

Posted: Tue Aug 06, 2013 9:19 am
by KaziInzamamMahmud
@ sir Brianfry713,,,
sir, the actual code that i compiled in my compiler was not that...there was an error (.) but it happened as i just copied the whole code at pastebin.com and then deleted my unnecessary comments there....my actual code is ( http://pastebin.com/ia6hZJbQ) .....which is working accurately for sample inputs !!!!

Re: 10189 - Minesweeper

Posted: Tue Aug 06, 2013 9:49 pm
by brianfry713
There must be an empty line between field outputs. Don't print an extra blank line at the end.

10189 - Minesweeper

Posted: Mon Aug 12, 2013 1:57 pm
by KaziInzamamMahmud
" There must be an empty line between field outputs. Don't print an extra blank line at the end. "
But how to do that...????? I am taking input from keyboard n when 0 0 is pressed,,the programme terminates.....how i can understand the number of input cases there ??

Re: 10189 - Minesweeper

Posted: Wed Aug 14, 2013 10:04 pm
by brianfry713
It's easier to put the input into a file and redirect it into your program. In UNIX I usually do something like:
./a.out < 10189.in | diff - 10189.out

To print a newline between test cases you could do something like:

Code: Select all

for(cn = 1; scanf("%d%d", &n, &m) == 2 && n; cn++) {
  if(cn > 1)
    puts("");
  // the rest of your code goes here
}

Re: 10189 - Minesweeper

Posted: Thu Aug 15, 2013 6:19 pm
by KaziInzamamMahmud
@brainfry713
////////****[It's easier to put the input into a file and redirect it into your program. In UNIX I usually do something like:
./a.out < 10189.in | diff - 10189.out
] ********/////// ** I didnt understand it ?
////****[To print a newline between test cases you could do something like:
for(cn = 1; scanf("%d%d", &n, &m) == 2 && n; cn++) { /////// why also n ????????/
if(cn > 1)
puts(""); ////////// shouldnt it be "\n"or will puts(""); work the same !!!
// the rest of your code goes here
}
]******////////

Re: 10189 - Minesweeper

Posted: Thu Aug 15, 2013 6:27 pm
by KaziInzamamMahmud
This time i am not printing an extra line at the end....Though i am getting wrong answer !!!!!!!
http://pastebin.com/u2ttD3w6

Re: 10189 - Minesweeper

Posted: Thu Aug 15, 2013 10:49 pm
by brianfry713
In UNIX '<' redirects a file to stdin, '|' redirects the output to the next command, '-' redirects the previous output to an argument.
In my for loop I check n to see if it's non-zero.
puts(""); is the same as printf("\n");

You code works on the sample input on my machine, but you're not checking array boundaries.
if i and j are zero you should not be reading a[j - 1]

Re: 10189 - Minesweeper

Posted: Fri Aug 16, 2013 1:56 pm
by KaziInzamamMahmud
I got accepted........Thanks BRIAN for helping me a lot.......
Threats :
1) we have to print newlines between consecutive outputs...not an extra newline at the end..
/*
1 1
.
Field #1:
0
1 1
// newline
.
Field #2:
0
0 0
// newline by default ( we must not print an extra new line here same as the newline before )
Process returned 0
*/

2) we may not declare the variables as long int....only "int" is enough
3) though we may get correct output for sample input......but to get accepted verdict, we have to change the conditions from if(a[i-1][j-1]=='*') b[j]++; TO >>>>>
if((i-1>=0&&j-1>=0)&&(a[i-1][j-1]=='*')) b[j]++;

Re: 10189 - Minesweeper

Posted: Fri Sep 13, 2013 10:29 pm
by JSGS
I cant' for the life of me, find what is wrong with my code below.

Any hints from experienced programmers out there?

Code: Select all

#include <iostream>

using namespace std;

int main()
{
	int m = 0, n = 0;
	int iter = 1;
	int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
	int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
	int ctr = 0;
	char buffer = ' ';
	char board[100][100];
	
	while(cin >> m >> n)
	{
		if(m == 0 && n == 0)
			break;

		if(iter > 1) 
			cout << endl << endl;

		cout << "Field #" << iter << ":" << endl;

		for(int i = 0; i < m; i++)
		{	
			for(int j = 0; j < n; j++)
			{
				cin >> buffer;
			
				board[i][j] = buffer;
			}
		}
		
		for(int i = 0; i < m; i++)
		{	
			for(int j = 0; j < n; j++)
			{
				if(board[i][j] != '*')
				{
					ctr = 0;
	
					for(int q = 0; q < 8; q++)	
						if((dx[q] + i >= 0 && dx[q] + i < m) && (dy[q] + j >= 0 && dy[q] + j < n))
							if(board[dx[q] + i][dy[q] + j] == '*')	
								ctr++;	
								
					cout << ctr;
				}
				else
					cout << "*";
			}
	
			if(i + 1 < m)
				cout << endl;
		}	
		
		iter++;
	}

	return 0;
}

Re: 10189 - Minesweeper

Posted: Tue Sep 17, 2013 12:20 am
by brianfry713
Print a newline at the end of the last line.

Re: 10189 - Minesweeper

Posted: Wed Nov 13, 2013 1:09 pm
by venkikumar.m
I used all line formats, but still getting WA ,,,,
here is my code..... help .....

Code: Select all

#include<iostream>
using namespace std;
int main()
{
    int n,m,i,j,x=0;
    cin>>n>>m;
    while(n&&m)
    {
        x++;
        char **a=new char*[n];
        for(i=0;i<n;i++)
            a[i]=new char[m];
        char **b=new char*[n];
        for(i=0;i<n;i++)
            b[i]=new char[m];
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                cin>>a[i][j];
                b[i][j]=a[i][j];
            }
/*        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                cout<<b[i][j]<<" ";
            cout<<"\n";
        }
*/
       for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(a[i][j]=='*')
                {
                    b[i][j]='*';
                    if((i-1>=0)&&(j-1>=0))
                    {
                        if(b[i-1][j-1]>='0')
                            b[i-1][j-1]+=1;
                        else if(b[i-1][j-1]=='.')
                            b[i-1][j-1]='1';
                    }
                    if((i-1>=0)&&(j>=0))
                    {
                        if(b[i-1][j]>='0')
                            b[i-1][j]+=1;
                        else if(b[i-1][j]=='.')
                           b[i-1][j]='1';
                    }
                    if((i-1>=0)&&(j+1<m))
                    {
                        if(b[i-1][j+1]>='0')
                            b[i-1][j+1]+=1;
                        else if(b[i-1][j+1]=='.')
                            b[i-1][j+1]='1';
                    }
                    if((i<n)&&(j+1<m))
                    {
                        if(b[i][j+1]>='0')
                            b[i][j+1]+=1;
                        else if(b[i][j+1]=='.')
                            b[i][j+1]='1';
                    }
                    if((i+1<n)&&(j+1<m))
                    {
                        if(b[i+1][j+1]>='0')
                            b[i+1][j+1]+=1;
                        else if(b[i+1][j+1]=='.')
                            b[i+1][j+1]='1';
                    }
                    if((i+1<n)&&(j<m))
                    {
                        if(b[i+1][j]>='0')
                            b[i+1][j]+=1;
                        else if(b[i+1][j]=='.')
                            b[i+1][j]='1';
                    }
                    if((i+1<n)&&(j-1>=0))
                    {
                        if(b[i+1][j-1]>='0')
                            b[i+1][j-1]+=1;
                        else if(b[i+1][j-1]=='.')
                            b[i+1][j-1]='1';
                    }
                    if((i>=0)&&(j-1>=0))
                    {
                        if(b[i][j-1]>='0')
                            b[i][j-1]+=1;
                        else if(b[i][j-1]=='.')
                            b[i][j-1]='1';
                    }

                }
            }
        }

        cout<<"Field #"<<x<<":\n";
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(b[i][j]=='.')
                    cout<<"0";
                else
                    cout<<b[i][j];
            }
            cout<<"\n";
        }
        cout<<"\n";
        delete a;
        delete b;
        cin>>n>>m;
    }
    cout<<"\n";
    return 0;
}


Re: 10189 - Minesweeper

Posted: Thu Nov 14, 2013 12:28 am
by brianfry713
There must be an empty line between field outputs. You're printing extra blank lines at the end.

Re: 10189 - Minesweeper

Posted: Thu Nov 14, 2013 5:53 pm
by venkikumar.m
I removed the last blank line from the code,, still shows WA.....

Code: Select all

#include<iostream>
using namespace std;
int main()
{
    int n,m,i,j,x=0;
    cin>>n>>m;
    while(n&&m)
    {
        x++;
        char **a=new char*[n];
        for(i=0;i<n;i++)
            a[i]=new char[m];
        char **b=new char*[n];
        for(i=0;i<n;i++)
            b[i]=new char[m];
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                cin>>a[i][j];
                b[i][j]=a[i][j];
            }
/*        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                cout<<b[i][j]<<" ";
            cout<<"\n";
        }
*/
       for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(a[i][j]=='*')
                {
                    b[i][j]='*';
                    if((i-1>=0)&&(j-1>=0))
                    {
                        if(b[i-1][j-1]>='0')
                            b[i-1][j-1]+=1;
                        else if(b[i-1][j-1]=='.')
                            b[i-1][j-1]='1';
                    }
                    if((i-1>=0)&&(j>=0))
                    {
                        if(b[i-1][j]>='0')
                            b[i-1][j]+=1;
                        else if(b[i-1][j]=='.')
                           b[i-1][j]='1';
                    }
                    if((i-1>=0)&&(j+1<m))
                    {
                        if(b[i-1][j+1]>='0')
                            b[i-1][j+1]+=1;
                        else if(b[i-1][j+1]=='.')
                            b[i-1][j+1]='1';
                    }
                    if((i<n)&&(j+1<m))
                    {
                        if(b[i][j+1]>='0')
                            b[i][j+1]+=1;
                        else if(b[i][j+1]=='.')
                            b[i][j+1]='1';
                    }
                    if((i+1<n)&&(j+1<m))
                    {
                        if(b[i+1][j+1]>='0')
                            b[i+1][j+1]+=1;
                        else if(b[i+1][j+1]=='.')
                            b[i+1][j+1]='1';
                    }
                    if((i+1<n)&&(j<m))
                    {
                        if(b[i+1][j]>='0')
                            b[i+1][j]+=1;
                        else if(b[i+1][j]=='.')
                            b[i+1][j]='1';
                    }
                    if((i+1<n)&&(j-1>=0))
                    {
                        if(b[i+1][j-1]>='0')
                            b[i+1][j-1]+=1;
                        else if(b[i+1][j-1]=='.')
                            b[i+1][j-1]='1';
                    }
                    if((i>=0)&&(j-1>=0))
                    {
                        if(b[i][j-1]>='0')
                            b[i][j-1]+=1;
                        else if(b[i][j-1]=='.')
                            b[i][j-1]='1';
                    }

                }
            }
        }

        cout<<"Field #"<<x<<":\n";
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(b[i][j]=='.')
                    cout<<"0";
                else
                    cout<<b[i][j];
            }
            cout<<"\n";
        }
        cout<<"\n";
        delete a;
        delete b;
        cin>>n>>m;
    }
    return 0;
}