Sign in to follow this  
deja-vu_logic

UVa 10189 Minesweeper problem

Recommended Posts

I'm trying to solve this problem but still getting WA. I use the sample input as the input file and the program output is the same as the sample output in the problem, even the output file output the same number of characters including the new line characters as the sample output but the system still showing me WA. I don't know what to do. Any suggestion? This is my code:
#include<iostream>
#include<fstream>

using namespace std;

int main(){
    
    long cols = 0, rows = 0;
    long nThField = 0;
    long mines = 0;
    long checkedRow = 0;
    long checkedCol = 0;
    
    while (cin>>rows>>cols){// get # of row n col
          char field[rows][cols];
          
          if (!rows && !cols) break;
          else if (nThField > 0) cout<<"\n";
          
          for (int i = 0; i < rows; i++){// iterate row
              for (int j = 0; j < cols; j++){// iterate col
                  cin>>field[i][j];
              }
          }
          
          //output the value
          nThField++;
          cout<<"Field #"<<nThField<<":"<<"\n";
          for (int i = 0; i < rows; i++){// iterate row
              for (int j = 0; j < cols; j++){// iterate col
                  if (field[i][j] == '.'){//print the number of mine around
                     // checking for possible mine around in 3x3 area
                     mines = 0;
                     for (int tRow = -1; tRow <= 1; tRow++){
                         for (int tCol = -1; tCol <= 1; tCol++){
                             checkedRow = i + tRow;
                             checkedCol = j + tCol;
                             if (checkedRow < 0 || checkedCol < 0) continue;// if col or row out of bound
                             if (field[i + tRow][j + tCol] == '*'){
                                mines++;
                             }
                         }
                     }
                     cout<<mines;
                  } //end of printing the number of mine
                  else cout<<'*'; //print the mine
              }// end iterating col
              cout<<"\n";
          }// end iterating row
          
    }
    
    return 0;
}



[Edited by - deja-vu_logic on March 6, 2010 5:14:39 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
You need to check the upper boundaries too, not just the lowers.
checkedRow, checkedRow stuff.


Wow, thanks for the solution szecs.
I never took into account such a case because i'm a newbie to problem solving and always thought that the same output as the sample output would always be accepted by the system and never thought that there would be another case.

Share this post


Link to post
Share on other sites
I want to solve the problem with a difference way. So, i used C language to solve in an easy way (cuz i dont know about the C++ well). But, when i sent it to UVA OJ, it still had some problems. I couldn't found it.
Can u help me? Thank you very much!


This is my code:


#include<stdio.h>

int main()
{
char t;
int a[100][100],n,m,i,j,k=0;
scanf("%d%d",&n,&m);
while (n!=0 && m!=0)
{
k++;
for (i=0;i<n;i++)
{ for (j=0;j<m;j++)
{
scanf("%c",&t);
if (t=='.') a[i][j]=0;
else if (t=='*') a[i][j]=20;
else
{
scanf("%c",&t);
if (t=='.') a[i][j]=0;
else if (t=='*') a[i][j]=20;
}
}
}


for (i=0;i<n;i++)
for (j=0;j<m;j++)
if (a[i][j]>=20)
{
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",k);
for (i=0;i<n-1;i++)
{ for (j=0;j<m;j++)
if (a[i][j]>=20) printf("%c",'*');
else printf("%d",a[i][j]);
printf("\n");
}
scanf("%d%d",&n,&m);

}
return 0;
}

Share this post


Link to post
Share on other sites
I see something: now you don't check for field boundaries at all. But the idea is not bad, but you won't be able to use it in a real minesweeper game: because you must only reveal the clicked field.
So the first solution is better, because you can make a real game out of it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this