Im trying to solve a ACM problem 10189,

LINK:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1130

It looks ok to me...but, online judge is showing wrong answer...

can anyone help me to figure out the problem.

#include<stdio.h>

int row = 1, column = 1,i=0, j=0, a=0, b=0;
char array[100][100] = {0};

int change(int a, int b)
{
	if(!(a < 0) && !(a > row -1) && !(b<0) && !(b > column -1))
					if(!(array[a][b] == '*'))
						if(array[a][b] == '.')
							array[a][b] = 49;
						else
							++array[a][b];
	return 0;
}
int main()
{
   freopen("in.txt", "r", stdin);

	int count = 0;

    while(!(row == 0 && column == 0))
	{
	
	scanf("%d %d", &row, &column);

	if(row == 0 && column == 0) return 0;
	count++;

    for(i=0; i<row; i++)
	{
		scanf("\n");			
        for(j=0; j<column; j++)
		{
			scanf("%c", &array[i][j]);
			
		}
	}

    for(i=0; i<row; i++)
	{
        for(j=0; j< column; j++)
		{
			if(array[i][j] == '*')
			{
			a = i-1;
			b = j-1;
			change(a, b);

			a = i-1;
			b = j;
			change(a, b);
			
			a = i-1;
			b = j+1;
			change(a, b);	

			a = i;
			b = j+1;
			change(a, b);

			a = i+1;
			b = j+1;
			change(a, b);

			a = i+1;
			b = j;
			change(a, b);

			a = i+1;
			b = j-1;
			change(a, b);

			a = i;
			b = j-1;
			change(a, b);
			}
		}
	}

	printf("Field #%d:", count);

    for(i=0; i<row; i++)
	{
		printf("\n");

        for(j=0; j< column; j++)
		{
			if(array[i][j] == '.') array[i][j] = 48;
			printf("%c",array[i][j]);
		}

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

    return 0;
}

Though, i have a little confusion about the double "new line"(\n) at the end of the program...i have managed to get ride of it.But it is still showing Wrong Answer. :(

Recommended Answers

All 11 Replies

Member Avatar for iamthwee

I think your i and j checks are OK, the thing that would seem obvious to cause an issue would be the check grid boundaries... But I've not looked at your code closely at all...

I think your i and j checks are OK, the thing that would seem obvious to cause an issue would be the check grid boundaries...

#include<stdio.h>

int main()
{
    int row = 4, column = 4, i = 3, j = 4;
//change i and j for checking the validity

    if(!(i < 0) && !(i > row -1) && !(j<0) && !(j > column -1))
        printf("valid position");
    else
        printf("invalid position");
    return 0;
}

This snippet works perfect...so, how would that code could result in an error?
:(

Member Avatar for iamthwee

I honestly don't care to look into this properly because I'm incredibly lazy but I would...

1) validate each call to your i j check function. For example, if BOTH i and j are within the boundaries give it a green light.
2)Check that you have remember the arrays start at [0,0] and not [1,1] which is a common mistake.

good luck kiddo.


[edit]
Actually that check function looks good also...

Member Avatar for iamthwee

Seems that snippet looks OK.

I would then further split your program up.

Have a function checkValid(). If checkValid() returns true, and that array position == '*' increment count otherwise do nothing.

1) validate each call to your i j check function. For example, if BOTH i and j are within the boundaries give it a green light.

2)Check that you have remember the arrays start at [0,0] and not [1,1] which is a common mistake.

good luck kiddo.

yah, i and j is always within boundary...that's for sure.... :D

Seems that snippet looks OK.

I would then further split your program up.

Have a function checkValid(). If checkValid() returns true, and that array position == '*' increment count otherwise do nothing.

isn't it kinda little....meaningless..cause, im doing the same thing that ur telling....they are being checking at the successive line...so, putting them on the same line wont do any good....while the judge is telling me wrong answer...is it?

I isolated the checking part...but, still there is no improvement.

Member Avatar for iamthwee

I don't know dude...

Mine seems to work...

#include<stdio.h>

int change ( int i, int j );

int row = 3;
int column = 5;

int main()
{


   char grid[100][100] = {"**...", ".....", ".*..."};

   int i, j;
   int a, b;

   int counter = 0;

   for ( i = 0; i < row; i++ )
   {
      printf ( "\n" );

      for ( j = 0; j < column; j++ )
      {

         if ( grid[i][j] == '.' )
         {
            a = i - 1;
            b = j - 1;
            if ( change ( a, b ) == 1 && grid[a][b] == '*' )
            {
               counter++;
            }

            a = i - 1;
            b = j;
            if ( change ( a, b ) == 1 && grid[a][b] == '*' )
            {
               counter++;
            }

            a = i - 1;
            b = j + 1;
            if ( change ( a, b ) == 1 && grid[a][b] == '*' )
            {
               counter++;
            }

            a = i;
            b = j + 1;
            if ( change ( a, b ) == 1 && grid[a][b] == '*' )
            {
               counter++;
            }

            a = i + 1;
            b = j + 1;
            if ( change ( a, b ) == 1 && grid[a][b] == '*' )
            {
               counter++;
            }
            a = i + 1;
            b = j;
            if ( change ( a, b ) == 1 && grid[a][b] == '*' )
            {
               counter++;
            }

            a = i + 1;
            b = j - 1;
            if ( change ( a, b ) == 1 && grid[a][b] == '*' )
            {
               counter++;
            }

            a = i;
            b = j - 1;
            if ( change ( a, b ) == 1 && grid[a][b] == '*' )
            {
               counter++;
            }



            printf ( "%d", counter );

            counter = 0; //reset
         }

         else
            printf ( "%c", '*' );


      }
   }

   getchar();
   return 0;

}




int change ( int i, int j )
{

   //int row = 4, column = 4;
   //change i and j for checking the validity

   if ( ! ( i < 0 ) && ! ( i > row - 1 ) && ! ( j < 0 ) && ! ( j > column - 1 ) )
      return 1; //valid
   else
      return 0; //invalid
}
Member Avatar for iamthwee

That was the last edit...

Good luck... I hope that helped.

That was the last edit...

Good luck... I hope that helped.

In the light of ur code i modified mine...

But still Wrong Answer... :( :( :(
....any more suggestion!

#include<stdio.h>

int row = 1, column = 1,i=0, j=0, a=0, b=0, found=0;
char array[100][100] = {0};

int checkValid(int a, int b)
{
	if(!(a < 0) && !(a > row -1) && !(b<0) && !(b > column -1))
		return 1;		
	else
		return 0;	
}
int main()
{
 //  freopen("in.txt", "r", stdin);

	int count = 0;

    while(!(row == 0 && column == 0))
	{
	
	scanf("%d %d", &row, &column);

	if(row == 0 && column == 0) return 0;
	if(count > 0) printf("\n\n");
	count++;
	
    for(i=0; i<row; i++)
	{
		scanf("\n");			
        for(j=0; j<column; j++)
		{
			scanf("%c", &array[i][j]);
			
		}
	}
	
	printf("Field #%d:", count);
    for(i=0; i<row; i++)
	{
		printf("\n");
        for(j=0; j< column; j++)
		{
			if(array[i][j] == '.')
			{
			a = i-1;
			b = j-1;
			if(checkValid(a, b) && array[a][b] == '*')
				found++;

			a = i-1;
			b = j;
			if(checkValid(a, b) && array[a][b] == '*')
				found++;
			
			a = i-1;
			b = j+1;
			if(checkValid(a, b) && array[a][b] == '*')
				found++;	

			a = i;
			b = j+1;
			if(checkValid(a, b) && array[a][b] == '*')
				found++;

			a = i+1;
			b = j+1;
			if(checkValid(a, b) && array[a][b] == '*')
				found++;

			a = i+1;
			b = j;
			if(checkValid(a, b) && array[a][b] == '*')
				found++;

			a = i+1;
			b = j-1;
			if(checkValid(a, b) && array[a][b] == '*')
				found++;

			a = i;
			b = j-1;
			if(checkValid(a, b) && array[a][b] == '*')
				found++;

			printf("%d", found);

			found = 0;
			}
			else printf("*");
		}
	}
}

    return 0;
}
Member Avatar for iamthwee

Kiddo, what can't you do? Get it to work with scanf?

The algo works as far as I'm concerned. Everything else is just pedantic and that's basic c... Go look at a input tute or file handling tute.

It was accepted at last...there was a small presentation error.Extra new line...grrr...

#include<stdio.h>

int row = 1, column = 1,i=0, j=0, a=0, b=0;
char array[100][100] = {0};

int change(int a, int b)
{
	if(!(a < 0) && !(a > row -1) && !(b<0) && !(b > column -1))
					if(!(array[a][b] == '*'))
						if(array[a][b] == '.')
							array[a][b] = 49;
						else
							++array[a][b];
	return 0;
}
int main()
{
 //  freopen("in.txt", "r", stdin);

	int count = 0;

    while(!(row == 0 && column == 0))
	{

	scanf("%d %d", &row, &column);

	if(row == 0 && column == 0) return 0;
	count++;

    for(i=0; i<row; i++)
	{
		scanf("\n");
        for(j=0; j<column; j++)
		{
			scanf("%c", &array[i][j]);

		}
	}

    for(i=0; i<row; i++)
	{
        for(j=0; j< column; j++)
		{
			if(array[i][j] == '*')
			{
			change(i-1, j-1);
			change(i-1, j);
			change(i-1, j+1);

			change(i, j+1);

			change(i+1, j+1);
			change(i+1, j);
			change(i+1, j-1);

			change(i, j-1);
			}
		}
	}
    if(!(count > 1))
	printf("Field #%d:", count);
    else
    printf("\nField #%d:", count);
    for(i=0; i<row; i++)
	{
		printf("\n");

        for(j=0; j< column; j++)
		{
			if(array[i][j] == '.') array[i][j] = 48;
			printf("%c",array[i][j]);
		}

	}
    printf("\n");
}

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.