 # [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++)
{
}
//Console.Error.WriteLine(tableau.Contains(9));
//Console.Error.WriteLine(tab[i] );
}
//Console.Error.WriteLine(tab.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,x;

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.