Advertisement
bartekltg

wie

Dec 18th, 2022
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iterator>
  4. #include <algorithm>
  5. #include <tuple>
  6. #include <stack>
  7. #include <iomanip>
  8. #include <unordered_map>
  9. #include <bitset>
  10.  
  11. using namespace std;
  12.  
  13. // suma mozliwych ruchow ze wszytkich stanow przy danej liczbie pionków rozmiarze planszy,
  14. uint64_t ltable [8][8][8]={
  15.     {
  16.     {0, 2, 4, 6, 8, 10, 12, 14, },
  17.     {2, 8, 14, 20, 26, 32, 38, 44, },
  18.     {4, 14, 24, 34, 44, 54, 64, 74, },
  19.     {6, 20, 34, 48, 62, 76, 90, 104, },
  20.     {8, 26, 44, 62, 80, 98, 116, 134, },
  21.     {10, 32, 54, 76, 98, 120, 142, 164, },
  22.     {12, 38, 64, 90, 116, 142, 168, 194, },
  23.     {14, 44, 74, 104, 134, 164, 194, 224, },
  24.  
  25.     },
  26.     {
  27.     {0, 0, 4, 12, 24, 40, 60, 84, },
  28.     {0, 16, 56, 120, 208, 320, 456, 616, },
  29.     {4, 56, 168, 340, 572, 864, 1216, 1628, },
  30.     {12, 120, 340, 672, 1116, 1672, 2340, 3120, },
  31.     {24, 208, 572, 1116, 1840, 2744, 3828, 5092, },
  32.     {40, 320, 864, 1672, 2744, 4080, 5680, 7544, },
  33.     {60, 456, 1216, 2340, 3828, 5680, 7896, 10476, },
  34.     {84, 616, 1628, 3120, 5092, 7544, 10476, 13888, },
  35.  
  36.     },
  37.     {
  38.     {0, 0, 0, 6, 24, 60, 120, 210, },
  39.     {0, 8, 84, 300, 728, 1440, 2508, 4004, },
  40.     {0, 84, 504, 1530, 3432, 6480, 10944, 17094, },
  41.     {6, 300, 1530, 4368, 9486, 17556, 29250, 45240, },
  42.     {24, 728, 3432, 9486, 20240, 37044, 61248, 94202, },
  43.     {60, 1440, 6480, 17556, 37044, 67320, 110760, 169740, },
  44.     {120, 2508, 10944, 29250, 61248, 110760, 181608, 277614, },
  45.     {210, 4004, 17094, 45240, 94202, 169740, 277614, 423584, },
  46.  
  47.     },
  48.     {
  49.     {0, 0, 0, 0, 8, 40, 120, 280, },
  50.     {0, 0, 56, 400, 1456, 3840, 8360, 16016, },
  51.     {0, 56, 840, 4080, 12584, 30240, 62016, 113960, },
  52.     {0, 400, 4080, 17472, 50592, 117040, 234000, 422240, },
  53.     {8, 1456, 12584, 50592, 141680, 321048, 632896, 1130424, },
  54.     {40, 3840, 30240, 117040, 321048, 718080, 1402960, 2489520, },
  55.     {120, 8360, 62016, 234000, 632896, 1402960, 2724120, 4811976, },
  56.     {280, 16016, 113960, 422240, 1130424, 2489520, 4811976, 8471680, },
  57.  
  58.     },
  59.     {
  60.     {0, 0, 0, 0, 0, 10, 60, 210, },
  61.     {0, 0, 14, 300, 1820, 6720, 18810, 44044, },
  62.     {0, 14, 840, 7140, 31460, 98280, 248064, 541310, },
  63.     {0, 300, 7140, 48048, 189720, 555940, 1345500, 2850120, },
  64.     {0, 1820, 31460, 189720, 708400, 2006550, 4746720, 9891210, },
  65.     {10, 6720, 98280, 555940, 2006550, 5565120, 12977380, 26762340, },
  66.     {60, 18810, 248064, 1345500, 4746720, 12977380, 29965320, 61352694, },
  67.     {210, 44044, 541310, 2850120, 9891210, 26762340, 61352694, 124957280, },
  68.  
  69.     },
  70.     {
  71.     {0, 0, 0, 0, 0, 0, 12, 84, },
  72.     {0, 0, 0, 120, 1456, 8064, 30096, 88088, },
  73.     {0, 0, 504, 8568, 56628, 235872, 744192, 1948716, },
  74.     {0, 120, 8568, 96096, 531216, 2001384, 5920200, 14820624, },
  75.     {0, 1456, 56628, 531216, 2691920, 9631440, 27530976, 67260228, },
  76.     {0, 8064, 235872, 2001384, 9631440, 33390720, 93437136, 224803656, },
  77.     {12, 30096, 744192, 5920200, 27530976, 93437136, 257701752, 613526940, },
  78.     {84, 88088, 1948716, 14820624, 67260228, 224803656, 613526940, 1449504448, },
  79.  
  80.     },
  81.     {
  82.     {0, 0, 0, 0, 0, 0, 0, 14, },
  83.     {0, 0, 0, 20, 728, 6720, 35112, 132132, },
  84.     {0, 0, 168, 7140, 75504, 432432, 1736448, 5521362, },
  85.     {0, 20, 7140, 144144, 1150968, 5670588, 20720700, 61752600, },
  86.     {0, 728, 75504, 1150968, 8075760, 36920520, 128477888, 369931254, },
  87.     {0, 6720, 432432, 5670588, 36920520, 161388480, 545049960, 1536158316, },
  88.     {0, 35112, 1736448, 20720700, 128477888, 545049960, 1803912264, 5010470010, },
  89.     {14, 132132, 5521362, 61752600, 369931254, 1536158316, 5010470010, 13770292256, },
  90.  
  91.     },
  92.     {
  93.     {0, 0, 0, 0, 0, 0, 0, 0, },
  94.     {0, 0, 0, 0, 208, 3840, 30096, 151008, },
  95.     {0, 0, 24, 4080, 75504, 617760, 3224832, 12620256, },
  96.     {0, 0, 4080, 164736, 1973088, 12961344, 59202000, 211723200, },
  97.     {0, 208, 75504, 1973088, 19612560, 116035920, 495557568, 1691114304, },
  98.     {0, 3840, 617760, 12961344, 116035920, 645553920, 2647385520, 8778047520, },
  99.     {0, 30096, 3224832, 59202000, 495557568, 2647385520, 10565771832, 34357508640, },
  100.     {0, 151008, 12620256, 211723200, 1691114304, 8778047520, 34357508640, 110162338048, },
  101.  
  102.     },
  103.  
  104.     };
  105.  
  106.  
  107. int64_t ruch(uint64_t plansza, int pozycja,int n, int m){
  108.     //4 normalne ruchy pionka minus liczba kolizji wprowadzamy jesli na planszy postawimy
  109.     //pionek na pozycji. kolizji nie powinno byc wieksze niz 8.
  110.     int aku=0;
  111.     if (pozycja <m )
  112.         aku++;
  113.     else
  114.         if ((1ull<<(pozycja-m))&plansza)
  115.             aku+=2;
  116.  
  117.     if ((pozycja%m)==0 )
  118.         aku++;
  119.     else
  120.         if ((1ull<<(pozycja-1))&plansza)
  121.             aku+=2;
  122.  
  123.     if (pozycja >=m*n-m )
  124.         aku++;
  125.     else
  126.         if ((1ull<<(pozycja+m))&plansza)
  127.             aku+=2;
  128.  
  129.     if ((pozycja%m)==m-1 )
  130.         aku++;
  131.     else
  132.         if ((1ull<<(pozycja+1))&plansza)
  133.             aku+=2;
  134.     return 4-aku;
  135. }
  136.  
  137. uint64_t dozwolonych_ruchow(  int n, int m, uint64_t plansza, int pozycja, int pionkow, int max_pionkow, uint64_t ruchow ){
  138.     uint64_t aku=0;
  139.     if (pionkow>0){
  140.         ruchow+=ruch(plansza, pozycja,n,m);
  141.         plansza+= (1ull<<pozycja);
  142.     }
  143.     if (pionkow<max_pionkow)
  144.         for (int i=pozycja+1; i<=n*m - (max_pionkow-pionkow); i++ ){
  145.             aku+=dozwolonych_ruchow(n,m,plansza ,i,pionkow+1,max_pionkow,ruchow);
  146.         }
  147.     else{
  148.         aku=ruchow;
  149.     }
  150.     return aku;
  151. }
  152.  
  153. void generuj_tabelke(){
  154.     // suma mozliwych ruchow ze wszytkich stanow przy danym rozmiarze i liczbie pionkow,
  155.     //do przygotowania magicznej tabelki
  156.  
  157.     cout<<"{\n";
  158.     for (int pionkow =1; pionkow<=8; pionkow++){
  159.         array<array<int64_t,9>, 9> tab;
  160.  
  161.         for (int n=1; n<=8; n++){
  162.             for (int m=n; m<=8;m++){
  163.                 int64_t wyn = dozwolonych_ruchow(n,m,0,-1,0,pionkow,0);
  164.                 tab[m][n]=wyn;
  165.                 tab[n][m]=wyn;
  166.             }
  167.         }
  168.         cout<<"{\n";
  169.         for (int n=1; n<=8; n++){
  170.             cout<<"{";
  171.             copy(begin(tab[n])+1,end(tab[n]),ostream_iterator<int64_t>(cout, ", "));
  172.             cout<<"},\n";
  173.         }
  174.         cout<<"\n},\n";
  175.     }
  176.     cout<<"\n};\n";
  177.  
  178. }
  179.  
  180. int main()
  181. {
  182.     int inP=0, outP=0;//parzystosci
  183.  
  184.     int n,m;
  185.     cin>>n>>m;
  186.     if (n==42){// :)
  187.         generuj_tabelke();
  188.         return 0;
  189.     }
  190.  
  191.     int pionkow =0;
  192.  
  193.     for (int y=0; y<n; y++){
  194.         for (int x=0; x<m;x++){
  195.             char c;
  196.             cin>>c;
  197.             if (c=='O'){
  198.                 inP = (inP + y + x )%2;
  199.                 pionkow++;
  200.             }
  201.         }
  202.     }
  203.     int64_t ruchow=0;
  204.     uint64_t plansza=0;
  205.     int pozycja =0;
  206.     for (int y=0; y<n; y++){
  207.         for (int x=0; x<m;x++){
  208.             char c;
  209.             cin>>c;
  210.             if (c=='O'){
  211.                 outP = (outP + y + x )%2;
  212.                 ruchow += ruch(plansza, pozycja, n,m);
  213.                 plansza+= (1ull<<pozycja);
  214.             }
  215.             pozycja++;
  216.         }
  217.     }
  218.  
  219.     if (inP==outP){
  220.         cout<< std::fixed << std::setprecision(15) <<  ruchow*2.0 / (double)ltable[pionkow-1][n-1][m-1]<<endl;
  221.     }else
  222.         cout<<0<<endl;
  223.  
  224.     return 0;
  225. }
  226.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement