Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace HashCodePractice
- {
- class Program
- {
- static void Main(string[] args)
- {
- var problem = ReadProblem("c:\\tmp\\c_medium.in");
- }
- public static Problem ReadProblem(string file)
- {
- var lines = File.ReadAllLines(file);
- Problem problem = new Problem();
- problem.Height = int.Parse(lines[0].Split(' ')[0]);
- problem.Width = int.Parse(lines[0].Split(' ')[1]);
- problem.L = int.Parse(lines[0].Split(' ')[2]);
- problem.H = int.Parse(lines[0].Split(' ')[3]);
- problem.Map = new int[problem.Width, problem.Height];
- for(int y = 0; y<problem.Height; y++)
- {
- var line = lines[1 + y];
- for(int x = 0; x < problem.Width; x++)
- {
- problem.Map[x, y] = line[x] == 'M' ? -1 : 1;
- }
- }
- problem.Shapes = new List<Shape>();
- if (problem.L == 4 && problem.H == 12)
- {/*
- problem.Shapes.Add(new Shape(2, 1));
- problem.Shapes.Add(new Shape(1, 2));
- problem.Shapes.Add(new Shape(3, 1));
- problem.Shapes.Add(new Shape(1, 3));
- problem.Shapes.Add(new Shape(4, 1));
- problem.Shapes.Add(new Shape(1, 4));
- problem.Shapes.Add(new Shape(2, 2));
- problem.Shapes.Add(new Shape(5, 1));
- problem.Shapes.Add(new Shape(1, 5));
- problem.Shapes.Add(new Shape(6, 1));
- problem.Shapes.Add(new Shape(1, 6));
- problem.Shapes.Add(new Shape(2, 3));
- problem.Shapes.Add(new Shape(3, 2));*/
- problem.Shapes.Add(new Shape(1, 12));
- problem.Shapes.Add(new Shape(12, 1));
- problem.Shapes.Add(new Shape(2, 6));
- problem.Shapes.Add(new Shape(6, 2));
- problem.Shapes.Add(new Shape(3, 4));
- problem.Shapes.Add(new Shape(4, 3));
- problem.Shapes.Add(new Shape(1, 13));
- problem.Shapes.Add(new Shape(13, 1));
- problem.Shapes.Add(new Shape(1, 14));
- problem.Shapes.Add(new Shape(14, 1));
- problem.Shapes.Add(new Shape(2, 7));
- problem.Shapes.Add(new Shape(7, 2));
- }
- problem.Index = new List<Slice>[problem.Width, problem.Height];
- for (int x = 0; x < problem.Width; x++)
- {
- for (int y = 0; y < problem.Height; y++)
- {
- problem.Index[x, y] = new List<Slice>();
- }
- }
- for (int x=0; x<problem.Width; x++)
- {
- for (int y = 0; y < problem.Height; y++)
- {
- foreach(var shape in problem.Shapes)
- {
- if (x+shape.W < problem.Width && y+shape.H < problem.Height)
- {
- int t = 0;
- int m = 0;
- for (int x2 = 0; x2 < shape.W; x2++)
- {
- for (int y2 = 0; y2 < shape.H; y2++)
- {
- if (problem.Map[x + x2, y + y2] == -1)
- {
- m++;
- }
- else
- {
- t++;
- }
- }
- }
- if (t >= problem.L && m >= problem.L && (t + m) <= problem.H)
- {
- 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 };
- for (int x2 = 0; x2 < shape.W; x2++)
- {
- for (int y2 = 0; y2 < shape.H; y2++)
- {
- problem.Index[x + x2, y + y2].Add(slice);
- }
- }
- }
- }
- }
- }
- }
- bool[,] used = new bool[problem.Width, problem.Height];
- for(int x=0; x<problem.Width; x++)
- {
- for(int y=0; y<problem.Height; y++)
- {
- used[x, y] = false;
- }
- }
- List<Slice> result = new List<Slice>();
- for (int x = 0; x < problem.Width; x++)
- {
- for (int y = 0; y < problem.Height; y++)
- {
- if (!used[x, y])
- {
- var options = problem.Index[x, y].Where(s => s.active).ToList();
- if (options.Count > 0)
- {
- var slice = options.First();
- result.Add(slice);
- slice.active = false;
- for (int xx = slice.x1; xx <= slice.x2; xx++)
- {
- for (int yy = slice.y1; yy <= slice.y2; yy++)
- {
- used[xx, yy] = true;
- foreach(var s in problem.Index[xx,yy].ToList())
- {
- s.active = false;
- }
- }
- }
- }
- }
- }
- }
- List<string> outputLines = new List<string>();
- outputLines.Add(result.Count()+"");
- foreach(var res in result)
- {
- outputLines.Add($"{res.y1} {res.x1} {res.y2} {res.x2}");
- }
- File.WriteAllLines("c:\\tmp\\medium.txt", outputLines);
- return problem;
- }
- }
- public class Problem
- {
- public int Width;
- public int Height;
- public int[,] Map;
- public int L;
- public int H;
- public List<Shape> Shapes { get; set; }
- public List<Slice>[,] Index { get; set; }
- }
- public class Shape
- {
- public Shape(int w, int h)
- {
- this.W = w;
- this.H = h;
- }
- public int W;
- public int H;
- }
- public class Slice
- {
- public int x1 { get; set; }
- public int y1 { get; set; }
- public int x2 { get; set; }
- public int y2 { get; set; }
- public int w { get; set; }
- public int h { get; set; }
- public int s { get; set; }
- public bool active { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement