Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct RTR {
- int *rotor, *notches, notchno,*back;
- };
- RTR* cpyrtrs(RTR*rotors, int usedrtrs, int *currotors, int absize) {
- RTR*temp;
- temp = (RTR*)malloc(sizeof(RTR)*usedrtrs);
- for (int i = 0; i < usedrtrs; i++) {
- (temp + i)->rotor = (int*)malloc(sizeof(int)*absize);
- (temp + i)->back = (int*)malloc(sizeof(int)*absize);
- for (int j = 0; j < absize; j++)
- (temp + i)->rotor[j] = rotors[currotors[i]].rotor[j];
- for (int j = 0; j < absize; j++)
- (temp + i)->back[j] = rotors[currotors[i]].back[j];
- (temp + i)->notchno = rotors[currotors[i]].notchno;
- (temp + i)->notches = (int*)malloc(sizeof(int)*((temp + i)->notchno));
- for (int j = 0; j < (temp + i)->notchno; j++)
- (temp + i)->notches[j] = rotors[currotors[i]].notches[j];
- }
- return temp;
- }
- bool checknotch(RTR *rotor, int pos) {
- for (int i = 0; i < rotor->notchno; i++)
- if (pos == rotor->notches[i])
- return 1;
- return 0;
- }
- void code(int absize, int usedrtrs, int*rtrpos, RTR *rotors, int *reflector) {
- int chr, *temp, f = 2, *rtrspos;
- bool fstmoved = 0, scdmoved = 0;
- temp = (int*)malloc(sizeof(int));
- rtrspos = (int*)malloc(sizeof(int)*usedrtrs);
- for (int i = 0; i < usedrtrs; i++) {
- rtrspos[i] = rtrpos[i]-1;
- }
- while (1) {
- /*chr = (int)getchar() - 48;
- getchar();*/
- scanf("%d", &chr);
- if (chr == 0)
- break;
- /*for (int i = 0; i < absize; i++)
- printf("%d ", rotors->rotor[i]);
- printf("\n");*/
- /*for (int i = 0; i < absize; i++)
- printf("%d ", rotors->back[i]);
- printf("\n");*/
- if (usedrtrs > 2 && checknotch(rotors + 1, rtrspos[1]+1) && scdmoved == 1) {
- rtrspos[2] = (++rtrspos[2]) % absize;
- if (rtrspos[2] < 0)
- rtrspos[2] += absize;
- }
- rtrspos[0] = (++rtrspos[0]) % absize;
- if (rtrspos[0] < 0)
- rtrspos[0] += absize;
- if (usedrtrs > 1 && checknotch(rotors, rtrspos[0] +1) && fstmoved == 1) {
- rtrspos[1] = (++rtrspos[1]) % absize;
- if (rtrspos[1] < 0)
- rtrspos[1] += absize;
- scdmoved = 1;
- }
- fstmoved = 1;
- for (int i = 0; i < usedrtrs; i++) {
- chr = (chr + rotors[i].rotor[(rtrspos[i] + chr - 1) % absize]) % absize;
- if (chr <= 0)
- chr += absize;
- }
- /*for (int i = 0; i < absize; i++)
- printf("%d ", rotors->rotor[i]);
- printf("\n");*/
- chr = (chr + reflector[chr - 1]) % absize;
- if (chr <= 0)
- chr += absize;
- for (int i = usedrtrs - 1; i >= 0; i--) {
- chr = (chr + rotors[i].back[(rtrspos[i]+chr - 1)%absize]) % absize;
- if (chr <= 0)
- chr += absize;
- }
- temp[f - 2] = chr;
- temp = (int*)realloc(temp, sizeof(int)*f++);
- }
- for (int a = 0; a < f - 2; a++) {
- printf("%d ", *(temp + a));
- }
- printf("\n");
- free(temp);
- free(rtrspos);
- }
- int main()
- {
- int rtrno, absize, rflktrno, **reflectors, msgno, usedrtrs, *currotor, *rtrpos, usedrflktr,temp;
- RTR *rotors;
- scanf("%d", &absize);
- scanf("%d", &rtrno);
- rotors = (RTR*)malloc(sizeof(RTR)*rtrno);
- currotor = (int*)malloc(sizeof(int)*rtrno);
- rtrpos = (int*)malloc(sizeof(int)*absize);
- for (int i = 0; i < rtrno; i++) {
- rotors[i].rotor = (int*)malloc(sizeof(int)*absize);
- rotors[i].back = (int*)malloc(sizeof(int)*absize);
- for (int j = 0; j < absize; j++) {
- scanf("%d", &temp);
- rotors[i].rotor[j] =temp - (j + 1);
- if (rotors[i].rotor[j] < 0)
- rotors[i].rotor[j] += absize;
- rotors[i].back[temp - 1] = j+1 -temp;
- if (rotors[i].back[temp - 1] < 0)
- rotors[i].back[temp - 1] += absize;
- //printf("%d \n",*(rotors[i].rotor + j));
- }
- /*for (int j= 0; j < absize;j++)
- printf("%d ", rotors[i].back[j]);
- printf("\n");*/
- scanf("%d", &(rotors[i].notchno));
- rotors[i].notches = (int*)malloc(sizeof(int)*rotors[i].notchno);
- for (int j = 0; j < rotors[i].notchno; j++) {
- scanf("%d", rotors[i].notches + j);
- //printf("%d", *(rotors[i].notches + j));
- }
- }
- scanf("%d", &rflktrno);
- reflectors = (int**)malloc(sizeof(int*)*rflktrno);
- for (int i = 0; i < rflktrno; i++) {
- *(reflectors + i) = (int*)malloc(sizeof(int)*absize);
- for (int j = 0; j < absize; j++) {
- scanf(" %d", *(reflectors + i) + j);
- *(*(reflectors + i) + j) += -(j + 1);
- //printf("%d ",*(*(reflectors+i) + j));
- }
- }
- scanf("\n%d", &msgno);
- for (int i = 0; i < msgno; i++) {
- scanf("%d", &usedrtrs);
- for (int j = 0; j < usedrtrs; j++) {
- scanf("%d", currotor + j);
- scanf("%d", rtrpos + j);
- }
- scanf("%d\n", &usedrflktr);
- RTR*temp;
- temp = cpyrtrs(rotors, usedrtrs, currotor, absize);
- code(absize, usedrtrs, rtrpos, temp, *(reflectors + usedrflktr));
- free(temp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement