Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- std::vector<std::vector<int> > grid;
- std::vector<std::vector<int> > ships;
- long a,c,m;
- long r = 0;
- int lengths[10] = {4,3,3,2,2,2,1,1,1,1};
- void initialiseGrid(){
- std::vector<int> v;
- v.resize(10,0);
- for (int i = 0; i<10; i++){
- grid.push_back(v);
- }
- }
- bool isValidAndPlace(int x, int y, int length, bool horizontal){
- if (horizontal){
- if ((x+length-1)>9){
- return false;
- }
- int xbound1;
- int xbound2;
- if (x!=0){
- xbound1=x-1;
- } else {
- xbound1=x;
- }
- if (x+length<10){
- xbound2=x+length+1;
- } else {
- xbound2=x+length;
- }
- for (int xc = xbound1; xc<xbound2; xc++){
- if (grid[xc][y]==1){
- return false;
- }
- if (y+1<10){
- if (grid[xc][y+1]==1){
- return false;
- }
- }
- if (y-1>=0){
- if (grid[xc][y-1]==1){
- return false;
- }
- }
- }
- for (int xc = x; xc<x+length; xc++){
- grid[xc][y]=1;
- }
- std::vector<int> v;
- v.push_back(x);
- v.push_back(y);
- v.push_back(1);
- ships.push_back(v);
- return true;
- } else {
- if ((y+length-1)>9){
- return false;
- }
- int ybound1;
- int ybound2;
- if (y!=0){
- ybound1=y-1;
- } else {
- ybound1=y;
- }
- if (y+length<10){
- ybound2=y+length+1;
- } else {
- ybound2=y+length;
- }
- for (int yc = ybound1; yc<ybound2; yc++){
- if (grid[x][yc]==1){
- return false;
- }
- if (x+1<10){
- if (grid[x+1][yc]==1){
- return false;
- }
- }
- if (x-1>=0){
- if (grid[x-1][yc]==1){
- return false;
- }
- }
- }
- for (int yc = y; yc<y+length; yc++){
- grid[x][yc]=1;
- }
- std::vector<int> v;
- v.push_back(x);
- v.push_back(y);
- v.push_back(0);
- ships.push_back(v);
- return true;
- }
- }
- void placeShip(int length){
- r = (a*r+c)%m;
- int x = r%10;
- int y = ((r-(r%10))/10)%10;
- r = (a*r+c)%m;
- if (r%2==0){
- if (isValidAndPlace(x,y,length,true)){
- return;
- }
- } else {
- if (isValidAndPlace(x,y,length,false)){
- return;
- }
- }
- placeShip(length);
- }
- void viewGrid(){
- for (int y = 9; y>=0; y--){
- for (int x = 0; x<10; x++){
- std::cout<<grid[x][y];
- }
- std::cout<<std::endl;
- }
- }
- int main(int argc, char const *argv[])
- {
- initialiseGrid();
- std::cin>>a>>c>>m;
- for (int i = 0; i<10; i++){
- int length = lengths[i];
- placeShip(length);
- //std::cout<<"ship"<<std::endl;
- //viewGrid();
- }
- for (std::vector<int> v : ships){
- char x;
- if (v[2]==1){
- x='H';
- } else {
- x='V';
- }
- std::cout<<v[0]<<" "<<v[1]<<" "<<x<<std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement