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 . . . |