Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. std::vector<std::vector<int> > grid;
  5. std::vector<std::vector<int> > ships;
  6. long a,c,m;
  7. long r = 0;
  8.  
  9. int lengths[10] = {4,3,3,2,2,2,1,1,1,1};
  10.  
  11. void initialiseGrid(){
  12. std::vector<int> v;
  13. v.resize(10,0);
  14. for (int i = 0; i<10; i++){
  15. grid.push_back(v);
  16. }
  17. }
  18.  
  19. bool isValidAndPlace(int x, int y, int length, bool horizontal){
  20. if (horizontal){
  21. if ((x+length-1)>9){
  22. return false;
  23. }
  24. int xbound1;
  25. int xbound2;
  26. if (x!=0){
  27. xbound1=x-1;
  28. } else {
  29. xbound1=x;
  30. }
  31. if (x+length<10){
  32. xbound2=x+length+1;
  33. } else {
  34. xbound2=x+length;
  35. }
  36. for (int xc = xbound1; xc<xbound2; xc++){
  37. if (grid[xc][y]==1){
  38. return false;
  39. }
  40. if (y+1<10){
  41. if (grid[xc][y+1]==1){
  42. return false;
  43. }
  44. }
  45. if (y-1>=0){
  46. if (grid[xc][y-1]==1){
  47. return false;
  48. }
  49. }
  50. }
  51. for (int xc = x; xc<x+length; xc++){
  52. grid[xc][y]=1;
  53. }
  54. std::vector<int> v;
  55. v.push_back(x);
  56. v.push_back(y);
  57. v.push_back(1);
  58. ships.push_back(v);
  59. return true;
  60. } else {
  61. if ((y+length-1)>9){
  62. return false;
  63. }
  64. int ybound1;
  65. int ybound2;
  66. if (y!=0){
  67. ybound1=y-1;
  68. } else {
  69. ybound1=y;
  70. }
  71. if (y+length<10){
  72. ybound2=y+length+1;
  73. } else {
  74. ybound2=y+length;
  75. }
  76. for (int yc = ybound1; yc<ybound2; yc++){
  77. if (grid[x][yc]==1){
  78. return false;
  79. }
  80. if (x+1<10){
  81. if (grid[x+1][yc]==1){
  82. return false;
  83. }
  84. }
  85. if (x-1>=0){
  86. if (grid[x-1][yc]==1){
  87. return false;
  88. }
  89. }
  90. }
  91. for (int yc = y; yc<y+length; yc++){
  92. grid[x][yc]=1;
  93. }
  94. std::vector<int> v;
  95. v.push_back(x);
  96. v.push_back(y);
  97. v.push_back(0);
  98. ships.push_back(v);
  99. return true;
  100. }
  101. }
  102.  
  103. void placeShip(int length){
  104. r = (a*r+c)%m;
  105. int x = r%10;
  106. int y = ((r-(r%10))/10)%10;
  107. r = (a*r+c)%m;
  108. if (r%2==0){
  109. if (isValidAndPlace(x,y,length,true)){
  110. return;
  111. }
  112. } else {
  113. if (isValidAndPlace(x,y,length,false)){
  114. return;
  115. }
  116. }
  117. placeShip(length);
  118. }
  119.  
  120. void viewGrid(){
  121. for (int y = 9; y>=0; y--){
  122. for (int x = 0; x<10; x++){
  123. std::cout<<grid[x][y];
  124. }
  125. std::cout<<std::endl;
  126. }
  127. }
  128.  
  129. int main(int argc, char const *argv[])
  130. {
  131. initialiseGrid();
  132. std::cin>>a>>c>>m;
  133. for (int i = 0; i<10; i++){
  134. int length = lengths[i];
  135. placeShip(length);
  136. //std::cout<<"ship"<<std::endl;
  137. //viewGrid();
  138. }
  139. for (std::vector<int> v : ships){
  140. char x;
  141. if (v[2]==1){
  142. x='H';
  143. } else {
  144. x='V';
  145. }
  146. std::cout<<v[0]<<" "<<v[1]<<" "<<x<<std::endl;
  147. }
  148. return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement