Exemplo n.º 1
0
    static void Main()
    {
        var(h, w) = (4, 4);
        var s = GraphConsole.ReadGrid(h);

        var rhw      = Enumerable.Range(0, h * w).ToArray();
        var toHash   = GridHelper.CreateToHash(w);
        var fromHash = GridHelper.CreateFromHash(w);

        var sx = rhw
                 .Where(i => s.GetValue(fromHash(i)) == '#')
                 .Aggregate(0, (p, i) => p | (1 << i));

        var map = rhw.Select(id => new List <int> {
            id
        }).ToArray();

        for (int i = 0; i < h; i++)
        {
            for (int j = 1; j < w; j++)
            {
                map[toHash((i, j))].Add(toHash((i, j - 1)));
Exemplo n.º 2
0
    static bool Solve()
    {
        var(h, w) = Read2();
        var s = GraphConsole.ReadGrid(h);
        var t = GraphConsole.ReadGrid(h);

        GridHelper.EncloseGrid(ref h, ref w, ref s, delta: 10);

        if (Matches())
        {
            return(true);
        }
        t = GridHelper.RotateLeft(t);
        if (Matches())
        {
            return(true);
        }
        t = GridHelper.RotateLeft(t);
        if (Matches())
        {
            return(true);
        }
        t = GridHelper.RotateLeft(t);
        if (Matches())
        {
            return(true);
        }

        return(false);

        bool Matches()
        {
            var(th, tw) = (t.Length, t[0].Length);
            for (int di = 0; di < h - th + 1; di++)
            {
                for (int dj = 0; dj < w - tw + 1; dj++)
                {
                    if (Matches2(di, dj))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }

        bool Matches2(int di, int dj)
        {
            var(th, tw) = (t.Length, t[0].Length);
            for (int i = 0; i < th; i++)
            {
                for (int j = 0; j < tw; j++)
                {
                    if (t[i][j] == '#' && s[i + di][j + dj] == '#')
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
    }
Exemplo n.º 3
0
    static void Main()
    {
        var(h, w) = Read2();
        var s = GraphConsole.ReadGrid(h);

        var k = s.Sum(l => l.Count(x => x == '#'));

        var map = new GridListMap <Point>(h, w);

        for (int i = 0; i < h; i++)
        {
            for (int j = 1; j < w; j++)
            {
                Point v  = (i, j);
                Point nv = (i, j - 1);
                if (s.GetValue(v) == '#' && s.GetValue(nv) == '#')
                {
                    map.Add(v, nv);
                    map.Add(nv, v);
                }
            }
        }
        for (int j = 0; j < w; j++)
        {
            for (int i = 1; i < h; i++)
            {
                Point v  = (i, j);
                Point nv = (i - 1, j);
                if (s.GetValue(v) == '#' && s.GetValue(nv) == '#')
                {
                    map.Add(v, nv);
                    map.Add(nv, v);
                }
            }
        }

        var q = new Stack <Point>();
        var u = new GridMap <bool>(h, w, false);

        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if (s[i][j] == '.')
                {
                    continue;
                }
                q.Clear();
                u = new GridMap <bool>(h, w, false);
                Dfs((i, j), (-1, -1));
            }
        }

        void Dfs(Point v, Point pv)
        {
            q.Push(v);
            u[v] = true;
            if (q.Count == k)
            {
                Console.WriteLine(k);
                Console.WriteLine(string.Join("\n", q.Select(p => p + (1, 1))));
                Environment.Exit(0);
            }

            foreach (var nv in map[v])
            {
                if (nv == pv)
                {
                    continue;
                }
                if (u[nv])
                {
                    continue;
                }
                Dfs(nv, v);
            }
            q.Pop();
            u[v] = false;
        }
    }