Advertisement
Guest User

Problem2

a guest
Jun 1st, 2015
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Text.RegularExpressions;
  7. //using System.Collections.Generic;
  8.  
  9. namespace Problem2
  10. {
  11. class Problem2
  12. {
  13. private static int _rows;
  14. private static int _cols;
  15. private static char[,] _matrix;
  16.  
  17. static void Main(string[] args)
  18. {
  19. String[] p = Console.ReadLine().Trim().Split(' ');
  20.  
  21. _rows = int.Parse(p[0]);
  22. _cols = int.Parse(p[1]);
  23.  
  24. String snake = Console.ReadLine();
  25. _matrix = new char[_rows, _cols];
  26. PopulateMatrix(snake);
  27.  
  28. String[] shot = Console.ReadLine().Split(' ');
  29.  
  30. int shotRow = int.Parse(shot[0]);
  31. int shotCol = int.Parse(shot[1]);
  32. int shotRadius = int.Parse(shot[2]);
  33.  
  34. MakeShot(shotRow, shotCol, shotRadius);
  35. ObjectsFall();
  36.  
  37. PrintResults();
  38. }
  39.  
  40. private static void PrintResults()
  41. {
  42. for (int row = 0; row < _rows; row++)
  43. {
  44. for (int col = 0; col < _cols; col++)
  45. {
  46. Console.Write(_matrix[row, col].ToString());
  47. }
  48. Console.WriteLine();
  49. }
  50. }
  51.  
  52. private static void ObjectsFall()
  53. {
  54. for (int row = _rows - 2; row >= 0; row--)
  55. {
  56. for (int col = 0; col < _cols; col++)
  57. {
  58. int nextRow = row + 1;
  59. char current = _matrix[row, col];
  60. while (true)
  61. {
  62. char nextChar;
  63. try
  64. {
  65. nextChar = _matrix[nextRow, col];
  66. //if (nextChar == ' ' || nextChar == '\0')
  67. if (nextChar == ' ')
  68. {
  69. _matrix[nextRow, col] = current;
  70. _matrix[nextRow - 1, col] = ' ';
  71. current = _matrix[nextRow, col];
  72. nextRow++;
  73. continue;
  74. }
  75. break;
  76. }
  77. catch (Exception)
  78. {
  79. break;
  80. }
  81. }
  82. }
  83. }
  84. }
  85.  
  86. private static void MakeShot(int shotRow, int shotCol, int shotRadius)
  87. {
  88.  
  89.  
  90. //(pointX - 1) * (pointX - 1) + (pointY - 1) * (pointY - 1) <= radius * radius;
  91. // (x - h)*(x - h) + (y - k)*(y - k) = r * r
  92. for (int row = 0; row < _rows; row++)
  93. {
  94. for (int col = 0; col < _cols; col++)
  95. {
  96. if (IsInCircle(shotRow, shotCol, row, col, shotRadius))
  97. {
  98. _matrix[row, col] = ' ';
  99. }
  100. }
  101. }
  102. //_matrix[shotRow, shotCol] = ' ';
  103. ////ShotLeft(shotRow, shotCol, shotRadius);
  104. ////ShotRight(shotRow, shotCol, shotRadius);
  105. ////ShotUp(shotRow, shotCol, shotRadius);
  106. ////ShotDown(shotRow, shotCol, shotRadius);
  107. ////ShotLeftUp(shotRow, shotCol, shotRadius);
  108. ////ShotRightUp(shotRow, shotCol, shotRadius);
  109. ////ShotLeftDown(shotRow, shotCol, shotRadius);
  110. ////ShotRightDown(shotRow, shotCol, shotRadius);
  111. }
  112.  
  113. private static bool IsInCircle(int startX, int startY, int x, int y, int r)
  114. {
  115. return ((x - startX) * (x - startX) + (y - startY) * (y - startY)) <= r * r;
  116. }
  117.  
  118. private static void ShotRightDown(int shotRow, int shotCol, int shotRadius)
  119. {
  120. int startRow = shotRow;
  121. int startCol = shotCol;
  122.  
  123. if (shotRadius == 1)
  124. {
  125. try
  126. {
  127. _matrix[startRow + 1, startCol + 1] = ' ';
  128. return;
  129. }
  130. catch (Exception)
  131. {
  132. return;
  133. }
  134. }
  135.  
  136. for (int i = 1; i < shotRadius; i++)
  137. {
  138. try
  139. {
  140. _matrix[startRow + i, startCol + i] = ' ';
  141. }
  142. catch (Exception)
  143. {
  144. break;
  145. }
  146. }
  147. }
  148.  
  149. private static void ShotLeftDown(int shotRow, int shotCol, int shotRadius)
  150. {
  151. int startRow = shotRow;
  152. int startCol = shotCol;
  153.  
  154. if (shotRadius == 1)
  155. {
  156. try
  157. {
  158. _matrix[startRow + 1, startCol - 1] = ' ';
  159. return;
  160. }
  161. catch (Exception)
  162. {
  163. return;
  164. }
  165.  
  166. }
  167.  
  168. for (int i = 1; i < shotRadius; i++)
  169. {
  170. try
  171. {
  172. _matrix[startRow + i, startCol - i] = ' ';
  173. }
  174. catch (Exception)
  175. {
  176. break;
  177. }
  178. }
  179. }
  180.  
  181. private static void ShotRightUp(int shotRow, int shotCol, int shotRadius)
  182. {
  183. int startRow = shotRow;
  184. int startCol = shotCol;
  185.  
  186. if (shotRadius == 1)
  187. {
  188. try
  189. {
  190. _matrix[startRow - 1, startCol + 1] = ' ';
  191. return;
  192. }
  193. catch (Exception)
  194. {
  195. return;
  196. }
  197.  
  198. }
  199.  
  200. for (int i = 1; i < shotRadius; i++)
  201. {
  202. try
  203. {
  204. _matrix[startRow - i, startCol + i] = ' ';
  205. }
  206. catch (Exception)
  207. {
  208. break;
  209. }
  210. }
  211. }
  212.  
  213. private static void ShotLeftUp(int shotRow, int shotCol, int shotRadius)
  214. {
  215. int startRow = shotRow;
  216. int startCol = shotCol;
  217.  
  218. if (shotRadius == 1)
  219. {
  220. try
  221. {
  222. _matrix[startRow - 1, startCol - 1] = ' ';
  223. return;
  224. }
  225. catch (Exception)
  226. {
  227. return;
  228. }
  229. }
  230.  
  231. for (int i = 1; i < shotRadius; i++)
  232. {
  233. try
  234. {
  235. _matrix[startRow - i, startCol - i] = ' ';
  236. }
  237. catch (Exception)
  238. {
  239. break;
  240. }
  241. }
  242. }
  243.  
  244. private static void ShotDown(int shotRow, int shotCol, int shotRadius)
  245. {
  246. int startIndex = shotRow;
  247. for (int i = 1; i <= shotRadius; i++)
  248. {
  249. try
  250. {
  251. _matrix[startIndex + i, shotCol] = ' ';
  252. }
  253. catch (Exception)
  254. {
  255. break;
  256. }
  257. }
  258. }
  259.  
  260. private static void ShotUp(int shotRow, int shotCol, int shotRadius)
  261. {
  262. int startIndex = shotRow;
  263. for (int i = 1; i <= shotRadius; i++)
  264. {
  265. try
  266. {
  267. _matrix[startIndex - i, shotCol] = ' ';
  268. }
  269. catch (Exception)
  270. {
  271. break;
  272. }
  273. }
  274. }
  275.  
  276. private static void ShotRight(int shotRow, int shotCol, int shotRadius)
  277. {
  278. int startIndex = shotCol;
  279. for (int i = 1; i <= shotRadius; i++)
  280. {
  281. try
  282. {
  283. _matrix[shotRow, startIndex + i] = ' ';
  284. }
  285. catch (Exception)
  286. {
  287. break;
  288. }
  289. }
  290. }
  291.  
  292. private static void ShotLeft(int shotRow, int shotCol, int shotRadius)
  293. {
  294. int startIndex = shotCol;
  295. for (int i = 1; i <= shotRadius; i++)
  296. {
  297. try
  298. {
  299. _matrix[shotRow, startIndex - i] = ' ';
  300. }
  301. catch (Exception)
  302. {
  303. break;
  304. }
  305. }
  306. }
  307.  
  308. private static void PopulateMatrix(String snake)
  309. {
  310. int snakeIndex = 0;
  311. int startRow = _rows - 1;
  312. int startCol = _cols - 1;
  313.  
  314. int flag = 0;
  315. for (int row = _rows - 1; row >= 0; row--)
  316. {
  317. if (flag == 0)
  318. {
  319. for (int col = _cols - 1; col >= 0; col--)
  320. {
  321. if (snakeIndex >= snake.Length)
  322. {
  323. snakeIndex = 0;
  324. }
  325. _matrix[row, col] = snake[snakeIndex];
  326. snakeIndex++;
  327. }
  328. flag = 1;
  329. }
  330. else
  331. {
  332. for (int col = 0; col < _cols; col++)
  333. {
  334. if (snakeIndex >= snake.Length)
  335. {
  336. snakeIndex = 0;
  337. }
  338. _matrix[row, col] = snake[snakeIndex];
  339. snakeIndex++;
  340. }
  341. flag = 0;
  342. }
  343. }
  344. }
  345. }
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement