Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int jednako=(1<<16)-1;
- bool bio[252][1<<16], da;
- int n, cost[5], mask[252], bt, crntmsk;
- string s[4];
- set <pair <int,int> > st[252];
- pair <int,int> p;
- void f(int cnt, int maska, int rj, int x, int y)
- {
- //cout << x << " " << y << " // " << rj << endl;
- if (((maska)&(jednako))==jednako) {
- //cout << "-----" << endl;
- st[cnt+1].insert(p=make_pair(rj,maska>>16));
- return;
- }
- if (x==3 && y==3) {
- return;
- }
- int msk=maska;
- if (x<3) f(cnt,msk,rj,x+1,y);
- else f(cnt,msk,rj,0,y+1);
- for (int i=1;i<5;i++) {
- if (x+i<=4) {
- bt=y*4+x;
- for (int j=0;j<i;j++) {
- for (int k=0;k<i;k++) {
- maska|=(1<<bt);
- bt++;
- }
- bt+=(4-i);
- }
- if (x<3) f(cnt,maska,rj+cost[i],x+1,y);
- else f(cnt,maska,rj+cost[i],0,y+1);
- }
- }
- return;
- }
- int main()
- {
- cin >> n;
- for (int i=1;i<5;i++) cin >> cost[i];
- for (int i=0;i<4;i++) {
- cin >> s[i];
- }
- for (int i=0;i<n;i++) {
- for (int j=0;j<4;j++) {
- if (s[j][i]=='*') mask[i/4]|=(1<<((i%4)*4+j));
- }
- }
- if (n%4) {
- bt=1<<15;
- da=1;
- for (int i=0;i<4-n%4;i++) {
- for (int j=0;j<4;j++) {
- mask[n/4]|=bt;
- bt--;
- }
- }
- }
- f(0,mask[0],0,0,0);
- for (int i=1;da?i<=n/4:i<n/4;i++) {
- for (set<pair<int,int> >::const_iterator it=st[i].begin();it!=st[i].end();it++) {
- if (!bio[i][it->second]) {
- bio[i][it->second]=1;
- crntmsk=mask[i];
- crntmsk|=(it->second);
- f(i,crntmsk,it->first,0,0);
- }
- }
- }
- set<pair<int,int> >::const_iterator it=st[da?n/4+1:n/4].begin();
- cout << it->first;
- }
- /*
- 15
- 2 5 11 16
- .**...****.....
- **.*.*******..*
- *.....*..**..**
- ..**..****.*...
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement