Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int jednako=(1<<16)-1;
  5. bool bio[252][1<<16], da;
  6. int n, cost[5], mask[252], bt, crntmsk;
  7. string s[4];
  8. set <pair <int,int> > st[252];
  9. pair <int,int> p;
  10.  
  11. void f(int cnt, int maska, int rj, int x, int y)
  12. {
  13. //cout << x << " " << y << " // " << rj << endl;
  14. if (((maska)&(jednako))==jednako) {
  15. //cout << "-----" << endl;
  16. st[cnt+1].insert(p=make_pair(rj,maska>>16));
  17. return;
  18. }
  19.  
  20. if (x==3 && y==3) {
  21. return;
  22. }
  23.  
  24. int msk=maska;
  25.  
  26. if (x<3) f(cnt,msk,rj,x+1,y);
  27. else f(cnt,msk,rj,0,y+1);
  28.  
  29. for (int i=1;i<5;i++) {
  30. if (x+i<=4) {
  31. bt=y*4+x;
  32. for (int j=0;j<i;j++) {
  33. for (int k=0;k<i;k++) {
  34. maska|=(1<<bt);
  35. bt++;
  36. }
  37. bt+=(4-i);
  38. }
  39. if (x<3) f(cnt,maska,rj+cost[i],x+1,y);
  40. else f(cnt,maska,rj+cost[i],0,y+1);
  41. }
  42. }
  43.  
  44.  
  45. return;
  46. }
  47.  
  48. int main()
  49. {
  50. cin >> n;
  51. for (int i=1;i<5;i++) cin >> cost[i];
  52.  
  53. for (int i=0;i<4;i++) {
  54. cin >> s[i];
  55. }
  56.  
  57. for (int i=0;i<n;i++) {
  58. for (int j=0;j<4;j++) {
  59. if (s[j][i]=='*') mask[i/4]|=(1<<((i%4)*4+j));
  60. }
  61. }
  62. if (n%4) {
  63. bt=1<<15;
  64. da=1;
  65. for (int i=0;i<4-n%4;i++) {
  66. for (int j=0;j<4;j++) {
  67. mask[n/4]|=bt;
  68. bt--;
  69. }
  70. }
  71. }
  72.  
  73. f(0,mask[0],0,0,0);
  74.  
  75.  
  76. for (int i=1;da?i<=n/4:i<n/4;i++) {
  77.  
  78. for (set<pair<int,int> >::const_iterator it=st[i].begin();it!=st[i].end();it++) {
  79. if (!bio[i][it->second]) {
  80. bio[i][it->second]=1;
  81. crntmsk=mask[i];
  82. crntmsk|=(it->second);
  83. f(i,crntmsk,it->first,0,0);
  84. }
  85. }
  86. }
  87.  
  88. set<pair<int,int> >::const_iterator it=st[da?n/4+1:n/4].begin();
  89. cout << it->first;
  90. }
  91.  
  92. /*
  93. 15
  94. 2 5 11 16
  95. .**...****.....
  96. **.*.*******..*
  97. *.....*..**..**
  98. ..**..****.*...
  99. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement