Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- void check(void *a) {
- if (a == NULL)
- exit(1);
- }
- int fill(int x, int y, int n, char **a, char **viz, int *m) {
- int s = 0;
- viz[x][y] = 1;
- int dx[] = {-1, 0, 1, 0};
- int dy[] = {0, 1, 0, -1};
- for (int i = 0; i < 4; ++i) {
- int nx = x + dx[i], ny = y + dy[i];
- if (nx >= 0 && nx < n && ny >= 0 && ny < 4 * m[nx] && a[nx][ny] == 0 && viz[nx][ny] == 0)
- s += fill(nx, ny, n, a, viz, m);
- }
- return s + 1;
- }
- int trans_nr(char ch) {
- if (isdigit((ch))) {
- return ch - '0';
- }
- return ch - 'A' + 10;
- }
- char to_number(int x) {
- if (x < 128) {
- return x;
- }
- return x - 256;
- }
- int length(char ch) {
- if (ch == 'I') {
- return 4;
- } else if (ch == 'S') {
- return 2;
- }
- return 1;
- }
- void trans_hx(char x) {
- if (x < 10) {
- printf("%d", x);
- } else {
- printf("%c", x - 10 + 'A');
- }
- }
- void to_hexa(char x) {
- int cx = x;
- if (x < 0) {
- cx += 256;
- }
- trans_hx(cx / 16);
- trans_hx(cx % 16);
- }
- int main()
- {
- freopen("date.in", "r", stdin);
- int n;
- scanf("%d", &n);
- char **a = (char **)malloc(n * sizeof(char *));
- check(*a);
- int *m = (int*)malloc(n * sizeof(int));
- check(m);
- for (int i = 0; i < n; ++i) {
- scanf("%d ", &m[i]);
- a[i] = (char *)malloc(m[i] * 4 * sizeof(char));
- check(a[i]);
- char x, y;
- for (int k = 0; k < m[i]; ++k) {
- for (int j = 4 * k + 3; j >= 4 * k; --j) {
- scanf("%c%c", &x, &y);
- a[i][j] = to_number((int) trans_nr(x) * 16 + trans_nr(y));
- }
- scanf(" ");
- }
- }
- int s = 0;
- for (int j = 0; j < 4 * m[0]; ++j) {
- s += a[0][j];
- }
- for (int j = 0; j < 4 * m[n - 1]; ++j) {
- s += a[n - 1][j];
- }
- for (int i = 1; i < n - 1; ++i) {
- s += a[i][0];
- s += a[i][m[i] * 4 - 1];
- }
- printf("task 1:\n%.7lf\n", 1.0 * s / (4 * (m[0] + m[n - 1]) + 2 * (n - 2)));
- int q;
- for (scanf("%d\n", &q); q > 0; --q) {
- char op, type;
- int l, c, len;
- scanf("%c %c%d%d ", &op, &type, &l, &c);
- len = length(type);
- c = len * (c - 1);
- if (op == 'M') {
- if (c > 4 * m[l] - 1) {
- int aux = m[l];
- m[l] = c / 4 + 1;
- a[l] = (char *)realloc(a[l], m[l] * 4 * sizeof(char));
- check(a[l]);
- for (int j = 4 * aux; j < 4 * m[l]; ++j) {
- a[l][j] = 0;
- }
- }
- char ch, x, y;
- for (int i = 1; i <= 4 - len; ++i) {
- scanf("%c%c", &ch, &ch);
- }
- for (int j = len - 1; j >= 0; --j) {
- scanf("%c%c", &x, &y);
- a[l][c + j] = trans_nr(x) * 16 + trans_nr(y);
- }
- } else if (op == 'D') {
- for (int j = 0; j < len; ++j) {
- a[l][c + j] = 0;
- }
- } else {
- if (len == 1)
- continue;
- c += len;
- int st = c, dr = c + len - 1;
- while (st <= dr) {
- int aux = a[l][st];
- a[l][st] = a[l][dr];
- a[l][dr] = aux;
- ++st; --dr;
- }
- }
- scanf("\n");
- }
- printf("task 2\n");
- for (int i = 0; i < n; ++i) {
- for (int k = 0; k < m[i]; ++k) {
- for (int j = 4 * k + 3; j >= 4 * k; --j) {
- to_hexa(a[i][j]);
- }
- printf(" ");
- }
- printf("\n");
- }
- char **viz = (char**)malloc(n * sizeof(char*));
- check(*viz);
- for (int i = 0; i < n; ++i) {
- viz[i] = (char*)calloc(m[i], sizeof(char));
- check(viz[i]);
- }
- int ans = 0, mx = 0, my = 0;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < 4 * m[i]; ++j) {
- if(a[i][j] == 0 && viz[i][j] == 0) {
- int res = fill(i, j, n, a, viz, m);
- if (res > ans) {
- ans = res;
- mx = i;
- my = j;
- }
- }
- }
- }
- printf("task 3\n%d %d %d\n", mx, my, ans);
- for (int i = 0; i < n; ++i) {
- free(a[i]);
- free(viz[i]);
- }
- free(m);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement