Floor Plan Program (reading array to vector)

Hi guys,

I have some homework and I needed some help finishing off the program.
I got a good amount done but I am experiencing some issues.

**I know there are easier ways to do this but my assignment asks for the use of vectors which is pretty ridiculous.

Here is the question.

The floor plan of a house shows rooms separated by walls. This floor plan can be transferred to a grid using the character “I” for walls and “.” for room space. Doorways are not shown. Each “I” or “.” character occupies one square metre.
You have been given the floor plan of a house and a supply of hardwood flooring. You are to determine how many rooms will have the flooring installed if you start installing the floor in the largest room first and move to the next largest room, and so on. You may not skip over any room, and you must stop when you do not have enough wood for the next room. Output the number of rooms that can have hardwood installed, and how many square metres of flooring are left over.
No room will be larger than 64 square metres.
The first line of the data file contains the number of square metres of flooring you have. The second line in the file contains an integer r from 1 – 25 that represents the number of rows in the grid. The third line contains an integer c from 1 – 25 that represents the number of columns in the grid. The remaining r lines contain c characters of grid data.

and these are some sample inputs.
Input:
125
14
16
IIIIIIIIIIIIIIII
I......I.......I
I......III.....I
I........I.....I
I........IIIIIII
IIIIIIIIII.....I
I.I......I.....I
III..III.I.....I
I....I.IIIII...I
I....I.....III.I
I....I.......I.I
I....I.....III.I
I....I.....I...I
IIIIIIIIIIIIIIII
Output:
6 rooms, 3 square metre(s) left over

Basically my question is how can I get the grid into a vector and then assign it to my char a[26][26] variable?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <fstream>
#include <algorithm> // used for sort
#include <vector>

using namespace std;
char a[26][26];
int go(char board[26][26], int x, int y)
{
    int count=0;
        count = count + 1;
        a[x][y]='I';
        if (a[x+1][y]=='.')
            count = count + go(a, x+1, y);
        if (a[x-1][y]=='.')
            count = count + go(a, x-1, y);
        if (a[x][y+1]=='.')
            count = count + go(a, x, y+1);
        if (a[x][y-1]=='.')
            count = count + go(a, x, y-1);
        return count;
}

vector <int> floorpannelVL;
vector <string> floorpannelgrid;


int main()
{
    string filename= "floor.txt";
    ifstream InFile("floor.txt"); // just a text file
    if (InFile.fail())
    {
        cout << "File: "<< filename << " not found"<<endl;
    }
    else
    {
        string line;
        int value;
        cout<<"Contents of text file: "<<endl;


 while(InFile>>value)
            {
                    floorpannelVL.push_back(value);
            }
      //  cout<<value;

    }

      //  cout<<value;


        int room[100];
        int tiles, r, c, i, j, num=0, filled=0;
        //InFile.clear();
        //InFile.seekg (0, InFile.beg);
       for (int i=0; i<1; i++)

    {

        //cout << floorpannel[i] << endl;

        tiles=floorpannelVL[i];

    }

    cout<<"tiles: "<<tiles<<endl;
    for (int i=1; i<2; i++)
    {
       // cout << floorpannel[i] << endl;
        r=floorpannelVL[i];
    }
    cout<<"r: "<<r<<endl;

    for (int i=2; i<3; i++)
{
       // cout << floorpannel[i] << endl;

        c=floorpannelVL[i];

    }

    cout<<"c: "<<c<<endl;


        for (i=0; i<r; i++)
        {
            //cout<<r;
            for (j=0; j<c; j++)
                InFile >> a[i][j];
                //cout<<a[i][j];
        }
        for (i=0; i<r; i++)
            for (j=0; j<c; j++)
                if (a[i][j] == '.')
                {
                    room[num] = go(a, i, j);
                    num++;
                }
//cout<<a[i][j]<<endl;
        sort(room, room+num); //sorts array room (first,last)

        //for (i=num-1; i>=0; i--)
        //cout << room[i] << endl;

        for (i=num-1; i>=0; i--)
            if (tiles >= room[i])
            {
                tiles = tiles - room[i];
                filled++;
            }
            else
                break;

        cout << filled << " room";
        if (filled != 1)
            cout << 's';
        cout << ", " << tiles << " square metre(s) left over";
//go(a,x,y);
        return 0;
    }



Thanks in advance,
enesdl.
Last edited on
I'd recommend stepping through it with a debugger, so you can see exactly where it's going wrong, and examine the contents of the memory.

Also, I'd recommend printing some text to the screen, prompting the user to enter data, rather than just going straight to the input.
Updated this post can anyone suggest how I can fix this?
**I know there are easier ways to do this but my assignment asks for the use of vectors which is pretty ridiculous.

Why is using vectors ridiculous? You should prefer vectors over raw arrays in most circumstances.

Basically my question is how can I get the grid into a vector and then assign it to my char a[26][26] variable?

Why do you want to assign your vector to an array? Why not just use the vector? Why the global variables for those vectors?

By the way I still don't see where you're actually reading the file correctly.
Line 5: You're missing #include <string>

Line 32: If the open fails, you output a message, then continue on your merry way as if you had read the file. You should have a return 1; after line 34 and the else is unnecessary.

Lines 43-45: What's the point of reading into a vector? Just read into the variables directly.

Line 58,69,76: Why do you have for loops here? You're trying to get the first three numbers out of the vector. You can do that with a simple subscript. Although as noted earlier, it would have been simpler just to have read those values directly into the desired variables.

Lines 8-22: Your algorithm has no protection for out of bounds references.

my question is how can I get the grid into a vector and then assign it to my char a[26][26] variable?

Why do you want it in a vector? That loses the 2D-ness of the data. Read the data directly, which you appear to be doing at line 91.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <fstream>
#include <algorithm> // used for sort
#include <string>
using namespace std;

bool empty (char a[26][26], int r, int c, int rows, int cols) 
{   if (r < 0 || r >= rows) 
        return false;
    if (c < 0 || c >= cols)
        return false;
    return a[r][c] == '.';
}
            
int go(char a[26][26], int r, int c, int rows, int cols)
{   int count = 1;

    a[r][c]='I';
    if (empty (a, r+1,c,rows,cols))
        count += go(a, r+1, c, rows, cols);
    if (empty (a,r-1,c,rows,cols)) 
        count += go(a, r-1, c, rows,cols);
    if (empty (a,r,c+1,rows,cols))
        count += go(a, r, c+1, rows, cols);
    if (empty (a,r,c-1,rows,cols)) 
        count += go(a, r, c-1,rows,cols);
    return count;
}

int main()
{   int room[100];
    int tiles, rows, cols, num=0, filled=0;
    ifstream InFile("floor.txt"); // just a text file
    char a[26][26];
        
    if (InFile.fail())
    {   cout << "floor.txt not found"<<endl;
        return 1;
    }
    
    InFile >> tiles >> rows >> cols;
    cout << "tiles: "<< tiles << endl;
    cout << "Rows: " << rows << endl;
    cout << "Cols: " << cols << endl;
    for (int r=0; r<rows; r++)
    {   for (int c=0; c<cols; c++)
            InFile >> a[r][c];
    }
    for (int r=0; r<rows; r++)
        for (int c=0; c<cols; c++)
            if (a[r][c] == '.')
            {   room[num] = go(a, r, c, rows, cols);
                num++;
            }
    sort(room, room+num); //sorts array room (first,last)

    for (int i=num-1; i>=0; i--)
        if (tiles >= room[i])
        {   tiles = tiles - room[i];
            filled++;
        }
        else
            break;
    cout << filled << " room";
    if (filled != 1)
        cout << 's';
    cout << ", " << tiles << " square metre(s) left over" << endl;
    system ("pause");
    return 0;
}
tiles: 125
Rows: 14
Cols: 16
6 rooms, 3 square metre(s) left over
Press any key to continue . . .


edit: I missed the part where you said the assignment said to use vectors. Would using a vector for rooms meet the requirement?



