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

sdx
New poster
Posts: 5
Joined: Wed Apr 30, 2008 2:26 pm

Re: 10189 - Minesweeper

Post by sdx » Wed Apr 30, 2008 2:35 pm

Hi,

I too am getting a WA for no apparent reason. How I wish, the judge be a little more helpful! I'd be grateful if someone from the community could help. Here is my program ...

Code: Select all

#include <iostream>
#include <vector>

using namespace std;

typedef vector< vector<char> > Mine;

void printMine(Mine mine)
{
	for(int r=0; r<mine.size(); r++)
	{
		for(int c=0; c<mine[r].size(); c++)
		{
			cout<<mine[r][c];
		}
		cout<<endl;
	}
}

void Initialize(Mine &outMine, size_t rows, size_t cols)
{
	for(int r=0; r<rows; r++)
	{
		outMine.push_back(vector<char>(cols));
		for(int c=0; c<cols; c++)
		{
			outMine[r][c] = '0';
		}
	}
}

void putMine(int row, int col, Mine &outMine)
{
	//Errorlog
	//1. Was incrementing a char even if it was a * instead of a number!

	int numRows = outMine.size();
	int numCols = outMine[0].size();
	outMine[row][col] = '*';
	for(int r = row - 1; r <= row + 1; r++)
	{
		for(int c = col - 1; c <= col + 1; c++)
		{
			if((r>=0 && r<numRows) && (c>=0 && c<numCols) && outMine[r][c]!='*')
				outMine[r][c] += 1;
		}
	}
}

void processMineField(Mine mine)
{
	//printMine(mine);
	//Initialize a zero minefield of the same size as the input minefield.
	Mine outMine;
	Initialize(outMine, mine.size(), mine[0].size());

	for(int r=0; r<mine.size(); r++)
	{
		for(int c=0; c<mine[r].size(); c++)
		{
			if(mine[r][c] == '*')
			{
				putMine(r,c, outMine);
			}
		}
	}

	printMine(outMine);

}

void MineSweeper()
{
	//rows,cols<-(0,100]
	int rows=0, cols=0;
	int fieldNumber = 0;
	while(cin>>rows>>cols)
	{
		if(rows != 0 && cols != 0)
		{
			cout<<"Field #"<<++fieldNumber<<":\n";
			Mine mine(rows);
			for(int r=0; r<rows; r++)
			{
				for(int c=0; c<cols; c++)
				{
					char ch;
					cin>>ch;
					mine[r].push_back(ch);
				}
			}
			processMineField(mine);
			cout<<endl;
		}
		else
		{
			break;
		}
	}
}

#if defined ONLINE_JUDGE || defined MINESWEEPER
int main()
{
	MineSweeper();
	return 0;
}
#endif
Thanks in advance.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 10189 - Minesweeper

Post by Jan » Wed Apr 30, 2008 3:28 pm

A blank line should be printed between consecutive cases, not after every case.
Your code prints an extra blank line at the end.
Ami ekhono shopno dekhi...
HomePage

sdx
New poster
Posts: 5
Joined: Wed Apr 30, 2008 2:26 pm

Re: 10189 - Minesweeper

Post by sdx » Thu May 01, 2008 8:53 am

Thanks a ton buddy. Here is the modified code for everybody's reference...

Code: Select all

Removed...
Khoob khoob dhonnobad!

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 10189 - Minesweeper

Post by Jan » Thu May 01, 2008 9:14 am

You shouldn't post 'accepted' codes. I have removed your code.
Ami ekhono shopno dekhi...
HomePage

tiagowanderley
New poster
Posts: 4
Joined: Thu May 01, 2008 3:49 pm

Re: 10189 - Minesweeper

Post by tiagowanderley » Thu May 08, 2008 2:26 am

I am getting Wrong Answer. Don't know what is the problem.
and I tested all the cases which are present in this forum. It has passed all of them. Help-me please.

Code: Select all

 removed 
Last edited by tiagowanderley on Thu May 08, 2008 5:05 pm, edited 1 time in total.

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10189 - Minesweeper

Post by Obaida » Thu May 08, 2008 8:31 am

Your Output is...

Code: Select all

5 5                          
*****
*...*
*.*.*
*...*
*.*.*
*...*
*****
Filed #1:
*****
*646*
*4*4*
*646*
*****4 4                <<---------This should be separated in a new line.
*.*.            <<--------after 4 4 there will be a blank line(after every case less first one).
.***
**..
.*.*
                           <<--------There shouldn't be a blank line
Field #2:
*4*3
4***
**63
3*3*
Remove your Code After Accepted.
try_try_try_try_&&&_try@try.com
This may be the address of success.

tiagowanderley
New poster
Posts: 4
Joined: Thu May 01, 2008 3:49 pm

Re: 10189 - Minesweeper

Post by tiagowanderley » Thu May 08, 2008 5:03 pm

I really appreciate your answer. I got AC!!!!
Thanks.

takohwank
New poster
Posts: 4
Joined: Thu May 15, 2008 1:28 pm

Re: 10189 - Minesweeper

Post by takohwank » Thu May 15, 2008 1:32 pm

hi, i've tried all the test cases but allways got WA, and i don't know why.

somebody help me.. (thanx b4 :D )

Code: Select all

removed after ac
Last edited by takohwank on Thu May 15, 2008 5:55 pm, edited 1 time in total.

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10189 - Minesweeper

Post by Obaida » Thu May 15, 2008 1:52 pm

Here is my Output.

Code: Select all

5 5
*****
*...*
*.*.*
*...*
*****             
Field #1:
*****
*646*
*4*4*
*646*
*****         <<----not here
4 4
               <<----Here is the blank line.
*.*.
.***
**..
.*.*
Field #2:
*4*3
4***
**63
3*3*
try_try_try_try_&&&_try@try.com
This may be the address of success.

takohwank
New poster
Posts: 4
Joined: Thu May 15, 2008 1:28 pm

Re: 10189 - Minesweeper

Post by takohwank » Thu May 15, 2008 5:54 pm

waw, thanx Obaida! :D :D :D
thanx alot..

i've got AC now!
hehehe...

thank you very much..

yhlu
New poster
Posts: 1
Joined: Sat Jun 14, 2008 5:18 pm

WA - Minesweeper

Post by yhlu » Sat Jun 14, 2008 5:22 pm

Kept on getting WA... Don't know why...

Code: Select all

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;


static char mines[101][101];


void increaseCurrent(int i, int j)
{
  // check boundary
  if(i >=0 && j>=0 && i < 100 && j < 100){
    mines[i][j] = (mines[i][j] == '*' ? '*' : mines[i][j]+1);
  }
}

void increaseNeighbors(int i, int j)
{
  increaseCurrent(i-1, j-1);
  increaseCurrent(i-1, j);
  increaseCurrent(i-1, j+1);
  increaseCurrent(i, j+1);
  increaseCurrent(i+1, j+1);
  increaseCurrent(i+1, j);
  increaseCurrent(i+1, j-1);
  increaseCurrent(i, j-1);
}


int main()
{
  int m,n;
  string buf;

  int field = 0;
  while(cin>>m>>n){
    if(m == 0 && n ==0){
      break;
    }
    // read to the end of the line
    getline(cin, buf);
    

    // clear the mine field
    for(int i=0; i < m; ++i){
      for(int j=0; j < n; ++j){
        mines[i][j] = '0';
      }
    }

    // process row by row
    for(int i=0; i < m; ++i){
      getline(cin, buf);
      //debug cout<<buf<<endl;
      
      for(int j=0; j < n; ++j){
        if(buf[j] == '*'){
          mines[i][j] = '*';
          increaseNeighbors(i,j);
        }
      }
    }

    // print the mine field
    cout<<"Field #"<<++field<<":"<<endl;
    for(int i=0; i < m; ++i){
      for(int j=0; j < n; ++j){
        cout<<mines[i][j];
      }
      cout<<endl;
    }
    cout<<endl;
  }

  return 0;
}


User avatar
sreejond
New poster
Posts: 32
Joined: Fri May 23, 2008 6:16 pm
Contact:

Re: 10189 - Minesweeper

Post by sreejond » Mon Jun 30, 2008 4:38 pm

very easy problem but i cant understand why getting TLE.
can anyone help me?

here is my code:

Code: Select all

#include<stdio.h>
#include<string.h>

char a[111][111];
long b[111][111];

int main()
{
	long i,j,f,n,m,count;

	f=count=0;
	while(scanf("%ld%ld",&n,&m)==2,n!=0,m!=0)
	{
		count++;
		if(f==1)
			printf("\n");
		f=1;

		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));

		for(i=1;i<=n;i++)
		{
			fflush(stdin);
			for(j=1;j<=m;j++)
				scanf("%c",&a[i][j]);
		}

		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			{
				if(a[i][j]=='*')
				{
					b[i][j]=42;
					if(a[i][j-1]!=42)
						b[i][j-1]++;
					if(a[i][j+1]!=42)
						b[i][j+1]++;
					if(a[i-1][j-1]!=42)
						b[i-1][j-1]++;
					if(a[i-1][j]!=42)
						b[i-1][j]++;
					if(a[i-1][j+1]!=42)
						b[i-1][j+1]++;
					if(a[i+1][j-1]!=42)
						b[i+1][j-1]++;
					if(a[i+1][j]!=42)
						b[i+1][j]++;
					if(a[i+1][j+1]!=42)
						b[i+1][j+1]++;
				}					
			}

		printf("Field #%ld:\n",count);
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				if(a[i][j]==42)
					printf("*");
				else
					printf("%ld",b[i][j]);
			}
			printf("\n");
		}
	}
	return 0;
}

jesun
New poster
Posts: 10
Joined: Tue Jan 01, 2008 10:55 pm

10189 - Minesweeper Why WA can anyone help?

Post by jesun » Tue Jul 08, 2008 4:14 pm

I am getting WA for this easy problem & becoming frustrated.Can anyone give me a suggestion?here is my code

Code: Select all

removed after AC
Last edited by jesun on Sat Jul 12, 2008 7:12 pm, edited 1 time in total.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 10189 - Minesweeper

Post by Jan » Wed Jul 09, 2008 7:11 am

Your code prints a blank line before the first case. Remove it. Print a blank line between cases.

To sreejon, replace the following line

Code: Select all

while(scanf("%ld%ld",&n,&m)==2,n!=0,m!=0)
with

Code: Select all

while(scanf("%ld%ld",&n,&m)==2 && n!=0 && m!=0)
Ami ekhono shopno dekhi...
HomePage

jesun
New poster
Posts: 10
Joined: Tue Jan 01, 2008 10:55 pm

Re: 10189 - Minesweeper

Post by jesun » Wed Jul 09, 2008 8:29 pm

Thanks Jan.I have modified my code as per suggestion of U .However,still getting WA.here is my modified code:

Code: Select all

removed after AC
Last edited by jesun on Sat Jul 12, 2008 7:13 pm, edited 1 time in total.

Post Reply

Return to “Volume 101 (10100-10199)”