Source code for submission s931

fp.cpp

  1. #include <algorithm>
  2. #include <cctype>
  3. #include <cmath>
  4. #include <complex>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <iomanip>
  8. #include <iostream>
  9. #include <list>
  10. #include <map>
  11. #include <queue>
  12. #include <set>
  13. #include <sstream>
  14. #include <stack>
  15. #include <string>
  16. #include <utility>
  17.  
  18. using namespace std;
  19.  
  20. #define DEBUG(x) cout << ">>> " << #x << " : " << x << endl;
  21. #define REP(i,a) for (int i = 0; i < (a); ++i)
  22. #define FOR(i,a,b) for (int i = (a); i <= (b); ++i)
  23. #define FORD(i,a,b) for (int i = (a); i >= (b); --i)
  24. inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
  25. const int INF = 1<<29;
  26.  
  27. typedef long long ll;
  28.  
  29. struct Tile{
  30. char arr[5][5];
  31. };
  32.  
  33. struct Bigtile{
  34. char arr[15][15];
  35. };
  36.  
  37. Tile tiles[12][8];
  38.  
  39. char help[28];
  40.  
  41. Tile reflect(const Tile& A){
  42. Tile B;
  43. REP(i,5) REP(j,5) B.arr[4-i][j] = A.arr[i][j];
  44. return B;
  45. }
  46.  
  47. Tile rotate(const Tile& A){
  48. Tile B;
  49. REP(i,5) REP(j,5) B.arr[4-j][i] = A.arr[i][j];
  50. return B;
  51. }
  52.  
  53. Bigtile reflect(const Bigtile& A){
  54. Bigtile B;
  55. REP(i,15) REP(j,15) B.arr[14-i][j] = A.arr[i][j];
  56. return B;
  57. }
  58.  
  59. Bigtile rotate(const Bigtile& A){
  60. Bigtile B;
  61. REP(i,15) REP(j,15) B.arr[14-j][i] = A.arr[i][j];
  62. return B;
  63. }
  64.  
  65. int charnum(char c){
  66. if(c=='F') return 0;
  67. if(c=='I') return 1;
  68. if(c=='L') return 2;
  69. if(c=='N') return 3;
  70. if(c=='P') return 4;
  71. if(c=='T') return 5;
  72. if(c=='U') return 6;
  73. if(c=='V') return 7;
  74. if(c=='W') return 8;
  75. if(c=='X') return 9;
  76. if(c=='Y') return 10;
  77. if(c=='Z') return 11;
  78. return 0;
  79. }
  80.  
  81. Bigtile zero, Xzero,X;
  82. Bigtile shift(const Bigtile& A){
  83. Bigtile C = zero;
  84. int minX=20, minY=20;
  85. REP(i,15)REP(j,15){
  86. if (A.arr[i][j] == 'x') {
  87. if (i < minY) minY = i;
  88. if (j < minX) minX = j;
  89. }
  90. }
  91. if (minX == 20) return A;
  92. REP(i,15-minY)REP(j,15-minX){
  93. C.arr[i][j] = A.arr[i+minY][j+minX];
  94. }
  95. return C;
  96. }
  97.  
  98. bool compare(const Bigtile& A, const Bigtile& B_){
  99. Bigtile C,D;
  100. C = A;
  101. Bigtile B = shift(B_);
  102. bool ok2;
  103. REP(k,4){
  104. C = shift(C);
  105. ok2=true;
  106. REP(i,15) REP(j,15){
  107. if(C.arr[i][j]!=B.arr[i][j]) ok2=false;
  108. }
  109. if(ok2) return true;
  110. D = shift(reflect(C));
  111.  
  112. ok2 = true;
  113. REP(i,15) REP(j,15){
  114. if(D.arr[i][j]!=B.arr[i][j]) ok2=false;
  115. }
  116. if(ok2) return true;
  117. C = rotate(C);
  118. }
  119. return false;
  120. }
  121.  
  122. Bigtile con;
  123.  
  124. void dfs(int x, int y){
  125. if(x<0 || y<0 || x>=15 || y>=15) return;
  126. if(con.arr[x][y]=='.') return;
  127. con.arr[x][y]='.';
  128. dfs(x+1,y);
  129. dfs(x-1,y);
  130. dfs(x,y+1);
  131. dfs(x,y-1);
  132. }
  133. bool is_connected(){
  134. REP(i,15) REP(j,15){
  135. if(con.arr[i][j]=='x'){
  136. dfs(i,j);
  137. REP(k,15) REP(l,15) if(con.arr[k][l]=='x') return false;
  138. return true;
  139. }
  140. }
  141. return true;
  142. }
  143.  
  144.  
  145. vector< Bigtile > pos[12][12];
  146.  
  147. char c1,c2,c3,c4,c5,c6;
  148. bool done[12][12];
  149. int d1,d2,d3,d4;
  150. bool ok;
  151.  
  152. int main() {
  153. strcpy(help,".xx..xx....x.............");
  154. REP(i,5) REP(j,5) tiles[0][0].arr[i][j] = help[i*5+j];
  155.  
  156. strcpy(help,"x....x....x....x....x....");
  157. REP(i,5) REP(j,5) tiles[1][0].arr[i][j] = help[i*5+j];
  158.  
  159. strcpy(help,"x....x....x....xx........");
  160. REP(i,5) REP(j,5) tiles[2][0].arr[i][j] = help[i*5+j];
  161.  
  162. strcpy(help,".x....x...xx...x.........");
  163. REP(i,5) REP(j,5) tiles[3][0].arr[i][j] = help[i*5+j];
  164.  
  165. strcpy(help,"xx...xx...x..............");
  166. REP(i,5) REP(j,5) tiles[4][0].arr[i][j] = help[i*5+j];
  167.  
  168. strcpy(help,"xxx...x....x.............");
  169. REP(i,5) REP(j,5) tiles[5][0].arr[i][j] = help[i*5+j];
  170.  
  171. strcpy(help,"x.x..xxx.................");
  172. REP(i,5) REP(j,5) tiles[6][0].arr[i][j] = help[i*5+j];
  173.  
  174. strcpy(help,"x....x....xxx............");
  175. REP(i,5) REP(j,5) tiles[7][0].arr[i][j] = help[i*5+j];
  176.  
  177. strcpy(help,"x....xx....xx............");
  178. REP(i,5) REP(j,5) tiles[8][0].arr[i][j] = help[i*5+j];
  179.  
  180. strcpy(help,".x...xxx...x.............");
  181. REP(i,5) REP(j,5) tiles[9][0].arr[i][j] = help[i*5+j];
  182.  
  183. strcpy(help,".x...xx....x....x........");
  184. REP(i,5) REP(j,5) tiles[10][0].arr[i][j] = help[i*5+j];
  185.  
  186. strcpy(help,"xx....x....xx............");
  187. REP(i,5) REP(j,5) tiles[11][0].arr[i][j] = help[i*5+j];
  188.  
  189. REP(i,12){
  190. tiles[i][1] = reflect(tiles[i][0]);
  191. REP(j,3){
  192. tiles[i][2*j+2] = rotate(tiles[i][2*j]);
  193. tiles[i][2*j+3] = rotate(tiles[i][2*j+1]);
  194. }
  195. }
  196.  
  197. REP(i,15) REP(j,15) zero.arr[i][j]='.';
  198. while(scanf("%c%c%c%c%c%c",&c1,&c2,&c5,&c3,&c4,&c6) == 6){
  199. //DEBUG(c1);
  200. d1 = charnum(c1);
  201. d2 = charnum(c2);
  202. d3 = charnum(c3);
  203. d4 = charnum(c4);
  204. //DEBUG(d1);
  205. //DEBUG(d2);
  206. //DEBUG(d3);
  207. //DEBUG(d4);
  208. if(!done[d1][d2]){
  209. Xzero = zero;
  210. REP(k,8){
  211. REP(i,5) REP(j,5) Xzero.arr[i+5][j+5] = tiles[d1][k].arr[i][j];
  212. REP(m,10) REP(n,10){
  213. ok=true;
  214. X = Xzero;
  215. REP(i,5) REP(j,5){
  216. if(X.arr[i+m][j+n]=='x' && tiles[d2][0].arr[i][j]=='x') ok=false;
  217. if(tiles[d2][0].arr[i][j]=='x') X.arr[i+m][j+n]=tiles[d2][0].arr[i][j];
  218. }
  219. con = X;
  220. if(ok && is_connected()){
  221. pos[d1][d2].push_back(X);
  222. }
  223. }
  224. }
  225.  
  226. }
  227. if(!done[d3][d4]){
  228. d1 = d3; d2 = d4;
  229. Xzero = zero;
  230. REP(k,8){
  231. REP(i,5) REP(j,5) Xzero.arr[i+5][j+5] = tiles[d1][k].arr[i][j];
  232. REP(m,10) REP(n,10){
  233. ok=true;
  234. X = Xzero;
  235. REP(i,5) REP(j,5){
  236. if(X.arr[i+m][j+n]=='x' && tiles[d2][0].arr[i][j]=='x') ok=false;
  237. if(tiles[d2][0].arr[i][j]=='x') X.arr[i+m][j+n]=tiles[d2][0].arr[i][j];
  238. }
  239. con = X;
  240. if(ok && is_connected()){
  241. pos[d1][d2].push_back(X);
  242. }
  243. }
  244. }
  245. }
  246. d1 = charnum(c1); d2 = charnum(c2);
  247.  
  248. ok=false;
  249. REP(i,pos[d1][d2].size()){
  250. if(ok) break;
  251. REP(j,pos[d3][d4].size()){
  252. if(ok) break;
  253. if(compare(pos[d1][d2][i],pos[d3][d4][j])) ok=true;
  254. }
  255. }
  256. if(ok) printf("YES\n");
  257. else printf("NO\n");
  258. }
  259.  
  260.  
  261. return 0;
  262. }
  263.