Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <locale.h>
- #include <fstream>
- using namespace std;
- const int STR_LENGHT = 80;
- bool readLine(ifstream& in, char* buffer)
- {
- buffer[0] = 0;
- in.getline(buffer, STR_LENGHT + 1, '\n');
- if (in.fail())
- {
- if (buffer[0] > 0)
- cout << "Слишком длинная строка во входном файле" << endl;
- return false;
- }
- return true;
- }
- int Size(char* c)
- {
- int n = 0;
- while (*c != 0) {
- if (isdigit(*c)) {
- while (isdigit(*c)) {
- n *= 10;
- n += (*c - '0');
- c++;
- }
- return n;
- }
- }
- return -3;
- }
- int SerchLen(char** arr, int i, int j)
- {
- if (arr[i][j] == '0')
- return 0;
- arr[i][j] = '0';
- return 1 + SerchLen(arr, i, j + 1) + SerchLen(arr, i + 1, j) + SerchLen(arr, i, j - 1) + SerchLen(arr, i - 1, j);
- }
- int main()
- {
- int Size(char* c);
- bool readLine(ifstream& in, char* buffer);
- int SerchLen(char** arr, int i, int j);
- setlocale(LC_ALL, "Russian");
- ifstream in("in.txt");
- if (!in.is_open())
- {
- cout << "Файла in.txt не существует" << endl;
- return -1;
- }
- ofstream out("out.txt");
- if (!out.is_open())
- {
- cout << "Файла out.txt не существует" << endl;
- return -2;
- }
- char buffer[STR_LENGHT + 1];
- int N, M;
- if (readLine(in, buffer)) {
- N = Size(buffer);
- M = Size(buffer);
- }
- char** arr = new char*[N + 2];
- for (int i = 0; i < N + 2; i++)
- arr[i] = new char[M + 2];
- int i = 0, j = 0, max = 0;
- for (j = 0; j < M + 2; j++) {
- arr[0][j] = '0';
- arr[N+1][j] = '0';
- }
- i = 1;
- while (readLine(in, buffer))
- {
- arr[i][0] = '0';
- for (j = 1; j <= M; j++)
- arr[i][j] = buffer[j-1];
- arr[i][M + 1] = '0';
- i++;
- }
- for (i = 1;i < N +1;i++)
- for (j = 1; j < M + 1; j++) {
- int a = SerchLen(arr, i, j);
- if (a > max)
- max = a;
- }
- out << "Ответ: " << max << endl;
- for (i = 0; i < N; i++)
- delete[] arr[i];
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment