Source code for submission s897

fp.cpp

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <set>
  4.  
  5. typedef struct POINT
  6. {
  7. int x;
  8. int y;
  9. }
  10. POINT;
  11.  
  12. using namespace std;
  13. typedef set<POINT> SET;
  14. typedef SET::iterator IT;
  15.  
  16. POINT Base[12][5] =
  17. {
  18. //F
  19. {
  20. (POINT) {1, 0},
  21. (POINT) {2, 0},
  22. (POINT) {0, 1},
  23. (POINT) {1, 1},
  24. (POINT) {1, 2}
  25. },
  26.  
  27. //I
  28. {
  29. (POINT) {0, 0},
  30. (POINT) {0, 1},
  31. (POINT) {0, 2},
  32. (POINT) {0, 3},
  33. (POINT) {0, 4}
  34. },
  35.  
  36. //L
  37. {
  38. (POINT) {0, 0},
  39. (POINT) {0, 1},
  40. (POINT) {0, 2},
  41. (POINT) {0, 3},
  42. (POINT) {1, 3}
  43. },
  44.  
  45. //N
  46. {
  47. (POINT) {1, 0},
  48. (POINT) {1, 1},
  49. (POINT) {0, 2},
  50. (POINT) {1, 2},
  51. (POINT) {0, 3}
  52. },
  53.  
  54. //P
  55. {
  56. (POINT) {0, 0},
  57. (POINT) {1, 0},
  58. (POINT) {0, 1},
  59. (POINT) {1, 1},
  60. (POINT) {0, 2}
  61. },
  62.  
  63. //T
  64. {
  65. (POINT) {0, 0},
  66. (POINT) {1, 0},
  67. (POINT) {2, 0},
  68. (POINT) {1, 1},
  69. (POINT) {1, 2}
  70. },
  71.  
  72. //U
  73. {
  74. (POINT) {0, 0},
  75. (POINT) {2, 0},
  76. (POINT) {0, 1},
  77. (POINT) {1, 1},
  78. (POINT) {2, 1}
  79. },
  80.  
  81. //V
  82. {
  83. (POINT) {0, 0},
  84. (POINT) {0, 1},
  85. (POINT) {0, 2},
  86. (POINT) {1, 2},
  87. (POINT) {2, 2}
  88. },
  89.  
  90. //W
  91. {
  92. (POINT) {0, 0},
  93. (POINT) {0, 1},
  94. (POINT) {1, 1},
  95. (POINT) {1, 2},
  96. (POINT) {2, 2}
  97. },
  98.  
  99. //X
  100. {
  101. (POINT) {1, 0},
  102. (POINT) {0, 1},
  103. (POINT) {1, 1},
  104. (POINT) {2, 1},
  105. (POINT) {1, 2}
  106. },
  107.  
  108. //Y
  109. {
  110. (POINT) {1, 0},
  111. (POINT) {0, 1},
  112. (POINT) {1, 1},
  113. (POINT) {1, 2},
  114. (POINT) {1, 3}
  115. },
  116.  
  117. //Z
  118. {
  119. (POINT) {0, 0},
  120. (POINT) {1, 0},
  121. (POINT) {1, 1},
  122. (POINT) {1, 2},
  123. (POINT) {2, 2}
  124. }
  125. };
  126.  
  127. int operator < (const POINT& P, const POINT& Q)
  128. {
  129. if(P.x < Q.x) return 1;
  130. if(P.x > Q.x) return 0;
  131. return P.y < Q.y;
  132. }
  133.  
  134. POINT Pent[12][8][5];
  135.  
  136. int CharToIndex[300];
  137. char IndexToChar[20] = "FILNPTUVWXYZ";
  138.  
  139. void InitializeOrientations()
  140. {
  141. for(int i = 0; i < 12; i++)
  142. {
  143. for(int j = 0; j < 8; j++)
  144. {
  145. for(int k = 0; k < 5; k++)
  146. {
  147. Pent[i][j][k].x = Base[i][k].x;
  148. Pent[i][j][k].y = Base[i][k].y;
  149.  
  150. if(j >= 4)
  151. {
  152. Pent[i][j][k].x *= -1;
  153. }
  154.  
  155. for(int d = 0; d < j % 4; d++)
  156. {
  157. int q = -Pent[i][j][k].x;
  158. Pent[i][j][k].x = Pent[i][j][k].y;
  159. Pent[i][j][k].y = q;
  160. }
  161. }
  162. }
  163. }
  164. }
  165.  
  166. SET Adj[12][8];
  167.  
  168. void InitializeAdj()
  169. {
  170. for(int i = 0; i < 12; i++)
  171. {
  172. for(int j = 0; j < 8; j++)
  173. {
  174. for(int k = 0; k < 5; k++)
  175. {
  176. Adj[i][j].insert((POINT) {Pent[i][j][k].x + 0, Pent[i][j][k].y + 1});
  177. Adj[i][j].insert((POINT) {Pent[i][j][k].x + 1, Pent[i][j][k].y + 0});
  178. Adj[i][j].insert((POINT) {Pent[i][j][k].x + 0, Pent[i][j][k].y - 1});
  179. Adj[i][j].insert((POINT) {Pent[i][j][k].x - 1, Pent[i][j][k].y + 0});
  180. }
  181. }
  182. }
  183. }
  184.  
  185. void FindMinFromSet(SET& set, int& xMin, int& yMin)
  186. {
  187. xMin = 20;
  188. yMin = 20;
  189.  
  190. for(IT it = set.begin(); it != set.end(); it++)
  191. {
  192. if(it->x < xMin) xMin = it->x;
  193. if(it->y < yMin) yMin = it->y;
  194. }
  195. }
  196.  
  197. void FindMinFromArray(POINT* array, int& xMin, int& yMin)
  198. {
  199. xMin = 20;
  200. yMin = 20;
  201.  
  202. for(int i = 0; i < 5; i++)
  203. {
  204. if(array[i].x < xMin) xMin = array[i].x;
  205. if(array[i].y < yMin) yMin = array[i].y;
  206. }
  207. }
  208.  
  209. int Inner(SET& set, int ci, int di)
  210. {
  211. /*int Table[10][10] = {{0}};
  212. int xMin, yMin;
  213. FindMinFromSet(set, xMin, yMin);
  214.  
  215. for(IT it = set.begin(); it != set.end(); it++)
  216. {
  217. Table[it->x - xMin][it->y - yMin] = 1;
  218. printf("(%d, %d)\n", it->x, it->y);
  219. }
  220.  
  221. for(int y = 0; y < 10; y++)
  222. {
  223. putchar(' ');
  224. for(int x = 0; x < 10; x++)
  225. {
  226. putchar((Table[x][y]) ? '#' : '.');
  227. }
  228.  
  229. putchar('\n');
  230. }
  231.  
  232. putchar('\n');*/
  233.  
  234. for(int i = 0; i < 8; i++)
  235. //orientation of c
  236. {
  237. for(int j = 0; j < 5; j++)
  238. //going through points of c
  239. {
  240. for(IT it = set.begin(); it != set.end(); it++)
  241. //going through the points of a, b
  242. {
  243. SET set2(set);
  244.  
  245. int xDiff = it->x - Pent[ci][i][j].x;
  246. int yDiff = it->y - Pent[ci][i][j].y;
  247.  
  248. for(int k = 0; k < 5; k++)
  249. {
  250. if(!(set2.erase((POINT) {Pent[ci][i][k].x + xDiff, Pent[ci][i][k].y + yDiff})))
  251. {
  252. //printf("Could not find (%d, %d)\n", Pent[ci][i][k].x + xDiff, Pent[ci][i][k].y + yDiff);
  253. goto FAIL;
  254. }
  255. else
  256. {
  257. //printf("Could find (%d, %d)\n", Pent[ci][i][k].x + xDiff, Pent[ci][i][k].y + yDiff);
  258. }
  259. }
  260.  
  261. int xMin, yMin;
  262. FindMinFromSet(set2, xMin, yMin);
  263.  
  264. for(int d = 0; d < 8; d++)
  265. //orientation of d
  266. {
  267. SET set3(set2);
  268. int xMinD, yMinD;
  269. FindMinFromArray(Pent[di][d], xMinD, yMinD);
  270.  
  271. xDiff = xMin - xMinD;
  272. yDiff = yMin - yMinD;
  273.  
  274. for(int k = 0; k < 5; k++)
  275. {
  276. if(!(set3.erase((POINT) {Pent[di][d][k].x + xDiff, Pent[di][d][k].y + yDiff})))
  277. {
  278. //printf("Could not find 2 (%d, %d)\n", Pent[di][d][k].x + xDiff, Pent[di][d][k].y + yDiff);
  279. goto INNER_FAIL;
  280. }
  281. }
  282.  
  283. return 1;
  284.  
  285. INNER_FAIL:;
  286. }
  287.  
  288. FAIL:;
  289. }
  290. }
  291. }
  292.  
  293. return 0;
  294. }
  295.  
  296. int main()
  297. {
  298. for(int i = 0; i < 12; i++)
  299. {
  300. CharToIndex[(int) IndexToChar[i]] = i;
  301. }
  302.  
  303. InitializeOrientations();
  304. InitializeAdj();
  305.  
  306. /*for(int i = 0; i < 12; i++)
  307. {
  308. for(int q = 0; q < 8; q++)
  309. {
  310. int xMin = 10;
  311. int yMin = 10;
  312.  
  313. for(int j = 0; j < 5; j++)
  314. {
  315. if(Pent[i][q][j].x < xMin) xMin = Pent[i][q][j].x;
  316. if(Pent[i][q][j].y < yMin) yMin = Pent[i][q][j].y;
  317. }
  318.  
  319. printf("Znak %c; orientace %d\n", IndexToChar[i], q);
  320. int Table[5][5] = {{0}};
  321. for(int j = 0; j < 5; j++)
  322. {
  323. Table[Pent[i][q][j].x - xMin][Pent[i][q][j].y - yMin] = 1;
  324. }
  325.  
  326. for(int y = 0; y < 5; y++)
  327. {
  328. putchar(' ');
  329. for(int x = 0; x < 5; x++)
  330. {
  331. putchar((Table[x][y]) ? '#' : '.');
  332. }
  333.  
  334. putchar('\n');
  335. }
  336.  
  337. putchar('\n');
  338. }
  339. }
  340.  
  341. return 0;*/
  342.  
  343. char a, b, c, d;
  344. while(scanf(" %c%c %c%c", &a, &b, &c, &d) > 0)
  345. {
  346. int ai = CharToIndex[(int) a];
  347. int bi = CharToIndex[(int) b];
  348. int ci = CharToIndex[(int) c];
  349. int di = CharToIndex[(int) d];
  350.  
  351. SET& aAdj = Adj[ai][0];
  352.  
  353. for(int i = 0; i < 8; i++)
  354. //orientation of b
  355. {
  356. for(int j = 0; j < 5; j++)
  357. //going through points of b
  358. {
  359. for(IT it = aAdj.begin(); it != aAdj.end(); it++)
  360. //going through neighbors of a
  361. {
  362. int xDiff = it->x - Pent[bi][i][j].x;
  363. int yDiff = it->y - Pent[bi][i][j].y;
  364.  
  365. SET set;
  366. for(int k = 0; k < 5; k++)
  367. {
  368. set.insert(Pent[ai][0][k]);
  369. }
  370.  
  371. for(int k = 0; k < 5; k++)
  372. {
  373. set.insert((POINT) {Pent[bi][i][k].x + xDiff, Pent[bi][i][k].y + yDiff});
  374. }
  375.  
  376. if(set.size() == 10)
  377. {
  378. if(Inner(set, ci, di))
  379. {
  380. goto YES;
  381. }
  382. }
  383. }
  384. }
  385. }
  386.  
  387. printf("NO\n");
  388. continue;
  389.  
  390. YES: printf("YES\n");
  391. }
  392.  
  393. return 0;
  394. }
  395.