Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.58 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<vector>
  3. #include<time.h>
  4. //#include "bitmap_image.hpp"
  5. #define pi (2*acos(0.0))
  6. using namespace std;
  7. class node;
  8. class node
  9. {
  10. public:
  11. string val;
  12. node* left_child;
  13. node* right_child;
  14. vector <int > h;
  15. vector <int > w;
  16. };
  17. vector <int > temp;
  18. vector <node> tree;
  19. vector <int > temp2;
  20. vector <node> tree2;
  21.  
  22. int area()
  23. {
  24. int pos=0;
  25. for(int i=0;i<temp2.size();i++)
  26. {
  27. pos=temp2[i];
  28. //cout<<temp2[i]<<"hau";
  29.  
  30. if(tree[pos].left_child==NULL)
  31. {
  32. if(tree[pos].right_child==NULL)
  33. {
  34. //cout<<pos<<"\n";
  35. continue;
  36. }
  37. }
  38. else if(tree[pos].left_child!=NULL)
  39. {
  40. if(tree[pos].right_child!=NULL)
  41. {
  42. cout<<tree[pos].val<<"\n";
  43. //cout<<pos<<"\n";
  44. /* int pos1,pos2;
  45. for(int j=0;j<tree.size();j++)
  46. {
  47. if(tree[j].val==tree[pos].left_child->val)
  48. {
  49. pos1=j;
  50. }
  51. }
  52. for(int j=0;j<tree.size();j++)
  53. {
  54. if(tree[j].val==tree[pos].right_child->val)
  55. {
  56. pos2=j;
  57. cout<<pos1<<" "<<pos2<<"\n";
  58. }
  59. }
  60. */
  61. if(tree[pos].val=="H")
  62. {
  63. for(int x=0;x<tree[pos].left_child->h.size();x++)
  64. {
  65. for(int y=0;y<tree[pos].right_child->h.size();y++)
  66. {
  67. tree[pos].h.push_back(tree[pos].left_child->h[x]+tree[pos].right_child->h[y]);
  68. }
  69. }
  70. for(int x=0;x<tree[pos].left_child->w.size();x++)
  71. {
  72. for(int y=0;y<tree[pos].right_child->w.size();y++)
  73. {
  74. if(tree[pos].left_child->w[x]>=tree[pos].right_child->w[y])
  75. {
  76. tree[pos].w.push_back(tree[pos].left_child->w[x]);
  77. }
  78. else
  79. {
  80. tree[pos].w.push_back(tree[pos].right_child->w[y]);
  81. }
  82. }
  83. }
  84. cout<<"FOR H: \n";
  85. for(int i=0;i<tree[pos].h.size();i++)
  86. {
  87. cout<<tree[pos].h[i]<<" "<<tree[pos].w[i]<<"\n";
  88. }
  89. cout<<"\n\n\n\n\n\n\n";
  90. //cout<<pos<<" "<<pos1<<" "<<pos2<<"\n";
  91. }
  92. else if(tree[pos].val=="V")
  93. {
  94. cout<<"dhuksi\n";
  95. cout<<tree[pos].left_child->val<<" "<<tree[pos].left_child->h.size()<<"\n";
  96. cout<<"tree2:\n";
  97. for(int i=0;i<tree.size();i++)
  98. {
  99. cout<<tree[i].val<<"\n";
  100. if(tree[i].left_child!=NULL)
  101. {
  102. cout<<tree[i].left_child->val<<" "<<tree[i].right_child->val<<"\n";
  103. /* for(int j=0;j<tree[i].left_child->h.size();j++)
  104. {
  105. cout<<tree[i].left_child->h[j]<<" "<<tree[i].w[j]<<"\n";
  106. }
  107. for(int j=0;j<tree[i].right_child->h.size();j++)
  108. {
  109. cout<<tree[i].right_child->h[j]<<" "<<tree[i].w[j]<<"\n";
  110. }
  111. */
  112. }
  113.  
  114. for(int j=0;j<tree[i].h.size();j++)
  115. {
  116. cout<<tree[i].h[j]<<" "<<tree[i].w[j]<<"\n";
  117. }
  118. }
  119.  
  120.  
  121. for(int x=0;x<tree[pos].left_child->w.size();x++)
  122. {
  123.  
  124. for(int y=0;y<tree[pos].right_child->w.size();y++)
  125. {
  126. cout<<tree[pos].left_child->w[x]+tree[pos].right_child->w[y]<<"\n";
  127. tree[pos].w.push_back(tree[pos].left_child->w[x]+tree[pos].right_child->w[y]);
  128. }
  129. }
  130. for(int x=0;x<tree[pos].left_child->h.size();x++)
  131. {
  132. for(int y=0;y<tree[pos].right_child->h.size();y++)
  133. {
  134. if(tree[pos].left_child->h[x]>=tree[pos].right_child->h[y])
  135. {
  136. tree[pos].h.push_back(tree[pos].left_child->h[x]);
  137. cout<<tree[pos].left_child->h[x]<<"\n";
  138. }
  139. else
  140. {
  141. tree[pos].h.push_back(tree[pos].right_child->h[y]);
  142. cout<<tree[pos].left_child->h[x]<<"\n";
  143. }
  144. }
  145. }
  146. //cout<<pos<<" " <<pos1<<" "<<pos2<<"\n";
  147. for(int i=0;i<tree[pos].h.size();i++)
  148. {
  149. cout<<"FOR V: \n";
  150. cout<<tree[pos].h[i]<<" "<<tree[pos].w[i]<<"\n";
  151. }
  152. cout<<"\n\n\n\n\n\n\n";
  153.  
  154. }
  155. /*for(int i=0;i<tree[pos].h.size();i++)
  156. {
  157. cout<<tree[pos].h[i]<<" "<<tree[pos].w[i]<<"\n";
  158. }
  159. cout<<"\n\n\n\n\n\n\n";
  160. */
  161. //cout<<tree[pos].w.size()<<"\n";
  162. int choice[tree[pos].h.size()];
  163. for(int t=0;t<tree[pos].h.size();t++)
  164. {
  165. choice[t]=1;
  166. }
  167. for(int k=0;k<tree[pos].h.size()-1;k++)
  168. {
  169.  
  170. for(int b=k+1;b<tree[pos].h.size();b++)
  171. {
  172. if(choice[b]!=0 && choice[k]!=0)
  173. {
  174. if(tree[pos].h[k]<=tree[pos].h[b] && tree[pos].w[k]<=tree[pos].w[b])
  175. {
  176. choice[b]=0;
  177. //tree[pos].h.erase (tree[pos].h.begin()+b);
  178. //tree[pos].w.erase (tree[pos].w.begin()+b);
  179. }
  180. else if(tree[pos].h[k]>=tree[pos].h[b] && tree[pos].w[k]>=tree[pos].w[b])
  181. {
  182. //cout<<k<<" ssss "<<b<<"\n";
  183. choice[k]=0;
  184. //tree[pos].h.erase (tree[pos].h.begin()+b);
  185. //tree[pos].w.erase (tree[pos].w.begin()+b);
  186. }
  187. }
  188.  
  189.  
  190. }
  191.  
  192.  
  193. }
  194. /*for(int t=0;t<tree[pos].h.size();t++)
  195. {
  196. cout<<choice[t]<<" ";
  197. }
  198. cout<<"\n";
  199. */
  200. vector<int> hold1;
  201. vector<int> hold2;
  202. for(int t=0;t<tree[pos].h.size();t++)
  203. {
  204. if(choice[t]==1)
  205. {
  206. hold1.push_back(tree[pos].h[t]);
  207. hold2.push_back(tree[pos].w[t]);
  208. }
  209. }
  210. tree[pos].h.clear();
  211. tree[pos].w.clear();
  212. tree[pos].h=hold1;
  213. tree[pos].w=hold2;
  214. cout<<tree[pos].w.size()<<" \n";
  215.  
  216. /* for(int i=0;i<tree[pos].h.size();i++)
  217. {
  218. cout<<tree[pos].w[i]<<" "<<tree[pos].h[i]<<"\n";
  219. }
  220. cout<<"\n\n\n\n\n\n\n";
  221. */
  222. cout<<"tree:\n";
  223. for(int i=0;i<tree.size();i++)
  224. {
  225. cout<<tree[i].val<<"\n";
  226. if(tree[i].left_child!=NULL)
  227. {
  228. cout<<tree[i].left_child->val<<" "<<tree[i].right_child->val<<"\n";
  229. }
  230.  
  231. for(int j=0;j<tree[i].h.size();j++)
  232. {
  233. cout<<tree[i].h[j]<<" "<<tree[i].w[j]<<"\n";
  234. }
  235. }
  236. }
  237. }
  238. }
  239.  
  240. pos=temp2[temp2.size()-1];
  241. // cout<<pos<<"\n";
  242. int area[tree[pos].h.size()];
  243. int barea=0;
  244. int hold3=0;
  245. for(int c=0;c<tree[pos].h.size();c++)
  246. {
  247. if(tree[pos].h[c]*tree[pos].w[c]>barea)
  248. {
  249. barea=tree[pos].h[c]*tree[pos].w[c];
  250. hold3=c;
  251. // cout<<barea<<"\n";
  252. }
  253. }
  254. //cout<<barea;
  255. return barea;
  256. }
  257. void postorder(int m,int sz)
  258. {
  259.  
  260. //cout<<m<<" "<<tree[m].val<<" "<<tree[m].left_child<<" "<<tree[m].right_child<<"\n";
  261. if(tree[m].left_child!= NULL)
  262. {
  263. postorder(2*m+1,sz);
  264.  
  265. }
  266. if(tree[m].right_child!=NULL)
  267. {
  268. postorder(2*m+2,sz);
  269.  
  270. }
  271. else
  272. {
  273. temp.push_back(m);
  274. // cout<<tree[m].val<<"\n";
  275. return ;
  276. }
  277. temp.push_back(m);
  278. //cout<<tree[m].val<<"\n";
  279. return ;
  280. }
  281. void postorder2(int m,int sz)
  282. {
  283.  
  284. //cout<<m<<" "<<tree[m].val<<" "<<tree[m].left_child<<" "<<tree[m].right_child<<"\n";
  285. if(tree[m].left_child!=NULL)
  286. {
  287. postorder2(2*m+1,sz);
  288.  
  289. }
  290. if(tree[m].right_child!=NULL)
  291. {
  292. postorder2(2*m+2,sz);
  293.  
  294. }
  295. else
  296. {
  297. temp2.push_back(m);
  298. // cout<<tree[m].val<<"\n";
  299. return ;
  300. }
  301. temp2.push_back(m);
  302. //cout<<tree[m].val<<"\n";
  303. return ;
  304. }
  305. void algorithm()
  306. {
  307. int To,r,efsilon;
  308. //cin>>Eo;
  309. for(int i=0;i<tree.size();i++)
  310. {
  311. node tama;
  312. //tree2[i].h.clear();
  313. tama.h=tree[i].h;
  314. //cout<<"tama";
  315. tama.left_child=tree[i].left_child;
  316. tama.right_child=tree[i].right_child;
  317. tama.val=tree[i].val;
  318. //tree2[i].w.clear();
  319. tama.w=tree[i].w;
  320. tree2.push_back(tama);
  321.  
  322. }
  323. //cout<<tree2.size()<<"\n ";
  324. for(int i=0;i<tree2.size();i++)
  325. {
  326. //cout<<tree2[i].val<<" "<<tree2[i].left_child<<" "<<tree2[i].right_child<<"\n";
  327. for(int j=0;j<tree2[i].h.size();j++)
  328. {
  329. //cout<<tree2[i].h[j]<<" "<<tree2[i].w[j]<<"\n";
  330. }
  331. }
  332.  
  333. temp2.clear();
  334.  
  335. postorder2(0,tree.size());
  336.  
  337. To=1;
  338. r=2;
  339. efsilon=1.5;
  340.  
  341. // cin>>To;
  342. //cin>>r;
  343. //cin>>efsilon;
  344. vector <int> Ebest;
  345. vector <int> Eo;
  346. Eo=temp2;
  347. int T=To;
  348. Ebest=temp2;
  349. for(int b=0;b<temp2.size();b++)
  350. {
  351. // cout<<Eo[b]<<" "<<Ebest[b]<<"\n";
  352. }
  353. int oldarea=area();
  354. int bestcost=oldarea;
  355. cout<<oldarea<<" "<<bestcost<<"\n";
  356.  
  357. vector<node> tree4;
  358. vector<int > temp4;
  359. int pp=1;
  360. do
  361. {
  362. if(pp!=1)
  363. {
  364. for(int i=0;i<tree4.size();i++)
  365. {
  366. tree[i].h.clear();
  367. tree[i].h=tree4[i].h;
  368. tree[i].left_child=tree4[i].left_child;
  369. tree[i].right_child=tree4[i].right_child;
  370. tree[i].val=tree4[i].val;
  371. tree[i].w.clear();
  372. tree[i].w=tree4[i].w;
  373.  
  374. }
  375.  
  376.  
  377.  
  378. temp2.clear();
  379. temp2=temp4;
  380. pp++;
  381. }
  382.  
  383.  
  384. int Op=rand()%1;
  385. //Op=2;
  386. if(Op==0)
  387. {
  388. int i,j;
  389. i=rand()%temp2.size();
  390. while(tree[temp2[i]].val=="V" || tree[temp2[i]].val=="H")
  391. {
  392. i=rand()%temp2.size();
  393. }
  394. int j1=rand()%2;
  395. if(j1==0)
  396. {
  397. if(i==temp2.size()-1)
  398. {
  399. int k=i-1;
  400. while(tree[temp2[k]].val=="V" || tree[temp2[k]].val=="H")
  401. {
  402. k--;
  403. }
  404. int tempu;
  405. tempu=temp2[i];
  406. temp2[i]=temp2[k];
  407. temp2[k]=tempu;
  408. }
  409. else
  410. {
  411. int k=i+1;
  412. int flag=0;
  413. while(tree[temp2[k]].val=="V" || tree[temp2[k]].val=="H")
  414. {
  415. if(k==temp2.size()-1)
  416. {
  417. flag=1;
  418. break;
  419. }
  420.  
  421. k++;
  422. }
  423. if(k==temp2.size()-1 && flag==1)
  424. {
  425. k=i-1;
  426. int flag=0;
  427. while(tree[temp2[k]].val=="V" || tree[temp2[k]].val=="H")
  428. {
  429. k--;
  430. }
  431. }
  432. int tempu;
  433. tempu=temp2[i];
  434. temp2[i]=temp2[k];
  435. temp2[k]=tempu;
  436. }
  437.  
  438. }
  439. if(j1==1)
  440. {
  441. if(i==0)
  442. {
  443. int k=i+1;
  444. while(tree[temp2[k]].val=="V" || tree[temp2[k]].val=="H")
  445. {
  446. k++;
  447. }
  448. int tempu;
  449. tempu=temp2[i];
  450. temp2[i]=temp2[k];
  451. temp2[k]=tempu;
  452. }
  453. else
  454. {
  455. int k=i-1;
  456. //int flag=0;
  457. while(tree[temp2[k]].val=="V" || tree[temp2[k]].val=="H")
  458. {
  459. k--;
  460. }
  461. int tempu;
  462. tempu=temp2[i];
  463. temp2[i]=temp2[k];
  464. temp2[k]=tempu;
  465. }
  466. }
  467.  
  468. }
  469. else if(Op==1)
  470. {
  471. int i,j;
  472. i=rand()%temp2.size();
  473. while(!(tree[temp2[i]].val=="V" || tree[temp2[i]].val=="H"))
  474. {
  475. i=rand()%temp2.size();
  476. }
  477.  
  478. if(tree[temp2[i]].val=="H")
  479. {
  480. tree[temp2[i]].val="V";
  481. }
  482. else if(tree[temp2[i]].val=="V")
  483. {
  484. tree[temp2[i]].val="H";
  485. }
  486.  
  487. int k1=i+1;
  488. //vactor<int > invert;
  489. while(k1 <temp2.size() && (tree[temp2[k1]].val=="V" || tree[temp2[k1]].val=="H"))
  490. {
  491. if(tree[temp2[k1]].val=="H")
  492. {
  493. tree[temp2[k1]].val="V";
  494. }
  495. else if(tree[temp2[k1]].val=="V")
  496. {
  497. tree[temp2[k1]].val="H";
  498. }
  499. k1++;
  500. }
  501. //cout<<k1<<" ";//k--;
  502. k1=i-1;
  503. while((tree[temp2[k1]].val=="V" || tree[temp2[k1]].val=="H") && k1>=0)
  504. {
  505. if(tree[temp2[k1]].val=="H")
  506. {
  507. tree[temp2[k1]].val="V";
  508. }
  509. else if(tree[temp2[k1]].val=="V")
  510. {
  511. tree[temp2[k1]].val="H";
  512. }
  513. k1--;
  514. }
  515.  
  516. }
  517. else if(Op==2)
  518. {
  519. //bool done=false;
  520. for(int ii=0;ii<50;ii++)
  521. {
  522. int i,j;
  523. i=1+rand()%(temp2.size()-3);
  524. if((tree[temp2[i]].val=="H" || tree[temp2[i]].val=="V") && !(tree[temp2[i+1]].val=="H" || tree[temp2[i+1]].val=="V"))
  525. {
  526.  
  527. if(tree[temp2[i]].val!=tree[temp2[i+2]].val)
  528. {
  529. int tmp;
  530. tmp=temp2[i];
  531. temp2[i]=temp2[i+1];
  532. temp2[i+1]=tmp;
  533. break;
  534. }
  535.  
  536.  
  537. }
  538. if(!(tree[temp2[i]].val=="H" || tree[temp2[i]].val=="V") && (tree[temp2[i+1]].val=="H" || tree[temp2[i+1]].val=="V"))
  539. {
  540.  
  541. if(tree[temp2[i-1]].val!=tree[temp2[i+1]].val)
  542. {
  543. vector<int > tmp2;
  544. tmp2=temp2;
  545. int tmp;
  546. tmp=tmp2[i];
  547. tmp2[i]=tmp2[i+1];
  548. tmp2[i+1]=tmp;
  549. // cout<<"vitre ";
  550. for(int yy=0;yy<tmp2.size();yy++)
  551. {
  552. // cout<<tree[tmp2[yy]].val<<" ";
  553. }
  554. // cout<<"\n";
  555. int count1=0;
  556. int count2=0;
  557. int flag=0;
  558. for(int i=0;i<tmp2.size();i++)
  559. {
  560. count1=0;
  561. count2=0;
  562. for(int j=0;j<=i;j++)
  563. {
  564. if(tree[tmp2[j]].val=="H")
  565. {
  566. count1++;
  567. }
  568. else if(tree2[tmp2[j]].val=="V")
  569. {
  570. count1++;
  571. }
  572. else
  573. {
  574. count2++;
  575. }
  576.  
  577. }
  578. // cout<<count1<<" "<<count2<<"\n";
  579. if(count1>=count2)
  580. {
  581. flag=1;
  582. //cout<<flag<<" ";
  583. break;
  584. }
  585.  
  586. }
  587. if(flag==0)
  588. {
  589. temp2=tmp2;
  590. // done=true;
  591. break;
  592. }
  593. }
  594. }
  595. for(int yy=0;yy<temp2.size();yy++)
  596. {
  597. // cout<<tree[temp2[yy]].val<<" ";
  598. }
  599. // cout<<"\n";
  600. }
  601. }
  602. for(int yy=0;yy<temp2.size();yy++)
  603. {
  604. cout<<tree[temp2[yy]].val<<" ";
  605. }
  606. cout<<"\n";
  607.  
  608.  
  609.  
  610. vector<node*> tree3;
  611. vector<int>temp3;
  612. stack <node*> newtree;
  613. for(int m=0;m<temp2.size();m++)
  614. {
  615. if(tree[temp2[m]].val!="V" && tree[temp2[m]].val!="H")
  616. {
  617. node *tmp;
  618. tmp=new node;
  619. tmp->val=tree[temp2[m]].val;
  620. // node tt1,tt2;
  621. // tt1.val="\0";
  622. //tt2.val="\0";
  623. tmp->left_child=NULL;
  624. tmp->right_child=NULL;
  625. for(int n=0;n<tree[temp2[m]].w.size();n++)
  626. {
  627. tmp->w.push_back(tree[temp2[m]].w[n]);
  628.  
  629. }
  630. for(int n=0;n<tree[temp2[m]].w.size();n++)
  631. {
  632. tmp->h.push_back(tree[temp2[m]].h[n]);
  633.  
  634. }
  635. tree3.push_back(tmp);
  636. newtree.push(tmp);
  637. }
  638. else
  639. {
  640. // cout << "New Else " << endl;
  641. node *tmp;
  642. node *tmp2;
  643. node *tmp3;
  644. tmp= new node;
  645. tmp2= new node;
  646. tmp3= new node;
  647. tmp->val=tree[temp2[m]].val;
  648. //int sz1=newtree.size();
  649. tmp2=newtree.top();
  650. //cout<< "right child " << tmp2->val<<" s ";
  651. newtree.pop();
  652. tmp->right_child=tmp2;
  653. //int sz2=newtree.size();
  654. tmp3=newtree.top();
  655. //cout << "left child" <<tmp3->val<<"\n";
  656. newtree.pop();
  657. tmp->left_child=tmp3;
  658. //cout<<tmp->left_child->val<<" vvvvv \n";
  659.  
  660. //cout<<tmp->left_child->val<<" "<<tmp->right_child->val<<"\n";
  661. tree3.push_back(tmp);
  662. newtree.push(tmp);
  663.  
  664.  
  665. //cout<<newtree[newtree.size()-1].left_child->val<<" "<<newtree[newtree.size()-1].right_child->val<<"\n";
  666. // cout<<tmp->left_child->val<<" "<<tmp->right_child->val<<"\n";
  667.  
  668. // cout<<"current tree:\n";
  669.  
  670.  
  671. //cout<<"\n";
  672. //cout<<"current vector:\n";
  673.  
  674. }
  675.  
  676. temp3.push_back(m);
  677. }
  678.  
  679.  
  680.  
  681. for(int i=0;i<tree.size();i++)
  682. {
  683. tree[i].h.clear();
  684. tree[i].h=tree3[i]->h;
  685. tree[i].left_child=tree3[i]->left_child;
  686. tree[i].right_child=tree3[i]->right_child;
  687. if(tree[i].left_child!=NULL)
  688. {
  689. // cout<<tree3[i]->left_child->val<<" ";
  690. // cout<<tree3[i]->right_child->val<<"\n";
  691. }
  692.  
  693. tree[i].val=tree3[i]->val;
  694. tree[i].w.clear();
  695. tree[i].w=tree3[i]->w;
  696.  
  697. }
  698. temp2.clear();
  699. temp2=temp3;
  700.  
  701. for(int i=0;i<tree.size();i++)
  702. {
  703. cout<<tree[i].val<<"\n";
  704. if(tree[i].left_child!=NULL)
  705. {
  706. cout<<tree[i].left_child->val<<" "<<tree[i].right_child->val<<"\n";
  707. for(int j=0;j<tree[i].left_child->h.size();j++)
  708. {
  709. cout<<tree[i].left_child->h[j]<<" "<<tree[i].w[j]<<"\n";
  710. }
  711. for(int j=0;j<tree[i].right_child->h.size();j++)
  712. {
  713. cout<<tree[i].right_child->h[j]<<" "<<tree[i].w[j]<<"\n";
  714. }
  715. }
  716.  
  717. for(int j=0;j<tree[i].h.size();j++)
  718. {
  719. cout<<tree[i].h[j]<<" "<<tree[i].w[j]<<"\n";
  720. }
  721. }
  722. for(int g=0;g<newtree.size();g++)
  723. {
  724. // cout<<newtree[g]->val<<"\n";
  725. }
  726. //cout<<"\n";
  727.  
  728.  
  729. for(int yy=0;yy<temp2.size();yy++)
  730. {
  731. cout<<temp2[yy]<<" ";
  732. }
  733. cout<<"\n";
  734.  
  735.  
  736. /* int newarea=area();
  737.  
  738. int difcost=newarea-oldarea;
  739.  
  740. cout<<difcost<<" "<<newarea<<" "<<oldarea<<"cost\n";
  741. if(difcost<0)
  742. {
  743. tree4=tree;
  744.  
  745. temp4.clear();
  746. temp4=temp2;
  747.  
  748. if(newarea<bestcost)
  749. {
  750. Ebest.clear();
  751. Ebest=temp2;
  752. }
  753. cout<<oldarea<<" ber hoisi\n"<<newarea<<"\n";
  754. oldarea=newarea;
  755. for(int i=0;i<tree4.size();i++)
  756. {
  757. cout<<tree4[i].val<<"\n";
  758. if(tree4[i].left_child!=NULL)
  759. {
  760. cout<<tree4[i].left_child->val<<" "<<tree4[i].right_child->val<<"\n";
  761. }
  762.  
  763. for(int j=0;j<tree4[i].h.size();j++)
  764. {
  765. cout<<tree4[i].h[j]<<" "<<tree4[i].w[j]<<"\n";
  766. }
  767. }
  768.  
  769. }
  770. else if(difcost>=0)
  771. {
  772. double test=-difcost/T;
  773. double enlog=pow(2.71828,test);
  774. double f=(rand()%100)/100;
  775. if(enlog>f)
  776. {
  777. for(int i=0;i<tree.size();i++)
  778. {
  779. tree4[i].h.clear();
  780. tree4[i].h=tree[i].h;
  781. tree4[i].left_child=tree[i].left_child;
  782. tree4[i].right_child=tree[i].right_child;
  783. tree4[i].val=tree[i].val;
  784. tree4[i].w.clear();
  785. tree4[i].w=tree[i].w;
  786.  
  787. }
  788. temp4.clear();
  789. temp4=temp2;
  790.  
  791. oldarea=newarea;
  792. }
  793.  
  794. }
  795.  
  796.  
  797. T=r*T;
  798. */
  799. }while (T > efsilon);
  800.  
  801.  
  802. return;
  803.  
  804. }
  805.  
  806. int main()
  807. {
  808. ifstream myfile ("C:\\Users\\world co\\Desktop\\Hardware\\input.txt");
  809. string line1;
  810. vector < string > leaf;
  811. vector <int > width;
  812. vector <int > height;
  813. int k1=0;
  814. while(getline(myfile,line1))
  815. {
  816. string token1;
  817. istringstream ss;
  818. ss.clear();
  819. ss.str(line1);
  820.  
  821. int j=0;
  822. while(getline(ss,token1,' '))
  823. {
  824. if(j==0)
  825. {
  826. string omuk=token1.c_str();
  827. leaf.push_back(omuk);
  828. }
  829. if(j==1)
  830. {
  831. int tomuk=atoi(token1.c_str());
  832. width.push_back(tomuk);
  833. }
  834. if(j==2)
  835. {
  836. int tomuk2=atoi(token1.c_str());
  837. height.push_back(tomuk2);
  838. }
  839.  
  840. //break;
  841.  
  842.  
  843. j++;
  844. }
  845. //cout<<leaf[k1]<<" "<<width[k1]<<" "<<height[k1]<<"\n";
  846. k1++;
  847. }
  848. //cout<<k1<<"\n";
  849.  
  850. //char leaf2[k1];
  851. for(int i=0;i<k1;i++)
  852. {
  853. //int i;
  854. //char buffer [33];
  855.  
  856. //leaf2[i]= leaf[i]+48;
  857. }
  858.  
  859. int cc=2*k1-1-k1;
  860. for(int i=0;i<cc;i++)
  861. {
  862. node temp;
  863. int choice=rand()%2;
  864. if(choice==0)
  865. {
  866. temp.val="H";
  867.  
  868. }
  869. else if(choice==1)
  870. {
  871. temp.val="V";
  872. }
  873. tree.push_back(temp);
  874. }
  875. for(int i=0;i<k1;i++)
  876. {
  877. node temp;
  878. temp.val=leaf[i];
  879. temp.h.push_back(height[i]);
  880. temp.w.push_back(width[i]);
  881. temp.w.push_back(height[i]);
  882. temp.h.push_back(width[i]);
  883. // node tt1,tt2;
  884. // tt1.val="\0";
  885. // tt2.val="\0";
  886. temp.left_child=NULL;
  887. temp.right_child=NULL;
  888. tree.push_back(temp);
  889. }
  890.  
  891. for(int i=0;i<cc;i++)
  892. {
  893.  
  894. tree[i].left_child=&tree[2*i+1];
  895. tree[i].right_child=&tree[2*i+2];
  896. // cout<<tree[i].left_child<<" "<<tree[i].right_child<<"\n";
  897. }
  898.  
  899. for(int i=0;i<k1/2;i++)
  900. {
  901. if(tree[i].val==tree[2*i+2].val)
  902. {
  903. if(tree[2*i+2].val=="V")
  904. tree[2*i+2].val="H";
  905. else
  906. tree[2*i+2].val="V";
  907. }
  908. }
  909. for(int i=0;i<cc+k1;i++)
  910. {
  911.  
  912. //tree[i].left_child=tree[2*i+1].val;
  913. //tree[i].right_child,tree[2*i+2].val;
  914. // cout<<tree[i].val<<"\n";
  915. //cout<<tree[i].left_child<<" "<<tree[i].right_child<<"\n";
  916. }
  917. //vector <string> tempp;
  918. temp.clear();
  919. postorder(0,tree.size());
  920. for(int i=0;i<temp.size();i++)
  921. {
  922. //cout<<temp[i]<<"\n";
  923. }
  924. for(int i=0;i<tree.size();i++)
  925. {
  926. //cout<<tree[i].val<<" "<<tree[i].left_child<<" "<<tree[i].right_child<<"\n";
  927. for(int j=0;j<tree[i].h.size();j++)
  928. {
  929. // cout<<tree[i].h[j]<<" "<<tree[i].w[j]<<"\n";
  930. }
  931. }
  932. //temp2.clear();
  933. //postorder2(0,tree.size());
  934. //cout<<"thik";
  935. //int a=area();
  936. //cout<<a;
  937. algorithm();
  938.  
  939.  
  940. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement