### Please, check, where do i have a mistake?

Area of the room
Your task is to write a program that will find the area of room in a given square maze.

Note. Use recursion for solving this problem.
Input:
First line contains only one number N (3 <= N <= 10).The number that describes the size of the square maze.
On the next N lines maze itself is inputed ('.' - empty cell,'*' - wall).
And the last line contains two numbers - the row and column of the cell in the room those area you need to find.It is guaranteed that given cell is empty and that the maze is surrounded by walls on all sides.
Output:
Output only one number - total amount of empty cells in the selected room.
Input
5
*****
**..*
*.*.*
*..**
*****
2 4
Output 3

#include <iostream>
#include <cstdio>
using namespace std;
char a[12][12];
int sum=1, l1, l2, l3, l4;
void rec(int r, int c)
{
int r1=r;
int c1=c;
if (a[r+1][c]=='.')
{
sum++;
a[r+1][c]='*';
r=r+1;
rec(r, c);
}
if (a[r-1][c]=='.')
{
sum++;
a[r-1][c]='*';
r=r-1;
rec(r, c);
}
if (a[r][c+1]=='.')
{
sum++;
a[r][c+1]=='*';
c=c+1;
rec(r, c);
}
if (a[r][c+1]=='.')
{
sum++;
a[r][c+1]='*';
c=c+1;
rec(r, c);
}
cout << sum;
}

int main()
{
freopen("1.txt", "r", stdin);
int n;
cin >> n;
int row, col;
for (int i=1; i <= n; i++)
for (int j=1; j <= n; j++)
cin >> a[i][j];
cin >> row, col;

if (a[row][col] != '.') cout << "0";
else rec(row, col);
return 0;
}
Last edited on
Where in the question does it state that you read the input from a file?
Last edited on
in 1.txt:
5
*****
**..*
*.*.*
*..**
*****
2 4
That is merely showing an example of the user input for the program.
Last edited on
I use freopen that don't write again this example
You don't need to use a file, you haven't been asked to. The assignment states that the user enters the following at the program prompt:

5
*****
**..*
*.*.*
*..**
*****
2 4

and the program calculates the total number of empty cells in the room within the maze and outputs it to the screen:

3

NO FILE is required!
i understand it, but after compiling i check, and i write again again, if it works true then i just remove it (freopen).
ok.
#include <iostream>

using namespace std;
char a[12][12];
int sum=1, l1, l2, l3, l4;
void rec(int r, int c)
{
int r1=r;
int c1=c;
if (a[r+1][c]=='.')
{
sum++;
a[r+1][c]='*';
r=r+1;
rec(r, c);
}
if (a[r-1][c]=='.')
{
sum++;
a[r-1][c]='*';
r=r-1;
rec(r, c);
}
if (a[r][c+1]=='.')
{
sum++;
a[r][c+1]=='*';
c=c+1;
rec(r, c);
}
if (a[r][c+1]=='.')
{
sum++;
a[r][c+1]='*';
c=c+1;
rec(r, c);
}
cout << sum;
}

int main()
{

int n;
cin >> n;
int row, col;
for (int i=1; i <= n; i++)
for (int j=1; j <= n; j++)
cin >> a[i][j];
cin >> row, col;

if (a[row][col] != '.') cout << "0";
else rec(row, col);
return 0;
}
so can you help me to find where i have a mistake
The first pre-requisite is that the number N must be >= 3 and <= 10. Your code isn't checking for this! I could enter -100
Is this any help:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105`` ``````#include #include using namespace std; class Row { public: Row(const int size) : data(string(' ', size)), checked(false) { } string data; bool checked; }; int get_empty_cell_count(vector& v, int r, int c) { int count(0); if (r < 0 || c < 0 || v[r].checked) return count; if (v[r].data[c] == '.') { v[r].checked = true; ++count; if (r != 0) // check previous row count += get_empty_cell_count(v, r-1, c); if (r != v.size()) // check next row count += get_empty_cell_count(v, r+1, c); // check rest of this row // check to left: int col = c; while (c && v[r].data[--col] == '.') ++count; // reset to c and check to the right: col = c; while (c < v[r].data.length() && v[r].data[++col] == '.') ++count; } return count; } int main() { int maze_size; vector maze; // get size of maze: do { cout << "Enter the size of the maze, in the range 3 : 10 "; cin >> maze_size; cin.ignore(); } while (maze_size < 3 || maze_size > 10); // get maze: cout << "Enter lines for maze ('.' - empty cell, '*' - wall)" << endl; for (int n = 0; n < maze_size; ++n) { Row r(maze_size); cout << "Line #" << n <<": "; cin.getline(&r.data[0], '\n'); r.data = r.data.substr(0, maze_size); maze.push_back(r); } // get row and column from user: int row(0), col(0); cout << "Enter row and column, in the range 1 : " << maze_size << endl; do { int r, c; cin >> r >> c; if ((r > 0 && r < maze_size) && (c > 0 && c < maze_size)) { row = r; col = c; } else { cout << "Invalid, try again" << endl; } } while (row == 0 && col == 0); // find number of empty cells adjacent to and including row, col: cout << get_empty_cell_count(maze, row-1, col-1); cout << endl << endl << "Press [Enter] exit"; cin.ignore(); cin.get(); return 0; }``````
thank you so much:)
have you checked? i put ex. but it outputs "0" :(
 have you checked? i put ex. but it outputs "0" :(

Yes, I checked it against your example input and some other input and it worked for me.

It's probably not 100% robust but probably good enough for your tutor, what code to you finally come up with to solve the problem?
Last edited on
You enter row and column on same line, as the question stated:

2 4

With a space between numbers for row and column. Ok?

You should have been able to work that out from the source code. You will need to make sure you understand what all the code is doing.
Last edited on
Topic archived. No new replies allowed.