Last edited on
When I first made the program I had it like that. I had InFile for all the variables and it worked perfectly but upon further inspection of the assignment it said to use vectors. So I was able to get the tiles,rows,cols into a vector but I dont know how to get an array into a vector.

Also Jib to clarify i made an error in my question. I meant how can i assign the array to the vector not the other way around sorry about that!
Last edited on
If you want a 2D vector, then you'll need a vector of vectors:

std::vector< std::vector<char> > a;

a is a vector. Each element of a is itself a vector, and represents a row.
Last edited on
I am still not understanding how to add the elements of the array to the 2D vector. I understood the 2D vector.
You add elements to a vector using the push_back() method:

1
2
3
4
5
6
7
8
9
10
std::vector< std::vector<char> > a;
for (int r = 0; r < numRows; ++r)
{
  std::vector<char> row;
  for (int c = 0; c < numColumns; ++c)
  {
    row.push_back(some_value);
  }
  a.push_back(row);
}


Alternatively, if you know the vectors will be a fixed size, it's easier to set the sizes up to start with. which creates the elements, and then just access elements of the vector using the same syntax as for an array:

1
2
3
4
5
6
7
8
9
10
std::vector< std::vector<char> > a;
a.resize(numRows);
for (int r = 0; r < numRows; ++r)
{
  a[r].resize(numCols);
  for (int c = 0; c < numColumns; ++c)
  {
    a[r][c] = some_value;
  }
}


Last edited on
Show what you've tried.

Basically you read the "line" character by character into a vector<char> then insert that vector into your vector<vector<char>>.

Here's a version using a vector of vectors as MikeyBoy suggested.
Also uses a vector for rooms. No need for a 26x26 array at all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <algorithm> // used for sort
#include <string>
#include <vector>
using namespace std;

typedef vector<char>    Row;    //  A single row
typedef vector<Row>     Floor;  //  vector of Rows (vectors)

bool empty (Floor & a, int r, int c) 
{   if (r < 0 || r >= (int)a.size()) 
        return false;    // Out of bounds
    if (c < 0 || c >= (int)a[r].size())
        return false;   // Out of bounds
    return a[r][c] == '.';
}
            
int go(Floor & a, int r, int c)
{   int count = 1;

    a[r][c]='I';
    if (empty (a, r+1,c))
        count += go(a, r+1, c);
    if (empty (a,r-1,c)) 
        count += go(a, r-1, c);
    if (empty (a,r,c+1))
        count += go(a, r, c+1);
    if (empty (a,r,c-1)) 
        count += go(a, r, c-1);
    return count;
}

int main()
{   vector<int> rooms; 
    int tiles, rows, cols, filled=0;
    ifstream InFile("floor.txt"); // just a text file
    Floor   a;      //  vector of vectors
        
    if (InFile.fail())
    {   cout << "floor.txt not found"<<endl;
        return 1;
    }
    
    InFile >> tiles >> rows >> cols;  // No reason to put these in a vector
    cout << "tiles: "<< tiles << endl;
    cout << "Rows: " << rows << endl;
    cout << "Cols: " << cols << endl;
    for (int r=0; r<rows; r++)
    {   Row     row;
        for (int c=0; c<cols; c++)
        {   char    cell;
            InFile >> cell;
            row.push_back (cell); 
        }
        a.push_back (row);
    }
    for (int r=0; r<rows; r++)
        for (int c=0; c<cols; c++)
            if (a[r][c] == '.')
            {   rooms.push_back (go(a, r, c));
            }
    sort(rooms.begin(), rooms.end()); //sorts array room (first,last)

    for (int i=rooms.size()-1; i>=0; i--)
        if (tiles >= rooms[i])
        {   tiles = tiles - rooms[i];
            filled++;
        }
        else
            break;
    cout << filled << " room";
    if (filled != 1)
        cout << 's';
    cout << ", " << tiles << " square metre(s) left over" << endl;
    system ("pause");
    return 0;
}


Last edited on
Topic archived. No new replies allowed.