10189 - Minesweeper

All about problems in Volume CI. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Re: 10189 - Minesweeper

Postby Mohiuddin » Tue Apr 29, 2008 8:36 pm

still getting w a...what can i do further?where's the mistake? is that in output format?

[code][/code]


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

int pos(int x,int y,char **ch,int r,int c)
{
if(x>=0 && y>=0 && x<r && y<c)
{
if(ch[x][y]=='*')
return 1;
}
return 0;
}


int main ()
{
int r,c,k;

char **ch;

for(k=1;;k++)
{
scanf("%d %d",&r,&c);

if(r==0&&c==0)
{
break;
}

int i,j,count;

ch=(char **)malloc(r*sizeof(char*));

for(i=0;i<r;i++)
{
ch[i]=(char *)malloc(c+1);
}

for(i=0;i<r;i++)
{
scanf("%s",ch[i]);
}

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
count=0;

if(ch[i][j]=='.')
{
if(pos(i+1,j,ch,r,c))
{
count++;
}
if(pos(i,j+1,ch,r,c))
{
count++;
}
if(pos(i+1,j+1,ch,r,c))
{
count++;
}
if(pos(i+1,j-1,ch,r,c))
{
count++;
}
if(pos(i-1,j+1,ch,r,c))
{
count++;
}
if(pos(i-1,j,ch,r,c))
{
count++;
}
if(pos(i,j-1,ch,r,c))
{
count++;
}
if(pos(i-1,j-1,ch,r,c))
{
count++;
}

ch[i][j]=count+'0';
}
}
}

printf("\nField #%d:\n",k);

for(i=0;i<r;i++)
{

printf("%s\n",ch[i]);
}

for(i=0;i<r;i++)
{
free(ch[i]);
}

free(ch);
}
return 0;
}
Mohiuddin
New poster
 
Posts: 6
Joined: Fri Apr 25, 2008 12:09 pm

Re: 10189 - Minesweeper

Postby Obaida » Wed Apr 30, 2008 8:56 am

You are printing a blank line after each input. But this shouldn't be...
There should be blank line after each test case.
It should be like this...
Code: Select all
5 5
                   <<-------a blank line(I used, but you didn't)
*****
*...*
*.*.*
*...*
*****             <<---------no blank line(you are using blank line, but I didn't)
Field #1:
*****
*646*
*4*4*
*646*
*****
4 4

*.*.
.***
**..
.*.*
Field #2:
*4*3
4***
**63
3*3*

Remove the code After Accepted. :D
try_try_try_try_&&&_try@try.com
This may be the address of success.
Obaida
A great helper
 
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10189 - Minesweeper

Postby 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.
sdx
New poster
 
Posts: 5
Joined: Wed Apr 30, 2008 2:26 pm

Re: 10189 - Minesweeper

Postby 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
Jan
Guru
 
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh

Re: 10189 - Minesweeper

Postby 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!
sdx
New poster
 
Posts: 5
Joined: Wed Apr 30, 2008 2:26 pm

Re: 10189 - Minesweeper

Postby Jan » Thu May 01, 2008 9:14 am

You shouldn't post 'accepted' codes. I have removed your code.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru
 
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh

Re: 10189 - Minesweeper

Postby 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.
tiagowanderley
New poster
 
Posts: 4
Joined: Thu May 01, 2008 3:49 pm

Re: 10189 - Minesweeper

Postby 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.
Obaida
A great helper
 
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10189 - Minesweeper

Postby tiagowanderley » Thu May 08, 2008 5:03 pm

I really appreciate your answer. I got AC!!!!
Thanks.
tiagowanderley
New poster
 
Posts: 4
Joined: Thu May 01, 2008 3:49 pm

Re: 10189 - Minesweeper

Postby 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.
takohwank
New poster
 
Posts: 4
Joined: Thu May 15, 2008 1:28 pm

Re: 10189 - Minesweeper

Postby 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.
Obaida
A great helper
 
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10189 - Minesweeper

Postby 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..
takohwank
New poster
 
Posts: 4
Joined: Thu May 15, 2008 1:28 pm

WA - Minesweeper

Postby 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;
}

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

Re: 10189 - Minesweeper

Postby 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;
}
User avatar
sreejond
New poster
 
Posts: 32
Joined: Fri May 23, 2008 6:16 pm

10189 - Minesweeper Why WA can anyone help?

Postby 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.
jesun
New poster
 
Posts: 10
Joined: Tue Jan 01, 2008 10:55 pm

PreviousNext

Return to Volume CI

Who is online

Users browsing this forum: No registered users and 1 guest