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

10189 (Minesweeper): WA

Postby SP8472 » Tue Nov 29, 2005 5:40 pm

The code works on lots of test cases, including any "special" case I could think of. I believe also made the input parsing as robust as I possibly could. So...why does this code get WA? I compared it to code posted here that people said to be accepted, and I don't think I'm doing anything differently.

Code: Select all
/* DELETED */
Last edited by SP8472 on Tue Nov 29, 2005 9:18 pm, edited 1 time in total.
SP8472
New poster
 
Posts: 9
Joined: Tue Nov 29, 2005 5:27 pm
Location: Karlsruhe, Germany

Postby mamun » Tue Nov 29, 2005 8:45 pm

My goodness! You forgot to initiate fld in main(). :wink:
mamun
A great helper
 
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh

Postby SP8472 » Tue Nov 29, 2005 9:04 pm

Accepted.

Please excuse me while I go beat my head against the wall a few times. Hard.

I've been debugging this thing for ages, including two rewrites.

My local compiler just quietly initializes the variable to zero. It doesn't even warn, even though I'm using -Wall.
SP8472
New poster
 
Posts: 9
Joined: Tue Nov 29, 2005 5:27 pm
Location: Karlsruhe, Germany

Postby mamun » Tue Nov 29, 2005 9:16 pm

Take it easy. Don't break your head. :wink:
Why don't you better delete your code from the post?
mamun
A great helper
 
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh

Postby SP8472 » Tue Nov 29, 2005 9:17 pm

Just checked the gcc manpage. According to that, gcc is unable to warn about the use of uninitialized variables if not compiling with (at least) -O1. Guess I'll have to add that to my standard compilation parameters to avoid this kind of embarrassment in the future.
SP8472
New poster
 
Posts: 9
Joined: Tue Nov 29, 2005 5:27 pm
Location: Karlsruhe, Germany

Postby Roby » Wed Nov 30, 2005 6:00 am

thanx Rocky
Sorry for the late reply...
User avatar
Roby
Experienced poster
 
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia

10189 in Java - WA?

Postby SigmaJargon » Tue Jan 24, 2006 2:58 am

I keep on getting WA for this, but I can't for the life of me see what is wrong.

Here is the source code and two test cases. If anybody could please explain where I am going wrong, it would be greatly appreciated.

Code: Select all
import java.io.*;

class Main
{

    int[][] SweeperField;
   
    void SetupSweeperField(int x, int y)
    {
       
        SweeperField = new int[x][y];
       
    }
   
    String Begin(String Input)
    {
       
        int Axis = Input.indexOf(" ");
       
        byte x = Byte.parseByte(Input.substring(0, Axis));
        byte y = Byte.parseByte(Input.substring(Axis + 1, Input.length()));
       
        SetupSweeperField(x,y);
       
        for (int i = 0; i < x; i++)
        {
           
            Input = readLn(255);
            int LatestMine = Input.indexOf("*");
           
            while (LatestMine != -1)
            {
               
                Increment(i, LatestMine);
                LatestMine = Input.indexOf("*", LatestMine+1);
               
            }
           
        }

        return ToString();
       
    }
   
    void Begin()
    {
       
        String Answer = "";
       
        String Input = readLn(255);
       
        int Counter = 1;
       
        while (!Input.equals("0 0"))
        {
           
            Answer += "Field #" + Counter + ":\n" + Begin(Input) + "\n";
            Input = readLn(255);
            Counter++;
           
        }
       
        System.out.print(Answer.substring(0, Answer.length() - 2));
       
    }
   
    static void main(String[] args)
    {
       
        Main Sweeper = new Main();
        Sweeper.Begin();
       
    }
   
    void Increment(int x, int y)
    {
       
        for (int i = -1; i <= 1; i++)
        {
           
            for (int j = -1; j <= 1; j++)
            {
               
                if ((((x+i) >= 0) && ((x+i) < SweeperField.length) && ((y+j) >= 0) && ((y+j) < SweeperField[0].length)))
                {
                   
                    SweeperField[x+i][y+j]++;
                   
                }
               
            }
       
        }
       
        SweeperField[x][y] = -5;
       
    }
       
    String ToString()
    {
       
        String Answer = "";
       
        for (int i = 0; i < SweeperField.length; i++)
        {
           
            for (int j = 0; j < SweeperField[0].length; j++)
            {
               
                if (SweeperField[i][j] < 0)
                    Answer += "*";
                else
                    Answer += SweeperField[i][j];
               
            }
           
            Answer += "\n";
           
        }
       
        return Answer;
       
    }

    static String readLn (int maxLg)
    {
        byte lin[] = new byte [maxLg];
        int lg = 0, car = -1;
        String line = "";
     
        try
        {
           
            while (lg < maxLg)
            {
               
                car = System.in.read();
                if ((car < 0) || (car == '\n')) break;
                lin [lg++] += car;
               
           }
           
        }
        catch (IOException e)
        {
           
            return (null);
       
        }
     
        if ((car < 0) && (lg == 0))
            return (null);
           
        return (new String (lin, 0, lg));
    }
   
}

Code: Select all
3 4
...*
**..
..*.
5 5
..*..
..*..
**...
.....
....*
3 6
***..*
......
..*...
0 0
Field #1:
222*
**32
23*1

Field #2:
02*20
24*20
**210
22111
0001*

Field #3:
***11*
243211
01*100
Code: Select all
2 2
..
.*
3 3
..*
**.
...
1 8
....**.*
10 7
.......
...*..*
*....**
......*
..*....
.......
.......
..*...*
*...*..
*.....*
0 0
Field #1:
11
1*

Field #2:
23*
**2
221

Field #3:
0001**2*

Field #4:
0011111
111*23*
*1112**
121113*
01*1011
0111000
0111011
12*212*
*312*32
*20112*
SigmaJargon
New poster
 
Posts: 2
Joined: Tue Jan 24, 2006 2:39 am

10189 Minesweeper; Runtime Error

Postby Qodeus » Mon Mar 06, 2006 10:41 pm

Hi!
I have just started programming in C++, and I found this easy problem. I have submited solutions for this problem a lot of times and always the answer from the judge is Runtime Error. And, also, it seams to me that for every test case I can think of the program works fine.
This is my code.

Code: Select all
#include <iostream>
#include <string>
using namespace std;
char mines[101][101];

int count(int red,int kol,int maxred, int maxkol)
{
    int u=0;
    if (red!=maxred)
    {
       if (mines[red+1][kol]=='*') u++;
    }
    if (red!=1)
    {
       if (mines[red-1][kol]=='*') u++;
    }
    if (kol!=maxkol)
    {
       if (mines[red][kol+1]=='*') u++;
    }
    if (kol!=1)
    {
       if (mines[red][kol-1]=='*') u++;
    }
   
    if (kol!=1&&red!=1)
    {
       if (mines[red-1][kol-1]=='*') u++;
    }
   
    if (kol!=maxkol&&red!=maxred)
    {
       if (mines[red+1][kol+1]=='*') u++;
    }
   
    if (kol!=1&&red!=maxred)
    {
       if (mines[red+1][kol-1]=='*') u++;
    }
   
    if (kol!=maxkol&&red!=1)
    {
       if (mines[red-1][kol+1]=='*') u++;
    }

    return u;
}


int main()
{
    int n=0,m=0,q=0;
    char nmines[101][101];

    string s[100],s1="";
    while(1)
    {


         cin >> n >> m;


       if (n==0||m==0) return 0;
       q++;
       
       for (int i=1;i<=n;i++)
       {
           do
           {
               cin >> s[i];
           }
           while (s[i].length()!=m);
       }
     
       for (int i=1;i<=n;i++)
       {
           for (int j=0;j<s[i].length();j++)
           {
               s1=s[i];
               mines[i][j+1]=s1[j];
           }   
       }
       cout << "Field #" << q << ":" << endl;
       for (int i=1;i<=n;i++)
       {
           for (int j=1;j<=m;j++)
           {
               if (mines[i][j]!='*')
                  nmines[i][j]=count(i,j,n,m)+'0';
               else
                   nmines[i][j]='*';
           }
       }
       
       for (int i=1;i<=n;i++)
       {
           for (int j=1;j<=m;j++)
           {
               cout << nmines[i][j];
           }
           cout << endl;
       }
       cout << endl;
    }
    return 0;
}


Please could someone help me find out what the problem is?!
Thanks in advance!
Qodeus
New poster
 
Posts: 6
Joined: Mon Mar 06, 2006 10:32 pm

Postby kai » Thu Mar 09, 2006 9:08 am

One mistake I've found:

You have s[0] ... s[99].
Code: Select all
    string s[100],s1="";

However, when n=100, the program will use s[100].
Code: Select all
       for (int i=1;i<=n;i++)
       {
           do
           {
               cin >> s[i];
           }
           while (s[i].length()!=m);
       }
kai
New poster
 
Posts: 5
Joined: Mon Aug 08, 2005 2:36 pm
Location: Japan

Postby Qodeus » Thu Mar 09, 2006 6:48 pm

Oh, I can't beleve that i didn't see that. :oops: Thanks a lot man! :D
Qodeus
New poster
 
Posts: 6
Joined: Mon Mar 06, 2006 10:32 pm

10189 mines WA!

Postby xivphoenix » Thu May 25, 2006 3:01 pm

I've no idea what's wrong whith my code.somebody plz help. THX.

Here's my code.

Code: Select all
#include <stdio.h>

int main(void)
{
   int arr[150][150];
   int i,j,mrow,mcol,cnt=0;
   char c[200];

   while(scanf("%d%d ",&mrow,&mcol)!=EOF) {
      if(mrow==0 && mcol==0) break;
      printf("Field #%d:\n",++cnt);

      for(i=0 ; i<mrow ; i++) {
         scanf("%s",c);
         for(j=0 ; j<mcol ; j++) {
            if(c[j]=='*')    arr[i][j] = -100;
            else                      arr[i][j] =  0;
         }
      }

      for(i=0 ; i<mrow ; i++) {
         for(j=0 ; j<mcol ; j++) {
            if (arr[i][j] < 0) {
               arr[i-1][j-1] ++;  arr[i-1][j] ++;    arr[i-1][j+1] ++;
               arr[i][j-1] ++;      arr[i][j] ++;      arr[i][j+1] ++;
               arr[i+1][j-1] ++;  arr[i+1][j] ++;    arr[i+1][j+1] ++;
            }
         }
      }

      for(i=0 ; i<mrow ; i++) {
         for(j=0 ; j<mcol ; j++) {
            if (arr[i][j] > -1) printf("%d",arr[i][j]);
            else                       printf("*");
         }
         printf("\n");
      }

      printf("\n");

   }

   return 0;
}
xivphoenix
New poster
 
Posts: 3
Joined: Wed May 24, 2006 3:29 pm
Location: Thailand

Postby ayeshapakhi » Mon May 29, 2006 8:21 pm

hi there!
i didnt run ur code but probably u r getting out of array boundary by these lines.........

for(i=0 ; i<mrow ; i++) {
for(j=0 ; j<mcol ; j++) {
if (arr[i][j] < 0) {
arr[i-1][j-1] ++; arr[i-1][j] ++; arr[i-1][j+1] ++;
arr[i][j-1] ++; arr[i][j] ++; arr[i][j+1] ++;
arr[i+1][j-1] ++; arr[i+1][j] ++; arr[i+1][j+1] ++;
}
}
}
what abt arr[0-1][0-1]???
ayeshapakhi
Learning poster
 
Posts: 60
Joined: Sun Apr 16, 2006 7:59 pm

Postby SHAHADAT » Sun Jun 25, 2006 9:53 am

Please delete your code....
And it,s not good idea to upload a code and say whats wrong?????
Try to disscuss about algorithm....
SHAHADAT
New poster
 
Posts: 23
Joined: Thu Jun 22, 2006 8:55 am
Location: sust,bangladesh

10189WA

Postby AikoSenoo » Tue Jul 18, 2006 5:34 pm

I have tried many means to test,but still got WA.

Would you please help me to find where my code's problem is?

Thanks a lot!

There is my code:
Code: Select all
#include<stdio.h>
#include<stdlib.h>
char a[102][102];
int n,m;
int check(int j,int i,int m){
        int count=0;
        if(a[j][i]=='*')
            return 9;
        else {
            if(j!=0){     
                  if(a[j-1][i-1]=='*' && i!=0)
                      count++;           
                  if(a[j-1][i]=='*')
                        count++;
                  if(a[j-1][i+1]=='*')
                        count++;
               
            }
            if(a[j][i-1]=='*' && i!=0)
                count++;
            if(a[j][i+1]=='*')
                count++;
            if(j!=m){
              if(a[j+1][i-1]=='*' && i!=0)
                  count++;
              if(a[j+1][i]=='*')
                  count++;
              if(a[j+1][i+1]=='*')
                  count++;
            }
            return count+'0';
        }
                                                                               
}
                                                                               
int main(void){
    int cnt=0;
    int i,j;
                                                                               
    while(1){
        scanf("%d %d",&n,&m);
        if(n==0 && m==0)
          break;
        cnt++;
        for(j=0;j<n;j++){
            for(i=0;i<m;i++){
                a[j][i]='.';
            }
        }
        for(j=0;j<n;j++){
            for(i=0;i<m;i++){
                scanf(" %c",&a[j][i]);
            }
        }
        for(j=0;j<n;j++){
            for(i=0;i<m;i++){
                if(check(j,i,m)!=9)
                    a[j][i]=check(j,i,m);
            }
        }
        if(cnt>1)printf("\n");
        printf("Field #%d:\n",cnt);
        for(j=0;j<n;j++){
            for(i=0;i<m;i++)
                printf("%c",a[j][i]);
            printf("\n");
        }
       
    }
    return 0;
}
AikoSenoo
New poster
 
Posts: 3
Joined: Tue Jul 18, 2006 5:28 pm

Postby Darko » Tue Jul 18, 2006 6:52 pm

Clear the whole array, not just mxn (if board is smaller than the previous one, you might get WA) Or clear it after each case (not at the beginning).
Darko
Guru
 
Posts: 572
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

PreviousNext

Return to Volume CI

Who is online

Users browsing this forum: Google [Bot] and 1 guest