Guest User

Untitled

a guest
Mar 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <locale.h>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. const int STR_LENGHT = 80;
  8.  
  9. bool readLine(ifstream& in, char* buffer)
  10. {
  11. buffer[0] = 0;
  12. in.getline(buffer, STR_LENGHT + 1, '\n');
  13. if (in.fail())
  14. {
  15. if (buffer[0] > 0)
  16. cout << "Слишком длинная строка во входном файле" << endl;
  17. return false;
  18. }
  19. return true;
  20. }
  21.  
  22. int Size(char* c)
  23. {
  24. int n = 0;
  25. while (*c != 0) {
  26. if (isdigit(*c)) {
  27. while (isdigit(*c)) {
  28. n *= 10;
  29. n += (*c - '0');
  30. c++;
  31. }
  32. return n;
  33. }
  34. }
  35. return -3;
  36. }
  37.  
  38. int SerchLen(char** arr, int i, int j)
  39. {
  40. if (arr[i][j] == '0')
  41. return 0;
  42. arr[i][j] = '0';
  43. return 1 + SerchLen(arr, i, j + 1) + SerchLen(arr, i + 1, j) + SerchLen(arr, i, j - 1) + SerchLen(arr, i - 1, j);
  44. }
  45.  
  46. int main()
  47. {
  48. int Size(char* c);
  49. bool readLine(ifstream& in, char* buffer);
  50. int SerchLen(char** arr, int i, int j);
  51. setlocale(LC_ALL, "Russian");
  52. ifstream in("in.txt");
  53. if (!in.is_open())
  54. {
  55. cout << "Файла in.txt не существует" << endl;
  56. return -1;
  57. }
  58.  
  59. ofstream out("out.txt");
  60. if (!out.is_open())
  61. {
  62. cout << "Файла out.txt не существует" << endl;
  63. return -2;
  64. }
  65.  
  66. char buffer[STR_LENGHT + 1];
  67. int N, M;
  68. if (readLine(in, buffer)) {
  69. N = Size(buffer);
  70. M = Size(buffer);
  71. }
  72. char** arr = new char*[N + 2];
  73. for (int i = 0; i < N + 2; i++)
  74. arr[i] = new char[M + 2];
  75. int i = 0, j = 0, max = 0;
  76. for (j = 0; j < M + 2; j++) {
  77. arr[0][j] = '0';
  78. arr[N+1][j] = '0';
  79. }
  80.  
  81.  
  82. i = 1;
  83. while (readLine(in, buffer))
  84. {
  85. arr[i][0] = '0';
  86. for (j = 1; j <= M; j++)
  87. arr[i][j] = buffer[j-1];
  88. arr[i][M + 1] = '0';
  89. i++;
  90. }
  91.  
  92.  
  93. for (i = 1;i < N +1;i++)
  94. for (j = 1; j < M + 1; j++) {
  95. int a = SerchLen(arr, i, j);
  96. if (a > max)
  97. max = a;
  98. }
  99.  
  100. out << "Ответ: " << max << endl;
  101.  
  102. for (i = 0; i < N; i++)
  103. delete[] arr[i];
  104. system("pause");
  105. return 0;
  106. }
Add Comment
Please, Sign In to add comment