Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define FREE_FIELD 0
- #define UNBROKEN_FIELD_PART 1
- #define INJURED_FIELD_PART 2
- #define CHECKED_FIELD 3
- #define UNDEFINED_ORIENTATION_SHIP 0
- #define HORIZONTAL_SHIP 1
- #define VERTICAL_SHIP 2
- #define STATUS_UNBROKEN_SHIP 0
- #define STATUS_INJURED_SHIP 1
- #define STATUS_KILLED_SHIP 2
- typedef struct {
- long int x, y;
- int orintation;
- long int len;
- int status;
- } StatusShip;
- StatusShip GetStatus(long int width_field_, long int length_field_, int** array_, long int i_, long int j_) {
- StatusShip StatusShip_;
- StatusShip_.x = j_;
- StatusShip_.y = i_;
- StatusShip_.orintation = UNDEFINED_ORIENTATION_SHIP;
- StatusShip_.len = 1;
- if (array_[i_][j_] == UNBROKEN_FIELD_PART)
- StatusShip_.status = STATUS_UNBROKEN_SHIP;
- if (array_[i_][j_] == INJURED_FIELD_PART)
- StatusShip_.status = STATUS_KILLED_SHIP;
- if (j_ < length_field_) {
- if (array_[i_][j_ + 1] == UNBROKEN_FIELD_PART || array_[i_][j_ + 1] == INJURED_FIELD_PART) {
- if (j_ + 1 >= length_field_)
- printf("ALARM1\n");
- StatusShip_.orintation = HORIZONTAL_SHIP;
- StatusShip_.len = 0;
- int bomb_ = 0; // Переменная для определения статуса корабля
- for (long int k = j_; k <= length_field_; k++) {
- if (array_[i_][k] == UNBROKEN_FIELD_PART || array_[i_][k] == INJURED_FIELD_PART) {
- bomb_ = bomb_ + array_[i_][k];
- }
- else break;
- StatusShip_.len++;
- }
- StatusShip_.status = STATUS_INJURED_SHIP; //статус "ранен"
- if (bomb_ == StatusShip_.len)
- StatusShip_.status = STATUS_UNBROKEN_SHIP; //статус "целый"
- if (bomb_ == StatusShip_.len * 2)
- StatusShip_.status = STATUS_KILLED_SHIP; //статус "убит"
- }
- }
- if (i_ < width_field_) {
- if (i_ + 1 >= width_field_)
- printf("ALARM2\n");
- if (array_[i_ + 1][j_] == UNBROKEN_FIELD_PART || array_[i_ + 1][j_] == INJURED_FIELD_PART) {
- StatusShip_.orintation = VERTICAL_SHIP;
- StatusShip_.len = 0;
- int bomb_ = 0;
- for (long int k = i_; k <= width_field_; k++) {
- if (array_[k][j_] == UNBROKEN_FIELD_PART || array_[k][j_] == INJURED_FIELD_PART) {
- bomb_ = bomb_ + array_[k][j_];
- }
- else break;
- StatusShip_.len++;
- }
- StatusShip_.status = STATUS_INJURED_SHIP;
- if (bomb_ == StatusShip_.len)
- StatusShip_.status = STATUS_UNBROKEN_SHIP;
- if (bomb_ == StatusShip_.len * 2)
- StatusShip_.status = STATUS_KILLED_SHIP;
- }
- }
- return StatusShip_;
- }
- // Проверка, попадает ли коор-та в игровое поле:
- int CheckSea(long int width_field_, long int length_field_, long int i_, long int j_) {
- if (i_ < 0 || j_ < 0 || i_ > width_field_ || j_ > length_field_)
- return 0;
- return 1;
- }
- int main() {
- long int x, y;
- StatusShip CurrentShip;
- int Status_Ship[3] = { 0,0,0 };
- int res;
- if (scanf("%ld", &x) != 1) {
- printf("bad input");
- return 1;
- }
- if (scanf("%ld", &y) != 1) {
- printf("bad input");
- return 1;
- }
- if (((long long int)(x * y)) > pow(2, 31) - 1) {
- printf("out of memory");
- return 2;
- }
- if (x < 0 || y < 0) {
- printf("bad input");
- return 1;
- }
- int** array = (int**)malloc(sizeof(int*) * y);
- if (array == NULL) {
- printf("out of memory");
- return 2;
- }
- for (long int i = 0; i < y; i++) {
- array[i] = (int*)malloc(sizeof(int) * x);
- if (array[i] == NULL) {
- printf("out of memory");
- for (long int i_ = 0; i_ < i - 1; i_++)
- free(array[i_]);
- free(array);
- return 2;
- }
- }
- for (long int i = 0; i < y; i++) {
- for (long int j = 0; j < x; j++) {
- res = scanf("%d", &array[i][j]);
- if (res == EOF || res == 0) {
- printf("bad input");
- for (long int i_ = 0; i_ < y; i_++)
- free(array[i_]);
- free(array);
- return 1;
- }
- if (!(array[i][j] == 0 || array[i][j] == 1 || array[i][j] == 2)) {
- printf("bad input");
- for (long int i_ = 0; i_ < y; i_++)
- free(array[i_]);
- free(array);
- return 1;
- }
- }
- }
- for (long int i = 0; i < y; i++) {
- for (long int j = 0; j < x; j++) {
- if (array[i][j] == FREE_FIELD || array[i][j] == CHECKED_FIELD)
- continue;
- if (array[i][j] == UNBROKEN_FIELD_PART || array[i][j] == INJURED_FIELD_PART) {
- CurrentShip = GetStatus(y - 1, x - 1, array, i, j);
- if (CurrentShip.orintation == UNDEFINED_ORIENTATION_SHIP || CurrentShip.orintation == HORIZONTAL_SHIP) {
- for (long int k = CurrentShip.x - 1; k <= CurrentShip.x + CurrentShip.len; k++) {
- if (CheckSea(y - 1, x - 1, i - 1, k) == 1)
- array[i - 1][k] = CHECKED_FIELD;
- if (CheckSea(y - 1, x - 1, i, k) == 1)
- array[i][k] = CHECKED_FIELD;
- if (CheckSea(y - 1, x - 1, i + 1, k) == 1)
- array[i + 1][k] = CHECKED_FIELD;
- }
- }
- if (CurrentShip.orintation == VERTICAL_SHIP) {
- for (long int k = CurrentShip.y - 1; k <= CurrentShip.y + CurrentShip.len; k++) {
- if (CheckSea(y - 1, x - 1, k, j - 1) == 1)
- array[k][j - 1] = CHECKED_FIELD;
- if (CheckSea(y - 1, x - 1, k, j) == 1)
- array[k][j] = CHECKED_FIELD;
- if (CheckSea(y - 1, x - 1, k, j + 1) == 1)
- array[k][j + 1] = CHECKED_FIELD;
- }
- }
- Status_Ship[CurrentShip.status]++;
- }
- }
- }
- printf("%d %d %d", Status_Ship[0], Status_Ship[1], Status_Ship[2]);
- for (long int i = 0; i < y; i++)
- free(array[i]);
- free(array);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement