Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct RTR {
  7. int *rotor, *notches, notchno,*back;
  8.  
  9. };
  10.  
  11. RTR* cpyrtrs(RTR*rotors, int usedrtrs, int *currotors, int absize) {
  12. RTR*temp;
  13. temp = (RTR*)malloc(sizeof(RTR)*usedrtrs);
  14. for (int i = 0; i < usedrtrs; i++) {
  15. (temp + i)->rotor = (int*)malloc(sizeof(int)*absize);
  16. (temp + i)->back = (int*)malloc(sizeof(int)*absize);
  17. for (int j = 0; j < absize; j++)
  18. (temp + i)->rotor[j] = rotors[currotors[i]].rotor[j];
  19. for (int j = 0; j < absize; j++)
  20. (temp + i)->back[j] = rotors[currotors[i]].back[j];
  21. (temp + i)->notchno = rotors[currotors[i]].notchno;
  22. (temp + i)->notches = (int*)malloc(sizeof(int)*((temp + i)->notchno));
  23. for (int j = 0; j < (temp + i)->notchno; j++)
  24. (temp + i)->notches[j] = rotors[currotors[i]].notches[j];
  25. }
  26. return temp;
  27. }
  28.  
  29. bool checknotch(RTR *rotor, int pos) {
  30. for (int i = 0; i < rotor->notchno; i++)
  31. if (pos == rotor->notches[i])
  32. return 1;
  33. return 0;
  34. }
  35.  
  36. void code(int absize, int usedrtrs, int*rtrpos, RTR *rotors, int *reflector) {
  37. int chr, *temp, f = 2, *rtrspos;
  38. bool fstmoved = 0, scdmoved = 0;
  39. temp = (int*)malloc(sizeof(int));
  40. rtrspos = (int*)malloc(sizeof(int)*usedrtrs);
  41. for (int i = 0; i < usedrtrs; i++) {
  42. rtrspos[i] = rtrpos[i]-1;
  43. }
  44. while (1) {
  45.  
  46. /*chr = (int)getchar() - 48;
  47. getchar();*/
  48. scanf("%d", &chr);
  49. if (chr == 0)
  50. break;
  51. /*for (int i = 0; i < absize; i++)
  52. printf("%d ", rotors->rotor[i]);
  53. printf("\n");*/
  54. /*for (int i = 0; i < absize; i++)
  55. printf("%d ", rotors->back[i]);
  56. printf("\n");*/
  57. if (usedrtrs > 2 && checknotch(rotors + 1, rtrspos[1]+1) && scdmoved == 1) {
  58. rtrspos[2] = (++rtrspos[2]) % absize;
  59. if (rtrspos[2] < 0)
  60. rtrspos[2] += absize;
  61. }
  62. rtrspos[0] = (++rtrspos[0]) % absize;
  63. if (rtrspos[0] < 0)
  64. rtrspos[0] += absize;
  65. if (usedrtrs > 1 && checknotch(rotors, rtrspos[0] +1) && fstmoved == 1) {
  66. rtrspos[1] = (++rtrspos[1]) % absize;
  67. if (rtrspos[1] < 0)
  68. rtrspos[1] += absize;
  69. scdmoved = 1;
  70. }
  71.  
  72. fstmoved = 1;
  73.  
  74.  
  75. for (int i = 0; i < usedrtrs; i++) {
  76. chr = (chr + rotors[i].rotor[(rtrspos[i] + chr - 1) % absize]) % absize;
  77. if (chr <= 0)
  78. chr += absize;
  79. }
  80.  
  81. /*for (int i = 0; i < absize; i++)
  82. printf("%d ", rotors->rotor[i]);
  83. printf("\n");*/
  84.  
  85. chr = (chr + reflector[chr - 1]) % absize;
  86. if (chr <= 0)
  87. chr += absize;
  88.  
  89. for (int i = usedrtrs - 1; i >= 0; i--) {
  90. chr = (chr + rotors[i].back[(rtrspos[i]+chr - 1)%absize]) % absize;
  91. if (chr <= 0)
  92. chr += absize;
  93. }
  94.  
  95. temp[f - 2] = chr;
  96. temp = (int*)realloc(temp, sizeof(int)*f++);
  97. }
  98.  
  99. for (int a = 0; a < f - 2; a++) {
  100. printf("%d ", *(temp + a));
  101. }
  102.  
  103. printf("\n");
  104. free(temp);
  105. free(rtrspos);
  106. }
  107.  
  108. int main()
  109. {
  110. int rtrno, absize, rflktrno, **reflectors, msgno, usedrtrs, *currotor, *rtrpos, usedrflktr,temp;
  111. RTR *rotors;
  112. scanf("%d", &absize);
  113. scanf("%d", &rtrno);
  114. rotors = (RTR*)malloc(sizeof(RTR)*rtrno);
  115. currotor = (int*)malloc(sizeof(int)*rtrno);
  116. rtrpos = (int*)malloc(sizeof(int)*absize);
  117. for (int i = 0; i < rtrno; i++) {
  118. rotors[i].rotor = (int*)malloc(sizeof(int)*absize);
  119. rotors[i].back = (int*)malloc(sizeof(int)*absize);
  120. for (int j = 0; j < absize; j++) {
  121. scanf("%d", &temp);
  122. rotors[i].rotor[j] =temp - (j + 1);
  123. if (rotors[i].rotor[j] < 0)
  124. rotors[i].rotor[j] += absize;
  125. rotors[i].back[temp - 1] = j+1 -temp;
  126. if (rotors[i].back[temp - 1] < 0)
  127. rotors[i].back[temp - 1] += absize;
  128. //printf("%d \n",*(rotors[i].rotor + j));
  129. }
  130. /*for (int j= 0; j < absize;j++)
  131. printf("%d ", rotors[i].back[j]);
  132. printf("\n");*/
  133. scanf("%d", &(rotors[i].notchno));
  134. rotors[i].notches = (int*)malloc(sizeof(int)*rotors[i].notchno);
  135. for (int j = 0; j < rotors[i].notchno; j++) {
  136. scanf("%d", rotors[i].notches + j);
  137. //printf("%d", *(rotors[i].notches + j));
  138. }
  139. }
  140. scanf("%d", &rflktrno);
  141. reflectors = (int**)malloc(sizeof(int*)*rflktrno);
  142. for (int i = 0; i < rflktrno; i++) {
  143. *(reflectors + i) = (int*)malloc(sizeof(int)*absize);
  144. for (int j = 0; j < absize; j++) {
  145. scanf(" %d", *(reflectors + i) + j);
  146. *(*(reflectors + i) + j) += -(j + 1);
  147. //printf("%d ",*(*(reflectors+i) + j));
  148. }
  149. }
  150. scanf("\n%d", &msgno);
  151. for (int i = 0; i < msgno; i++) {
  152. scanf("%d", &usedrtrs);
  153. for (int j = 0; j < usedrtrs; j++) {
  154. scanf("%d", currotor + j);
  155. scanf("%d", rtrpos + j);
  156. }
  157. scanf("%d\n", &usedrflktr);
  158. RTR*temp;
  159. temp = cpyrtrs(rotors, usedrtrs, currotor, absize);
  160. code(absize, usedrtrs, rtrpos, temp, *(reflectors + usedrflktr));
  161. free(temp);
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement