fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5. void wypisz_macierz(vector<vector<char>>& macierz, int N) {
  6. for(int i = 0; i < N; i++) {
  7. for(int j = 0; j < N; j++) {
  8. cout << macierz[i][j];
  9. }
  10. cout << endl;
  11. }
  12. }
  13.  
  14. void obrot_w_prawo_270(vector<vector<char>>& matrix, int N) {
  15. //int temp[N][N];
  16. vector<vector<char>> temp(N, vector<char>(N, 0));
  17. for(int i = 0; i < N; i++) {
  18. for(int j = 0; j < N; j++) {
  19. temp[N-j-1][i] = matrix[i][j];
  20. }
  21. }
  22. for(int i = 0; i < N; i++) {
  23. for(int j = 0; j < N; j++) {
  24. matrix[i][j] = temp[i][j];
  25. }
  26. }
  27. }
  28. //obrot w prawo 0 270
  29. // 0,0 x=N-1 y=0 , 0,1 x=N-2, y=0
  30.  
  31. void obrot_w_lewo_270(vector<vector<char>>& matrix, int N) {
  32. vector<vector<char>> temp(N, vector<char>(N, 0));
  33. for(int i = 0; i < N; i++) {
  34. for(int j = 0; j < N; j++) {
  35. temp[j][N-i-1] = matrix[i][j];
  36. }
  37. }
  38. for(int i = 0; i < N; i++) {
  39. for(int j = 0; j < N; j++) {
  40. matrix[i][j] = temp[i][j];
  41. }
  42. }
  43. }
  44. //obrot w lewo 270
  45. //0,0- x=0 y=N-1 0,1 x=1 y=N-1
  46. void obracanieMacierzy(int n, char** macierz) {
  47. for (int i = 0; i < n / 2; i++) {
  48. for (int j = 0; j < n; j++) {
  49. int temp = macierz[i][j];
  50. macierz[i][j] = macierz[n - i - 1][n - j - 1];
  51. macierz[n - i - 1][n - j - 1] = temp;
  52. }
  53. }
  54. if (n % 2 != 0) {
  55. for (int j = 0; j < n / 2; j++) {
  56. int temp = macierz[n / 2][j];
  57. macierz[n / 2][j] = macierz[n / 2][n - j - 1];
  58. macierz[n / 2][n - j - 1] = temp;
  59. }
  60. }
  61. }
  62. //obrot 90
  63. //0,0- x=N-1 y=N-1 0,1 x=N-1 y=N-2
  64. void rotate180(vector<vector<char>>& matrix, int N) {
  65. for (int i = 0; i < N/2; i++) {
  66. for (int j = 0; j < N; j++) {
  67. swap(matrix[i][j], matrix[N-i-1][N-j-1]);
  68. }
  69. }
  70. if (N%2 == 1) {
  71. int mid = N/2;
  72. for (int j = 0; j < mid; j++) {
  73. swap(matrix[mid][j], matrix[mid][N-j-1]);
  74. }
  75. }
  76. }
  77. //obrot 180
  78. //0,0- x=N-1 y=N-1 0,1 x=N-1 y=N-2
  79. // Funkcja obraca macierz o 180 stopni w lewo
  80. void rotate180_left(vector<vector<char>>& matrix,int N) {
  81. for (int i = 0; i < N/2; i++) {
  82. for (int j = 0; j < N; j++) {
  83. swap(matrix[i][j], matrix[N-i-1][N-j-1]);
  84. }
  85. }
  86. for (int i = 0; i < N; i++) {
  87. for (int j = 0; j < N/2; j++) {
  88. swap(matrix[i][j], matrix[i][N-j-1]);
  89. }
  90. }
  91. for (int i = 0; i < N/2; i++) {
  92. for (int j = i+1; j < N; j++) {
  93. swap(matrix[i][j], matrix[j][i]);
  94. }
  95. }
  96. }
  97.  
  98. // Funkcja wykonuje transpozycjê macierzy wzglêdem g³ównej przek¹tnej
  99. void transpose_main(vector<vector<char>>& matrix,int N) {
  100.  
  101. for (int i = 0; i < N; i++) {
  102. for (int j = i+1; j < N; j++) {
  103. swap(matrix[i][j], matrix[j][i]);
  104. }
  105. }
  106. }
  107. //transpose _main
  108. // 0,0 x=0 y,0 0,1 x=1, y=0;
  109. // Funkcja wykonuje transpozycjê macierzy wzglêdem drugiej przek¹tnej
  110. void transpose_secondary(vector<vector<char>>& matrix,int N) {
  111. vector<vector<char>> temp(N, vector<char>(N));
  112. for(int i = 0;i < N;i++){
  113. for (int j = 0; j < N; j++) {
  114. // cout<<N-j-1<<" "<<N-i-1<<endl;
  115. temp[N-j-1][N-i-1]=matrix[i][j];
  116. }
  117. }
  118. matrix=temp;
  119. }
  120. //transpose secondary
  121. //0,0 x=N-1, y=N-1, 0,1 x=N-2, y=N-1
  122. // funkcja do odbicia poziomego macierzy
  123. void flipVertical(vector<vector<char>>& matrix,int N) {
  124.  
  125. for (int i = 0; i < N/2; i++) {
  126. for (int j = 0; j < N; j++) {
  127. swap(matrix[i][j], matrix[N-i-1][j]);
  128. }
  129. }
  130. }
  131. //odbicie poziome
  132. //0,0 x=N-1, y=0; 0,1 x=N-1, y=1
  133. // funkcja do odbicia pionowego macierzy
  134. void flipHorizontal(vector<vector<char>>& matrix,int N) {
  135.  
  136. for (int i = 0; i < N; i++) {
  137. for (int j = 0; j < N/2; j++) {
  138. swap(matrix[i][j], matrix[i][N-j-1]);
  139. }
  140. }
  141. }
  142. //odbicie poziome
  143. // 0,0 x=0, y=N-1, 0,1 x=0, y=N-2
  144. void rotateRight(vector<vector<char>>& matrix, int N) {
  145. // utworzenie nowej macierzy z obróconymi elementami
  146. vector<vector<char>> rotatedMatrix(N, vector<char>(N));
  147. for (int i = 0; i < N; i++) {
  148. for (int j = 0; j < N; j++) {
  149. rotatedMatrix[i][j] = matrix[N - j - 1][i];
  150. }
  151. }
  152. // przypisanie nowej macierzy do oryginalnej
  153. matrix = rotatedMatrix;
  154. }
  155.  
  156. void rotateLeft(vector<vector<char>>& matrix, int N) {
  157. // utworzenie nowej macierzy z obróconymi elementami
  158. vector<vector<char>> rotatedMatrix(N, vector<char>(N));
  159. for (int i = 0; i < N; i++) {
  160. for (int j = 0; j < N; j++) {
  161. rotatedMatrix[i][j] = matrix[j][N - i - 1];
  162. }
  163. }
  164. // przypisanie nowej macierzy do oryginalnej
  165. matrix = rotatedMatrix;
  166. }
  167.  
  168. void transpose(vector<vector<char>>& matrix, int N) {
  169. // zamiana elementów macierzy wzglêdem g³ównej przek¹tnej
  170. for (int i = 0; i < N; i++) {
  171. for (int j = i + 1; j < N; j++) {
  172. int temp = matrix[i][j];
  173. matrix[i][j] = matrix[j][i];
  174. matrix[j][i] = temp;
  175. }
  176. }
  177. }
  178. void wynik(string str,int &wspx,int &wspy, int &wsx, int &wsy ,int N)
  179. {
  180. int dl=str.length(),i=0,obrot=0,pomx,pomy;
  181. //for(i=0;i<dl-1;i++)
  182.  
  183. while(i<dl-1)
  184. {
  185. switch(str[i])
  186. {
  187. case 'A':
  188. //for (int i = 0; i < N; i++) {
  189. // for (int j = 0; j < N; j++) {
  190. //rotatedMatrix[i][j] = matrix[N - j - 1][i];
  191. //cout<<"A"<<endl;
  192. pomx=wspx;
  193. pomy=wspy;
  194. wspx=N-pomy-1;
  195. wspy=pomx;
  196. pomx=wsx;
  197. pomy=wsy;
  198. wsx=N-pomy-1;
  199. wsy=pomx;
  200. break;
  201. case 'B'://rotate180(matrix,N);
  202. // for (int i = 0; i < N/2; i++) {
  203. // for (int j = 0; j < N; j++) {
  204. // swap(matrix[i][j], matrix[N-i-1][N-j-1]);
  205. //cout<<"B"<<endl;
  206. pomx=wspx;
  207. pomy=wspy;
  208. wspx=N-pomx-1;
  209. wspy=N-pomy-1;
  210. pomx=wsx;
  211. pomy=wsy;
  212. wsx=N-pomx-1;
  213. wsy=N-pomy-1;
  214. break;
  215. case 'C':
  216. // for(int i = 0; i < N; i++) {
  217. // for(int j = 0; j < N; j++) {
  218. // rotatedMatrix[i][j] = matrix[j][N - i - 1];
  219. pomx=wspx;
  220. pomy=wspy;
  221. wspx=pomy;
  222. wspy=N-pomx-1;
  223. pomx=wsx;
  224. pomy=wsy;
  225. wsx=pomy;
  226. wsy=N-pomx-1;
  227.  
  228. //cout<<"C"<<endl;
  229. break;
  230. case 'X':
  231. //rotatedMatrix[i][j] = matrix[j][N - i - 1];
  232. pomx=wspx;
  233. pomy=wspy;
  234. wspx=pomy;
  235. wspy=N-pomx-1;
  236. pomx=wsx;
  237. pomy=wsy;
  238. wsx=pomy;
  239. wsy=N-pomx-1;
  240. //cout<<"X"<<endl;
  241. break;
  242. case 'Y':
  243. pomx=wspx;
  244. pomy=wspy;
  245. wspx=N-pomx-1;
  246. wspy=N-pomy-1;
  247. pomx=wsx;
  248. pomy=wsy;
  249. wsx=N-pomx-1;
  250. wsy=N-pomy-1;
  251. //cout<<"Y"<<endl;
  252. break;
  253. case 'Z':
  254. //cout<<"Z"<<endl;
  255. pomx=wspx;
  256. pomy=wspy;
  257. wspx=N-pomy-1;
  258. wspy=pomx;
  259. pomx=wsx;
  260. pomy=wsy;
  261. wsx=N-pomy-1;
  262. wsy=pomx;
  263. break;
  264. case 'T':
  265. //transpose _main
  266. // 0,0 x=0 y,0 0,1 x=1, y=0;
  267. //cout<<'T'<<" "<<wspx<<" "<<wspy<<" "<<wsx<<" "<<wsy<<endl;
  268. pomx=wspx;
  269. pomy=wspy;
  270. wspx=pomy;
  271. wspy=pomx;
  272. pomx=wsx;
  273. pomy=wsy;
  274. wsx=pomy;
  275. wsy=pomx;
  276. //cout<<'T'<<" "<<wspx<<" "<<wspy<<" "<<wsx<<" "<<wsy<<endl;
  277. // cout<<"T"<<endl;
  278. break;
  279. case 'D':
  280. //cout<<"D"<<endl;
  281. //temp[N-j-1][N-i-1]=matrix[i][j];
  282. pomx=wspx;
  283. pomy=wspy;
  284. wspx=N-pomy-1;
  285. wspy=N-pomx-1;
  286. pomx=wsx;
  287. pomy=wsy;
  288. wsx=N-pomy-1;
  289. wsy=N-pomx-1;
  290. break;
  291. case 'V':
  292. //swap(matrix[i][j], matrix[i][N-j-1]);
  293.  
  294. pomx=wspx;
  295. pomy=wspy;
  296. wspx=pomx;
  297. wspy=N-pomy-1;
  298. pomx=wsx;
  299. pomy=wsy;
  300. wsx=pomx;
  301. wsy=N-pomy-1;
  302. //cout<<"H"<<endl;
  303. cout<<'H'<<" "<<wspx<<" "<<wspy<<" "<<wsx<<" "<<wsy<<endl;
  304. break;
  305. case 'H':
  306. //swap(matrix[i][j], matrix[N-i-1][j]);
  307. pomx=wspx;
  308. pomy=wspy;
  309. wspx=N-pomx-1;
  310. wspy=pomy;
  311. pomx=wsx;
  312. pomy=wsy;
  313. wsx=N-pomx-1;
  314. wsy=pomy;
  315. //cout<<"V"<<endl;
  316. break;
  317. }
  318. i++;
  319. }
  320. }
  321. void analiza(string str,vector<vector<char>>& matrix, int N)
  322. {
  323. int dl=str.length(),i=0,obrot=0;
  324. //for(i=0;i<dl-1;i++)
  325. while(i<dl-1)
  326. {
  327. //cout<<"jestem"<<endl;
  328. while(str[i]=='A'||str[i]=='B'||str[i]=='C'||str[i]=='X'||str[i]=='Y'||str[i]=='Z')
  329. {
  330. switch (str[i])
  331. {
  332. case 'A':obrot+=90 ;
  333. break;
  334. case 'B':obrot+=180;
  335. break;
  336. case 'C':obrot+=270;
  337. break;
  338. case 'X':obrot+=-90;
  339. break;
  340. case 'Y':obrot+=-180;
  341. break;
  342. case 'Z':obrot+=-270;
  343. break;
  344. }
  345. i++;
  346. }
  347.  
  348. obrot=obrot%360;
  349. //cout<<obrot<<endl;
  350. switch(obrot)
  351. {
  352. case 90:rotateRight(matrix,N) ;
  353. break;
  354. case 180:rotate180(matrix,N);
  355. break;
  356. case 270:obrot_w_prawo_270(matrix,N);
  357. break;
  358. case -90:rotateLeft(matrix,N);
  359. break;
  360. case -180:rotate180(matrix,N);
  361. break;
  362. case -270:obrot_w_lewo_270(matrix,N);
  363. }
  364. obrot=0;
  365. while(i<dl-1 && str[i]==str[i+1])i=i+2;
  366. //cout<<i<<endl;
  367. if(i<dl-1 && (str[i]=='T'||str[i]=='D'||str[i]=='H'||str[i]=='V'))
  368. {
  369. //cout<<"tu"<<endl;
  370. switch(str[i])
  371. {
  372. case 'T':transpose_main(matrix,N);
  373. break;
  374. case 'D':transpose_secondary( matrix,N);
  375. break;
  376. case 'H':flipHorizontal(matrix,N);
  377. break;
  378. case 'V':flipVertical(matrix,N);
  379. break;
  380.  
  381. }
  382. cout<<"wyk="<<str[i]<<" ";
  383. i++;
  384. }
  385. }
  386. cout<<endl;
  387. }
  388. void analize(string str,vector<vector<char>>& matrix, int N)
  389. {
  390. int dl=str.length(),i;
  391. cout<<"dl="<<dl<<endl;
  392. for(i=0;i<dl-1;i++)
  393. {
  394. switch(str[i])
  395. {
  396. case 'A':rotateRight(matrix,N) ;
  397. //cout<<"A"<<endl;
  398. break;
  399. case 'B':rotate180(matrix,N);
  400. //cout<<"B"<<endl;
  401. //wypisz_macierz(matrix,N);
  402. break;
  403. case 'C':obrot_w_prawo_270(matrix,N);
  404. //cout<<"C"<<endl;
  405. break;
  406. case 'X':rotateLeft(matrix,N);
  407. //cout<<"X"<<endl;
  408. break;
  409. case 'Y':rotate180(matrix,N);
  410. //cout<<"Y"<<endl;
  411. break;
  412. case 'Z':obrot_w_lewo_270(matrix,N);
  413. //cout<<"Z"<<endl;
  414. break;
  415. case 'T':transpose_main(matrix,N);
  416. // cout<<"T"<<endl;
  417. break;
  418. case 'D':transpose_secondary( matrix,N);
  419. //cout<<"D"<<endl;
  420. break;
  421. case 'H':flipHorizontal(matrix,N);
  422. //cout<<"H"<<endl;
  423. break;
  424. case 'V':flipVertical(matrix,N);
  425. //cout<<"V"<<endl;
  426. break;
  427. case '&':
  428. //cout<<"&"<<endl;
  429. break;
  430. }
  431.  
  432. }
  433. cout<<i<<endl;
  434. }
  435. int main() {
  436. ios_base::sync_with_stdio(false);
  437. int N,t,dl,obrot=0;
  438. string str;
  439. cin>>t;
  440.  
  441. while(t--)
  442. {
  443. cin >> N;
  444. //cout<<N<<endl;
  445. // wczytanie macierzy
  446. vector<vector<char>> matrix(N, vector<char>(N));
  447. //cout<<matrix.size()<<endl;
  448. //vector<vector<char>> matrix2(N, vector<char>(N));
  449. for (int i = 0; i < N; i++) {
  450. for (int j = 0; j < N; j++) {
  451. cin >> matrix[i][j];
  452. }
  453. }
  454. // matrix2=matrix;
  455. cin>>str;
  456. //cout<<str.length()<<endl;
  457. //cout<<str<<endl;
  458. int wspx=0,wspy=0,wsx=0,wsy=1,pomx,pomy,pox,poy;
  459. //analiza(str,matrix,N);
  460. wynik(str,wspx,wspy,wsx,wsy,N);
  461. cout<<wspx<<" "<<wspy<<" "<<wsx<<" "<<wsy<<" "<<wsx-wspx<<" "<<wsy-wspy<<endl;
  462. //odzyskaj macierz po dwóch elementach
  463. //for (int i = 0; i < N; i++) {
  464. // for (int j = 0; j < N; j++) {
  465. if(wspx==0)//ta sama kolumna
  466. {
  467. if(wspy==0) //bez zmian?
  468. {
  469.  
  470. if(wsx==1)
  471. transpose_main( matrix,N);
  472. }
  473. else
  474. {
  475. if(wsx==0) //ta sama kolumna wiersz wyzej
  476. {
  477. //V
  478. // cout<<"H"<<endl;
  479. //flipHorizontal(matrix,N);
  480. flipVertical(matrix,N);
  481. }
  482. else
  483. {
  484. // cout<<"C"<<endl;
  485. // cout<<wspx<<" "<<wspy<<" "<<wsx<<" wsy"<<endl;
  486. obrot_w_prawo_270(matrix,N);
  487. }//wiersz dolny kolumna obok
  488. }
  489. }//kolumna ostatnia
  490. else
  491. {
  492. if(wspy==0) //pierwszy wiersz
  493. {
  494. if(wsx==N-1)
  495. {
  496. //H
  497. //cout<<"V"<<endl;
  498. flipHorizontal(matrix,N);
  499. //flipVertical(matrix,N);
  500. }
  501. else
  502. {
  503. //cout<<"A"<<endl;
  504. rotateRight(matrix,N) ;
  505. }
  506. }
  507. else
  508. {
  509. //ostatni wiersz
  510. if(wsy==N-1)
  511. {
  512. //D
  513. transpose_secondary( matrix,N);
  514. }
  515. else
  516. {
  517. //B
  518. rotate180(matrix,N);
  519. }
  520. }
  521.  
  522. }
  523. // }
  524. //}
  525.  
  526.  
  527. //cout<<"po:"<<endl;
  528. wypisz_macierz(matrix,N);
  529. cout<<endl;
  530.  
  531. }
  532. return 0;
  533. }
  534.  
Success #stdin #stdout 0.01s 5392KB
stdin
10
6
qldswt
mhmjxq
edqkpr
ngprox
vfpeua
gbsxex
YYTATT&
6
qldswt
mhmjxq
edqkpr
ngprox
vfpeua
gbsxex
YYTAT&
6
qldswt
mhmjxq
edqkpr
ngprox
vfpeua
gbsxex
H&
6
qrjaty
dimofu
gclqur
clqecd
rhvqsu
wbvntm
DVZYXBCXXY&


6
qrjaty
dimofu
gclqur
clqecd
rhvqsu
wbvntm
Z&
4
abcf
defg
ghih
lkjh
ABTHVDTHDACVDT&
4
abcf
defg
ghih
lkjh
AB&
4
abcf
defg
ghih
lkjh
C&
4
abcf
defg
ghih
lkjh
T&
4
abcf
defg
ghih
lkjh
D&
4
abcf
defg
ghih
lkjh
H&
4
abcf
defg
ghih
lkjh
V&
4
abcf
defg
ghih
lkjh
X&
4
abcf
defg
ghih
lkjh
Y&
4
abcf
defg
ghih
lkjh
Z&
5
poiud
sdfgk
dfsad
dfghj
qwert
TDHVVHDT&
stdout
5 0 5 1 0 1
twsdlq
qxjmhm
rpkqde
xorpgn
auepfv
xexsbg

0 5 1 5 1 0
tqrxax
wxpoue
sjkrex
dmqpps
lhdgfb
qmenvg

5 0 5 1 0 1
twsdlq
qxjmhm
rpkqde
xorpgn
auepfv
xexsbg

H 5 0 4 0
0 0 0 1 0 1
qrjaty
dimofu
gclqur
clqecd
rhvqsu
wbvntm

5 0 4 0 -1 0
wrcgdq
bhlcir
vvqlmj
nqeqoa
tscuft
mudruy

H 0 3 0 2
H 3 0 2 0
0 3 1 3 1 0
fghh
cfij
behk
adgl

0 3 1 3 1 0
fghh
cfij
behk
adgl

0 3 1 3 1 0
fghh
cfij
behk
adgl

0 0 1 0 1 0
adgl
behk
cfij
fghh

3 3 2 3 -1 0
hhgf
jifc
kheb
lgda