Guest User

Untitled

a guest
Nov 7th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. class config
  5. {
  6.     char save_ship[101];
  7.     char save_pull[101];
  8. public:
  9.     int cnt_win;
  10.     int cnt_lose;
  11.     bool operator < (const config& g2)
  12.     {
  13.         if (new_point > g2.new_point)
  14.             return true;
  15.         //if (cnt_win > g2.cnt_win)
  16.         //  return true;
  17.         //else if(cnt_win == g2.cnt_win)
  18.         //{
  19.         //  if (cnt_lose < g2.cnt_lose)
  20.         //      return true;
  21.         //  else
  22.         //      return false;
  23.         //}
  24.         return false;
  25.     }
  26.     char ship[101];
  27.     int cnt_ship;
  28.     char pull[101];
  29.     int cnt_pull;
  30.     int total_lost_ship;
  31.     int total_kill_ship;
  32.     int new_point;
  33.     int id_pull;
  34.     void rebuild()
  35.     {
  36.         memcpy(ship,save_ship,100);
  37.         memcpy(pull,save_pull,100);
  38.         cnt_pull = 10;
  39.         cnt_ship = 5;
  40.         id_pull = 0;
  41.     }
  42.     void read_ship()
  43.     {
  44.         memset(ship,0,sizeof ship);
  45.         int c;
  46.         char c1;
  47.         for (int i = 0; i < 5; i++)
  48.         {
  49.             scanf ("%d%c\n",&c,&c1);
  50.             c--;
  51.             c1 -= 'а';
  52.             ship[c * 10 + c1] = 1;
  53.         }
  54.     }
  55.     void read_pull()
  56.     {
  57.         memset(pull,0,sizeof pull);
  58.         int c;
  59.         char c1;
  60.         for (int i = 0; i < 10; i++)
  61.         {
  62.             scanf ("%d%c\n",&c,&c1);
  63.             c--;
  64.             c1 -= 'а';
  65.             pull[c * 10 + c1] = 1;
  66.         }
  67.  
  68.     }
  69.     config ()
  70.     {
  71.         new_point = 0;
  72.         total_lost_ship = 0;
  73.         total_kill_ship = 0;
  74.         cnt_win = 0;
  75.         cnt_lose = 0;
  76.         cnt_pull = 10;
  77.         cnt_ship = 5;
  78.         id_pull = 0;
  79.         memset (ship,0,sizeof ship);
  80.         memset (pull,0,sizeof pull);
  81.         for (int i = 0; i < 5; i++)
  82.         {
  83.             ship[i] = 1;
  84.         }
  85.         for (int i = 0; i < 10; i++)
  86.         {
  87.             pull[i] = 1;
  88.         }
  89.         for (int i = 0; i < 10000; i++)
  90.         {
  91.             int x = rand() % 100;
  92.             int y = rand() % 100;
  93.             int a = rand() % 100;
  94.             int b = rand() % 100;
  95.             swap(ship[x],ship[y]);
  96.             swap(pull[a],pull[b]);
  97.         }
  98.         random_shuffle(ship,ship + 100);
  99.         random_shuffle(pull,pull + 100);
  100.         memcpy(save_ship,ship,100);
  101.         memcpy(save_pull,pull,100);
  102.     }
  103.     pair<int,int> send_pull()
  104.     {
  105.         if (cnt_pull == 0)
  106.             return make_pair(-1,-1);
  107.         for ( ;id_pull < 100;)
  108.         {
  109.             if (pull[id_pull] == 1)
  110.             {
  111.                 pull[id_pull] = 0;
  112.                 cnt_pull--;
  113.                 return make_pair(id_pull / 10,id_pull % 10);
  114.             }
  115.             id_pull++;
  116.         }
  117.     }
  118.     bool accept_shut(const pair<int,int> &p)
  119.     {
  120.         int x = p.first,y = p.second;
  121.         int s = x * 10 + y;
  122.         if (ship[s] == 1)
  123.         {
  124.             ship[s] = 0;
  125.             cnt_ship--;
  126.             total_lost_ship++;
  127.             new_point -= 2;
  128.             return true; // попал
  129.         }
  130.         return false;
  131.     }
  132. };
  133. int who_win(config& a,config& b)
  134. {
  135.     if (a.cnt_ship < b.cnt_ship)
  136.     {
  137.         return 2; //b
  138.         b.cnt_win++;
  139.         a.cnt_lose++;
  140.     }
  141.     else if(a.cnt_ship == b.cnt_ship)
  142.         return 0;// a b
  143.     a.cnt_win++;
  144.     b.cnt_lose++;
  145.     return 1; //a
  146. }
  147. void play_game(config &a,config &b)
  148. {
  149.     for (int i = 0; i < 10; i++)
  150.     {
  151.         if (b.accept_shut(a.send_pull()))
  152.         {
  153.             a.total_kill_ship++;
  154.             a.new_point++;
  155.         }
  156.         if (a.accept_shut(b.send_pull()))
  157.         {
  158.             b.total_kill_ship++;
  159.             b.new_point++;
  160.         }
  161.     }
  162. }
  163. int main()
  164. {
  165.     freopen ("input.txt","r",stdin);
  166.     config a,b;
  167.     a.read_ship();
  168.     a.read_pull();
  169.     b.read_ship();
  170.     b.read_pull();
  171.     play_game(a,b);
  172.     int r = who_win(a,b);
  173.     if (r == 0)
  174.         cout << "DRAW";
  175.     else if (r == 1)
  176.         cout << "A";
  177.     else
  178.         cout << "B";
  179.  
  180.     //config gamers[1001];
  181.     //int gmr = 1000;
  182.     //for (int i = 0; i < gmr; i++)
  183.     //{
  184.     //  for (int j = 0; j < gmr; j++)
  185.     //  {
  186.     //      if (i != j)
  187.     //      {
  188.     //          play_game(gamers[i],gamers[j]);
  189.     //          who_win(gamers[i],gamers[j]);
  190.     //          gamers[i].rebuild();
  191.     //          gamers[j].rebuild();
  192.     //      }
  193.     //  }
  194.     //}
  195.     //freopen("output.txt","w",stdout);
  196.     //sort(gamers,gamers + gmr);
  197.     //cout << "table:\n";
  198.     //for (int i = 0; i < gmr; i++)
  199.     //{
  200.     //  printf ("%d: win %d, lose %d, total_kill_ship: %d, total_lost_ship %d\n ",i + 1,gamers[i].cnt_win,gamers[i].cnt_lose,gamers[i].total_kill_ship,gamers[i].total_lost_ship);
  201.     //}
  202.     //cout << "strategy of winner:\n";
  203.     //cout << "5 ships (x,y):\n";
  204.     //for (int i = 0; i < 100; i++)
  205.     //{
  206.     //  if (gamers[0].ship[i] == 1)
  207.     //  {
  208.     //      printf ("%d%c\n",i / 10 + 1,'а' + i % 10);
  209.     //  }
  210.     //}
  211.     //cout << "10 shuts (x,y):\n";
  212.     //for (int i = 0; i < 100; i++)
  213.     //{
  214.     //  if (gamers[0].pull[i] == 1)
  215.     //  {
  216.     //      printf ("%d%c\n",i / 10 + 1,'а' + i % 10);
  217.     //  }
  218.     //}
  219.     //cout << "\ntable 1 - ships situated:\n";
  220.     //for (int i = 0; i < 10; i++) {
  221.     //  for (int j = 0; j < 10; j++)
  222.     //  {
  223.     //      printf ("%d%c",gamers[0].ship[i * 10 + j]," \n"[j == 9]);
  224.     //  }
  225.     //}
  226.     //cout << "\ntable 1 - pulls situated:\n";
  227.     //for (int i = 0; i < 10; i++)
  228.     //{
  229.     //  for (int j = 0; j < 10; j++)
  230.     //  {
  231.     //      printf ("%d%c",gamers[0].pull[i * 10 + j]," \n"[j == 9]);
  232.     //  }
  233.     //}
  234.     return 0;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment