Page 9 of 28

Posted: Tue Jul 17, 2007 10:29 pm
by Jan
Just check this.

Code: Select all

if((a[i+1][j-1]=='*')&& i+1< m && j-1>= 0 )
Suppose j = 0 then you are first trying to check a[][-1] (searching for a negative index), then you are checking whether j-1>=0 or not. So, better to use

Code: Select all

if(j-1>= 0 && i+1<m && (a[i+1][j-1]=='*'))
Now, your code first checks whether j-1>=0 or not. If j=0 then other conditions will not be checked.

Another Idea:

You can write a function which will return true if the position is valid and it contains a '*'. Suppose the function is valid(i,j). Then for any valid position(i,j) you can write

Code: Select all

count = valid(i,j+1) + valid(i+1,j) + valid(i,j-1) + ...
Then it would be easier to detect error.

Hope these help.

P.S. Dont forget to remove your previous code.

What's wrong?!

Posted: Sun Sep 09, 2007 8:35 pm
by Arashk_kh68
Hi, I have tested my code with test cases provided above, and all of them answered correctly, Can anyone please tell me what's wrong with my code?

Code: Select all

Removed After AC.
I know I have coded this so messy, sorry, but I have some problems with function calls, because Im new in C++ and confused with Pointers, MD-Arrays , etc.

Thanks for your help!

Posted: Sun Sep 09, 2007 11:43 pm
by Jan
Your code looks ok. But read the description again
There must be an empty line between field outputs
You are printing a blank line after every case. You should get PE, but I am not sure about the new judge. So, try changing accoring to this. Hope it helps.

Still WA

Posted: Mon Sep 10, 2007 7:01 am
by Arashk_kh68
Hi , thanks for the hint, but did u mean that i change "endl" with "\n"?!
cause i did this, and still WA.

Posted: Mon Sep 10, 2007 6:48 pm
by Jan
No no. There should be a blank line between cases. But you are printing an extra blank line (the last one). So, endl or '\n' is not the problem. Try to find a way such that your code prints a blank line between cases. Hope it helps.

Posted: Tue Sep 11, 2007 6:46 pm
by Arashk_kh68
Thanks a lot Jan, AC after removing last blank line.

Posted: Thu Sep 13, 2007 8:18 pm
by Dao007forever
Sorry, but I still stuck??

Code: Select all

#include <cstdio>

#define MAX 110
#define FIN "Mine.in"
#define FOU "Mine.ou"
#define DEBUG 0

int a[MAX][MAX],b[MAX][MAX];
int m,n;

int main()
{
    if (DEBUG)
    {
       freopen(FIN,"r",stdin);
       freopen(FOU,"w",stdout);
    }
    int count=0;
    char c;
    while (1)
    {
          scanf("%d%d",&m,&n);
          if (!m&&!n) break;
          
          if (count) printf("\n\n");
          
          count++;
          
          for (int i=0;i<=m+1;i++)
              for (int j=0;j<=n+1;j++)
              {
                  a[i][j]=0;
                  b[i][j]=0;
              }
          
          for (int i=1;i<=m;i++)
          {
              for (int j=1;j<=n;j++)
              {
                  do
                    scanf("%c",&c);
                  while ((c!='*')&&(c!='.'));
                  a[i][j] = (c=='*');
              }
              
          }
          for (int i=1;i<=m;i++)
              for (int j=1;j<=n;j++)
                  if (!a[i][j]) b[i][j] = a[i-1][j-1] + a[i-1][j] + a[i-1][j+1] + a[i][j-1]   +           + a[i][j+1] + a[i+1][j-1] + a[i+1][j] + a[i+1][j+1];
          printf("Field #%d:\n",count);
          for (int i=1;i<=m;i++)
          {
              for (int j=1;j<=n;j++)
                  if (!a[i][j]) printf("%d",b[i][j]);
                  else printf("*");
              if (i!=m) printf("\n");
          }
    }
    
    if (DEBUG)
    {
       fclose(stdin);
       fclose(stdout);
    }
    return 0;
}

Posted: Fri Oct 12, 2007 1:29 pm
by lnr

Code: Select all

Removed.
Thanks Jan.

Posted: Fri Oct 12, 2007 2:28 pm
by Jan
You should a blank line between two cases, not after every case.

I hav a problem like this in 10189

Posted: Sat Oct 20, 2007 12:11 am
by GeorgeDePaulo
HI, i am tring to solve the problem 10189 but get only WA. My output is the same Jan has benn posted but l i got WA, any one, please help me to find the bug. This is my code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(){
	int mineField[105][105];
	int m, n, i, j, size, cont, campo=1, flag=1;
	char field;

	while ( scanf("%d %d", &m, &n) == 2 && m != 0 && n !=0){
		size = m*n;
		for ( i=0; i < m; i++){
			for(j=0; j < n; j++ )mineField[i][j]=0;
		}
		cont=0;
		i=0;
		j=0;
		while ( i < m  && j < n ){
			scanf("%c",&field);
			if ( field=='*' || field=='.'){
				j = cont%n;
				if ( field == '*' ){
					mineField[i][j]=-10;
					if ( i != 0){
						mineField[i-1][j]++;
						if( j != 0 )mineField[i-1][j-1]++;
						if( j != n-1)mineField[i-1][j+1]++;
					}
					if ( i != m-1){
						mineField[i+1][j]++;
						if(j != 0)mineField[i+1][j-1]++;
						if(j != n-1)mineField[i+1][j+1]++;
					}
					if ( j != 0 )mineField[i][j-1]++;
					if ( j != n-1)mineField[i][j+1]++;
				}
				if ( j == n-1) i++; 
				cont++;

			}
		}
		if(flag != 1){ printf("\n\n");}else flag=0;
		printf("Field #%d:\n",campo);
		for (i=0;i<m;i++){
			for(j=0;j<n;j++){
				if( mineField[i][j] < 0 )printf("*");else
				printf("%d",mineField[i][j]);
			}
			if(i != m-1) printf("\n");
		}
		campo++;
	}
	return 0;
}

Posted: Sun Dec 02, 2007 8:50 pm
by Jan
Your code looks correct to me. Try updating the line..

Code: Select all

scanf(" %c",&field); // note that there is a space before %c
Hope it helps.

acm-10189

Posted: Wed Dec 05, 2007 7:28 am
by hridoy
CAN any one please tell me why I m getting CE??

Code: Select all

#include<iostream>
using namespace std;

int check(int i, int j, int x, int y, char b[][100]);

void main(void)
{
	int i,j,k=0,m,n,z;
	char a[100][100];
	while(1)
	{
		cin >> m >> n;
		if(m==0&&n==0)
			break;
		for(i=0;i<m;i++)
			for(j=0;j<n;j++)
				cin >> a[i][j];
		k++;
		for(i=0;i<m;i++)
			for(j=0;j<n;j++)
				if(a[i][j]!='*')
				{
					z=check(i,j,m-1,n-1,a);
					a[i][j]=z;
				}

		cout << "Field #" << k << ":\n";
		for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
				cout << a[i][j];
			cout << "\n";
		}
		cout << "\n";
	}
}

int check(int i, int j, int x, int y, char b[][100])
{
	int k=i-1,l=i+1,m=j-1,n=j+1,p=0,q,w;
	char a[100][100];

	for(q=0;q<=x;q++)
		for(w=0;w<=y;w++)
			a[q][w]=b[q][w];
	if(i==0)
		k=0;
	if(i==x)
		l=i;
	if(j==0)
		m=0;
	if(j==y)
		n=j;
	if(a[k][m]=='*')
	{
		a[k][m]=-1;
		p++;
	}
	if(a[i][m]=='*')
	{
		a[i][m]=-1;
		p++;
	}
	if(a[l][m]=='*')
	{
		a[l][m]=-1;
		p++;
	}
	if(a[k][j]=='*')
	{
		a[k][j]=-1;
		p++;
	}
	if(a[l][j]=='*')
	{
		a[l][j]=-1;
		p++;
	}
	if(a[k][n]=='*')
	{
		a[k][n]=-1;
		p++;
	}
	if(a[i][n]=='*')
	{
		a[i][n]=-1;
		p++;
	}
	if(a[l][n]=='*')
	{
		a[l][n]=-1;
		p++;
	}
	return (p+48);
}

Posted: Wed Dec 05, 2007 11:45 am
by rio
Trying to compile with g++ 4.1 compiler had below error.
hr.cpp:6: error: ::main must return int
-----
RIo

HELP!!

Posted: Fri Jan 04, 2008 6:02 pm
by jackpigman
Can somebody help me with my code? I've got a RTE and just can't fix it!!
Here's my code:

Code: Select all

Code removed after AC

Posted: Fri Jan 04, 2008 11:48 pm
by Samiul
Your array indexing is not ok. In if(e == '*') condition, you are using three if's, among which one should be accesed. Now guess when a = 100 and b = 100, a * b - 1 = 9999. Now in the three if's you used some array indexing like in the first if c[d + b], in the second if also c[d + b] and in the third if c[d + 1]. Now do you see that when d = 9999, you are going out of boundary of c(0 - 9999). This is giving you RTE.

And another thing, every first time for an input set, you would try to read e, you would read the 10 after a, b. So you better use the line scanf("%d %d\n" , &a , &b).

I don't know what you think but I think using a 2 dimwnsional array in such problems would be easier.