Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace HashCodePractice
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14.  
  15.  
  16. var problem = ReadProblem("c:\\tmp\\c_medium.in");
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. }
  24.  
  25. public static Problem ReadProblem(string file)
  26. {
  27. var lines = File.ReadAllLines(file);
  28. Problem problem = new Problem();
  29. problem.Height = int.Parse(lines[0].Split(' ')[0]);
  30. problem.Width = int.Parse(lines[0].Split(' ')[1]);
  31. problem.L = int.Parse(lines[0].Split(' ')[2]);
  32. problem.H = int.Parse(lines[0].Split(' ')[3]);
  33. problem.Map = new int[problem.Width, problem.Height];
  34. for(int y = 0; y<problem.Height; y++)
  35. {
  36. var line = lines[1 + y];
  37. for(int x = 0; x < problem.Width; x++)
  38. {
  39. problem.Map[x, y] = line[x] == 'M' ? -1 : 1;
  40. }
  41. }
  42.  
  43. problem.Shapes = new List<Shape>();
  44. if (problem.L == 4 && problem.H == 12)
  45. {/*
  46. problem.Shapes.Add(new Shape(2, 1));
  47. problem.Shapes.Add(new Shape(1, 2));
  48.  
  49. problem.Shapes.Add(new Shape(3, 1));
  50. problem.Shapes.Add(new Shape(1, 3));
  51.  
  52. problem.Shapes.Add(new Shape(4, 1));
  53. problem.Shapes.Add(new Shape(1, 4));
  54. problem.Shapes.Add(new Shape(2, 2));
  55.  
  56. problem.Shapes.Add(new Shape(5, 1));
  57. problem.Shapes.Add(new Shape(1, 5));
  58.  
  59. problem.Shapes.Add(new Shape(6, 1));
  60. problem.Shapes.Add(new Shape(1, 6));
  61. problem.Shapes.Add(new Shape(2, 3));
  62. problem.Shapes.Add(new Shape(3, 2));*/
  63.  
  64. problem.Shapes.Add(new Shape(1, 12));
  65. problem.Shapes.Add(new Shape(12, 1));
  66.  
  67. problem.Shapes.Add(new Shape(2, 6));
  68. problem.Shapes.Add(new Shape(6, 2));
  69.  
  70. problem.Shapes.Add(new Shape(3, 4));
  71. problem.Shapes.Add(new Shape(4, 3));
  72.  
  73. problem.Shapes.Add(new Shape(1, 13));
  74. problem.Shapes.Add(new Shape(13, 1));
  75.  
  76. problem.Shapes.Add(new Shape(1, 14));
  77. problem.Shapes.Add(new Shape(14, 1));
  78.  
  79. problem.Shapes.Add(new Shape(2, 7));
  80. problem.Shapes.Add(new Shape(7, 2));
  81.  
  82. }
  83.  
  84. problem.Index = new List<Slice>[problem.Width, problem.Height];
  85. for (int x = 0; x < problem.Width; x++)
  86. {
  87. for (int y = 0; y < problem.Height; y++)
  88. {
  89. problem.Index[x, y] = new List<Slice>();
  90. }
  91. }
  92.  
  93. for (int x=0; x<problem.Width; x++)
  94. {
  95. for (int y = 0; y < problem.Height; y++)
  96. {
  97. foreach(var shape in problem.Shapes)
  98. {
  99. if (x+shape.W < problem.Width && y+shape.H < problem.Height)
  100. {
  101. int t = 0;
  102. int m = 0;
  103. for (int x2 = 0; x2 < shape.W; x2++)
  104. {
  105. for (int y2 = 0; y2 < shape.H; y2++)
  106. {
  107. if (problem.Map[x + x2, y + y2] == -1)
  108. {
  109. m++;
  110. }
  111. else
  112. {
  113. t++;
  114. }
  115. }
  116. }
  117.  
  118.  
  119. if (t >= problem.L && m >= problem.L && (t + m) <= problem.H)
  120. {
  121. var slice = new Slice() { x1 = x, y1 = y, x2 = x + shape.W -1, y2 = y + shape.H - 1, h = shape.H, w = shape.W, s = shape.W * shape.H, active = true };
  122. for (int x2 = 0; x2 < shape.W; x2++)
  123. {
  124. for (int y2 = 0; y2 < shape.H; y2++)
  125. {
  126. problem.Index[x + x2, y + y2].Add(slice);
  127. }
  128. }
  129.  
  130. }
  131.  
  132. }
  133.  
  134. }
  135. }
  136.  
  137. }
  138.  
  139. bool[,] used = new bool[problem.Width, problem.Height];
  140. for(int x=0; x<problem.Width; x++)
  141. {
  142. for(int y=0; y<problem.Height; y++)
  143. {
  144. used[x, y] = false;
  145. }
  146. }
  147.  
  148. List<Slice> result = new List<Slice>();
  149. for (int x = 0; x < problem.Width; x++)
  150. {
  151. for (int y = 0; y < problem.Height; y++)
  152. {
  153. if (!used[x, y])
  154. {
  155. var options = problem.Index[x, y].Where(s => s.active).ToList();
  156. if (options.Count > 0)
  157. {
  158. var slice = options.First();
  159. result.Add(slice);
  160. slice.active = false;
  161. for (int xx = slice.x1; xx <= slice.x2; xx++)
  162. {
  163. for (int yy = slice.y1; yy <= slice.y2; yy++)
  164. {
  165. used[xx, yy] = true;
  166. foreach(var s in problem.Index[xx,yy].ToList())
  167. {
  168. s.active = false;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176.  
  177.  
  178. List<string> outputLines = new List<string>();
  179. outputLines.Add(result.Count()+"");
  180. foreach(var res in result)
  181. {
  182. outputLines.Add($"{res.y1} {res.x1} {res.y2} {res.x2}");
  183. }
  184.  
  185. File.WriteAllLines("c:\\tmp\\medium.txt", outputLines);
  186.  
  187. return problem;
  188. }
  189.  
  190. }
  191.  
  192. public class Problem
  193. {
  194. public int Width;
  195.  
  196. public int Height;
  197.  
  198. public int[,] Map;
  199.  
  200. public int L;
  201.  
  202. public int H;
  203.  
  204. public List<Shape> Shapes { get; set; }
  205.  
  206. public List<Slice>[,] Index { get; set; }
  207. }
  208.  
  209. public class Shape
  210. {
  211. public Shape(int w, int h)
  212. {
  213. this.W = w;
  214. this.H = h;
  215. }
  216.  
  217. public int W;
  218.  
  219. public int H;
  220. }
  221.  
  222. public class Slice
  223. {
  224. public int x1 { get; set; }
  225. public int y1 { get; set; }
  226. public int x2 { get; set; }
  227. public int y2 { get; set; }
  228. public int w { get; set; }
  229. public int h { get; set; }
  230. public int s { get; set; }
  231. public bool active { get; set; }
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement