/////////////////////////////////////////////////////////////////////////////////////////
//На клеточном листе бумаги размером MхN расположены прямоугольники.
//Задан массив MхN в котором элемент a[i,j]=1 если клетка листа (i,j)
//яв-ся частью прямоугольника, и a[i,j]=0 если это пустая клетка.
//Напечатать число прямоугольников.
/////////////////////////////////////////////////////////////////////////////////////////
//В моей реализации нули и единицы в исходный файл записывать без пробелов.
/////////////////////////////////////////////////////////////////////////////////////////
#include
#include
#include
#include
#include
#include
/////////////////////////////////////////////////////////////////////////////////////////
typedef std::string T_str;
typedef std::vector T_strings;
/////////////////////////////////////////////////////////////////////////////////////////
bool is_left_upper_corner
(
const T_strings& strings,
int i,
int j
)
{
return strings[j] == '1'
&& (
i == 0
|| strings[i - 1][j] == '0'
)
&& (
j == 0
|| strings[j - 1] == '0'
);
}
/////////////////////////////////////////////////////////////////////////////////////////
void count_rectangles
(
const T_str& ifilename,
const T_str& ofilename
)
{
std::ifstream ifile( ifilename.c_str() );
if( !ifile )
{
std::cout << "Невозможно открыть файл \""
<< ifilename
<< "\"."
<< std::endl;
}
T_strings strings;
std::copy
(
std::istream_iterator (ifile),
std::istream_iterator (),
std::back_inserter (strings)
);
int counter = 0;
for(int i = 0; i < strings.size(); ++i)
{
for(int j = 0; j < strings.size(); ++j)
{
if( is_left_upper_corner(strings, i, j) )
{
++counter;
}
}
}
std::ofstream ofile( ofilename.c_str() );
ofile << counter;
}
/////////////////////////////////////////////////////////////////////////////////////////
int main()
{
std::locale::global(std::locale(""));
const T_str ifilename = "if.txt";
const T_str ofilename = "of.txt";
count_rectangles(ifilename, ofilename);
}