[Community Puzzle] Reverse Minesweeper

https://www.codingame.com/training/easy/reverse-minesweeper

Send your feedback or ask for help here!

Created by @psychedelic68,validated by @snoyes,@davilla and @Alain-Delpuch.
If you have any issues, feel free to ping them.

hi,
i don’t understand why i have too much char and why the final count is bad.
the Tests 1 and 4 are OK.

class Solution
{
    static void Main(string[] args)
    {
        int w = int.Parse(Console.ReadLine());
        int h = int.Parse(Console.ReadLine());
        
        List<List<int>> tab = new List<List<int>>();
        
        for (int i = 0; i < h; i++)
        {
            
            string line = Console.ReadLine();
            Console.Error.WriteLine(line);
            line= line.Replace('.','0');
            line= line.Replace('x','9');
            List<int>tableau = new List<int>();
            for (int j=0;j<w;j++)
            {
                tableau.Add(int.Parse(line[j].ToString()));
            }
            //Console.Error.WriteLine(tableau.Contains(9));
            tab.Add(tableau);
            //Console.Error.WriteLine(tab[i] [0]);
        }
        //Console.Error.WriteLine(tab[8][15].ToString());
        
        for (int i=0;i<h;i++)
        {
           for (int j=0;j<w;j++)
           if (tab[i][j]==9)
           {
               try
               {
                   tab[i-1][j-1]++;
               }catch{
                   Console.Error.WriteLine("out found for "+i +" "+j );
               }
               try
               {
                   tab[i-1][j]++;
               }catch{
                   Console.Error.WriteLine("out found for "+i +" "+j );
               }
               try
               {
                   tab[i-1][j+1]++;
               }catch{
                   Console.Error.WriteLine("out found for "+i +" "+j );
               }
               try
               {
                   tab[i][j-1]++;
               }catch{
                   Console.Error.WriteLine("out found for "+i +" "+j );
               }
               try
               {
                   tab[i][j+1]++;
               }catch{
                   Console.Error.WriteLine("out found for "+i +" "+j );
               }
               try
               {
                   tab[i+1][j-1]++;
               }catch{
                   Console.Error.WriteLine("out found for "+i +" "+j );
               }
               try
               {
                   tab[i+1][j]++;
               }catch{
                   Console.Error.WriteLine("out found for "+i +" "+j );
               }
               try
               {
                   tab[i+1][j+1]++;
               }catch{
                   Console.Error.WriteLine("out found for "+i +" "+j );
               }
           }
        }
        for (int i=0;i<h;i++)
        {
            string result="";
            for (int j=0;j<w;j++)
            {
                result+=tab[i][j];
            }
            result=result.Replace('0','.');
            result=result.Replace('9','.');
            Console.WriteLine(result);
        }   
    }
}

Every one can help me please ?

Hi,

You have too many characters because some of your “9” (the bombs) have their value increased by a bomb of the previous line so not only your logic don’t apply to them but they also are no longer just one digit.

ohhh thx, i didn’t think about that

greetings,
i tried to solve the puzzle and i think my solution works in a good way except for a problem with 3rd test case , it works well when i use a 50*50 table but increasing the parameters cause a problem in it (segmentation problem)
if anyone could help me i’ll be grateful ^^

#include <stdio.h>
int w,h;
int i,j ;
char line[50][50],x[150];

int main()
{
    scanf("%d", &w);
    scanf("%d", &h); fgetc(stdin);
    for (i = 0; i < h; i++) {
        scanf("%[^\n]", x); fgetc(stdin);
        for (j = 0; j < w; j++) 
        {
        line[i][j]=x[j];
        }
    }
    for (i = 0; i < h; i++) 
    {
        for (j = 0; j < w; j++) 
        {
            if (line[i][j]=='.')
            {
                line[i][j]='0';
            }
            else
            {
                line[i][j]='.';
            }
        }
    }
    for (i = 0; i < h; i++) 
    {
        for (j = 0; j < w; j++) 
        {
            if (line[i][j]=='.')
            {
                if (line[i-1][j-1] != '.')
                    line[i-1][j-1]+=1;
                if (line[i-1][j] != '.')
                    line[i-1][j]+=1;
                if (line[i-1][j+1] != '.')
                    line[i-1][j+1]+=1;     
                if (line[i][j-1] != '.') 
                    line[i][j-1]+=1;
                if (line[i][j+1] != '.')  
                    line[i][j+1]+=1;
                if (line[i+1][j-1] != '.')  
                    line[i+1][j-1]+=1;
                if (line[i+1][j] != '.') 
                    line[i+1][j]+=1;
                if (line[i+1][j+1] != '.') 
                    line[i+1][j+1]+=1;
            }
        }
    }
    for (i = 0; i < h; i++) 
    {
        for (j = 0; j < w; j++) 
        {
            if (line[i][j]=='0')
            {
                line[i][j]='.';
            }
        }
    }
    for (i = 0; i < h; i++) 
    {
        for (j = 0; j < w; j++) 
        {
            printf("%c",line[i][j]);
        }
        printf("\n");
    }
    return 0;
}


Hi,

Issue is here:

                if (line[i-1][j-1] != '.')
                    line[i-1][j-1]+=1;
                if (line[i-1][j] != '.')
                    line[i-1][j]+=1;
                if (line[i-1][j+1] != '.')
                    line[i-1][j+1]+=1;     

When i is 0 you are accessing & modifying memory outside of the bounds of the array, segmentation problem occurs because you are changing the value of w.

Is there some bug in javascript? I answered the correct matrix (test 1), but it says it wants only the line where are the 111.

And now, it’s worse, I have “attendu: Rien”. But it doesn’t valid if I put console.log(‘Rien’).

Please refer to the default code. It tells you how to output something in the error stream (as the standard stream is for answers only), e.g.
// To debug: console.error('Debug messages...');

And referring to your earlier message, I think it’s more likely that the bug is in your code. Maybe you could clarify which test you fail, and also copy and paste here the full error message(s) shown in the console.

This was interesting and fun to work on, I appreciate the answer and how it can either be really complex or very straightforward. Thank you for making this!

Hello all,

I tried to solve the puzzle, first in the case of 1 mine or none.
My output seems correct, but the tests don’t pass… Is anyone can see what is wrong in my code please ?
Thanks !

import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.


w = int(input())
h = int(input())

entree = []
sortie = []
for i in range(h):
    line = input()
    entree.append(line)

line_num = 0
line = ['.']*w
lineMin1 = ['.']*w
linePl1 = ['.']*w
while line_num < h-2:
    line_position = 0
    z = 0
    """ je decale mes lignes"""
    lineMin1 = line
    line = linePl1
    linePl1 = ['.']*w
    while line_position < w:
        if entree[line_num+1][line_position] == "x":
            if line_position > 0:
                line[line_position - 1] = '1'
                lineMin1[line_position - 1] = '1'
                linePl1[line_position - 1] = '1'
            
            if line_num > 0:
                lineMin1[line_position + 1] = '1'
                lineMin1[line_position] = '1'

            if line_num < h-1:
                if line_position < w-1:
                    linePl1[line_position + 1] = '1'
                linePl1[line_position] = '1' 

            if line_position < w-1:
                line[line_position + 1] = '1'

            line[line_position] = '.'

        line_position = line_position + 1
    line_num = line_num + 1
    sortie.append(lineMin1)

sortie.append(line)
sortie.append(linePl1)


str_sortie = ''

for z in sortie[:]:
    for zz in z[:]:    
        str_sortie = str_sortie + zz
    if z != len(sortie):
        str_sortie = str_sortie + '\n'
    else:
        str_sortie = str_sortie
print(str_sortie)

Please format your code properly using the </> button in the formatting toolbar, so that others are able to review it.

I have found the solution, the last line should not have a /n

It does need an end of line character, but the print function in python is adding one at the end so you would have had two on the last line.

You could also have done (to prevent the print function adding an end of line):
print(str_sortie, end='')

1 Like

The tag says 2D array, but you only ever need to keep two lines in memory.

Well, to be fair, 2 lines makes a 2D array. :wink:

That’s weird. All my tests have passed in C#, but when I submit the code, test 4 fails. Obviusly, I’m not using hard coded solutions. Any idea what’s going on?

The exact same algorithm works fine in C++. All my tests and validators have passed. This means there is a problem in the C# solution.

I suppose it is up to you to make the tests and determine where the C# has issues.

I can give you a clue : the validator 4 is the only one having more lines than columns. Don’t you have a problem defining your lines/columns ?
That’s my guess.

1 Like

Yeap, found the problem. Thanks for the clue.