Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- class config
- {
- char save_ship[101];
- char save_pull[101];
- public:
- int cnt_win;
- int cnt_lose;
- bool operator < (const config& g2)
- {
- if (new_point > g2.new_point)
- return true;
- //if (cnt_win > g2.cnt_win)
- // return true;
- //else if(cnt_win == g2.cnt_win)
- //{
- // if (cnt_lose < g2.cnt_lose)
- // return true;
- // else
- // return false;
- //}
- return false;
- }
- char ship[101];
- int cnt_ship;
- char pull[101];
- int cnt_pull;
- int total_lost_ship;
- int total_kill_ship;
- int new_point;
- int id_pull;
- void rebuild()
- {
- memcpy(ship,save_ship,100);
- memcpy(pull,save_pull,100);
- cnt_pull = 10;
- cnt_ship = 5;
- id_pull = 0;
- }
- void read_ship()
- {
- memset(ship,0,sizeof ship);
- int c;
- char c1;
- for (int i = 0; i < 5; i++)
- {
- scanf ("%d%c\n",&c,&c1);
- c--;
- c1 -= 'а';
- ship[c * 10 + c1] = 1;
- }
- }
- void read_pull()
- {
- memset(pull,0,sizeof pull);
- int c;
- char c1;
- for (int i = 0; i < 10; i++)
- {
- scanf ("%d%c\n",&c,&c1);
- c--;
- c1 -= 'а';
- pull[c * 10 + c1] = 1;
- }
- }
- config ()
- {
- new_point = 0;
- total_lost_ship = 0;
- total_kill_ship = 0;
- cnt_win = 0;
- cnt_lose = 0;
- cnt_pull = 10;
- cnt_ship = 5;
- id_pull = 0;
- memset (ship,0,sizeof ship);
- memset (pull,0,sizeof pull);
- for (int i = 0; i < 5; i++)
- {
- ship[i] = 1;
- }
- for (int i = 0; i < 10; i++)
- {
- pull[i] = 1;
- }
- for (int i = 0; i < 10000; i++)
- {
- int x = rand() % 100;
- int y = rand() % 100;
- int a = rand() % 100;
- int b = rand() % 100;
- swap(ship[x],ship[y]);
- swap(pull[a],pull[b]);
- }
- random_shuffle(ship,ship + 100);
- random_shuffle(pull,pull + 100);
- memcpy(save_ship,ship,100);
- memcpy(save_pull,pull,100);
- }
- pair<int,int> send_pull()
- {
- if (cnt_pull == 0)
- return make_pair(-1,-1);
- for ( ;id_pull < 100;)
- {
- if (pull[id_pull] == 1)
- {
- pull[id_pull] = 0;
- cnt_pull--;
- return make_pair(id_pull / 10,id_pull % 10);
- }
- id_pull++;
- }
- }
- bool accept_shut(const pair<int,int> &p)
- {
- int x = p.first,y = p.second;
- int s = x * 10 + y;
- if (ship[s] == 1)
- {
- ship[s] = 0;
- cnt_ship--;
- total_lost_ship++;
- new_point -= 2;
- return true; // попал
- }
- return false;
- }
- };
- int who_win(config& a,config& b)
- {
- if (a.cnt_ship < b.cnt_ship)
- {
- return 2; //b
- b.cnt_win++;
- a.cnt_lose++;
- }
- else if(a.cnt_ship == b.cnt_ship)
- return 0;// a b
- a.cnt_win++;
- b.cnt_lose++;
- return 1; //a
- }
- void play_game(config &a,config &b)
- {
- for (int i = 0; i < 10; i++)
- {
- if (b.accept_shut(a.send_pull()))
- {
- a.total_kill_ship++;
- a.new_point++;
- }
- if (a.accept_shut(b.send_pull()))
- {
- b.total_kill_ship++;
- b.new_point++;
- }
- }
- }
- int main()
- {
- freopen ("input.txt","r",stdin);
- config a,b;
- a.read_ship();
- a.read_pull();
- b.read_ship();
- b.read_pull();
- play_game(a,b);
- int r = who_win(a,b);
- if (r == 0)
- cout << "DRAW";
- else if (r == 1)
- cout << "A";
- else
- cout << "B";
- //config gamers[1001];
- //int gmr = 1000;
- //for (int i = 0; i < gmr; i++)
- //{
- // for (int j = 0; j < gmr; j++)
- // {
- // if (i != j)
- // {
- // play_game(gamers[i],gamers[j]);
- // who_win(gamers[i],gamers[j]);
- // gamers[i].rebuild();
- // gamers[j].rebuild();
- // }
- // }
- //}
- //freopen("output.txt","w",stdout);
- //sort(gamers,gamers + gmr);
- //cout << "table:\n";
- //for (int i = 0; i < gmr; i++)
- //{
- // 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);
- //}
- //cout << "strategy of winner:\n";
- //cout << "5 ships (x,y):\n";
- //for (int i = 0; i < 100; i++)
- //{
- // if (gamers[0].ship[i] == 1)
- // {
- // printf ("%d%c\n",i / 10 + 1,'а' + i % 10);
- // }
- //}
- //cout << "10 shuts (x,y):\n";
- //for (int i = 0; i < 100; i++)
- //{
- // if (gamers[0].pull[i] == 1)
- // {
- // printf ("%d%c\n",i / 10 + 1,'а' + i % 10);
- // }
- //}
- //cout << "\ntable 1 - ships situated:\n";
- //for (int i = 0; i < 10; i++) {
- // for (int j = 0; j < 10; j++)
- // {
- // printf ("%d%c",gamers[0].ship[i * 10 + j]," \n"[j == 9]);
- // }
- //}
- //cout << "\ntable 1 - pulls situated:\n";
- //for (int i = 0; i < 10; i++)
- //{
- // for (int j = 0; j < 10; j++)
- // {
- // printf ("%d%c",gamers[0].pull[i * 10 + j]," \n"[j == 9]);
- // }
- //}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment