Informatika gyűjtemény

NézetNyomtat

kb_jegtomb.cs (Vissza)
Az alábbi letöltési lehetőségek közül választhatsz: (segítség)
Karakterkódolás:
Sortörés:
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 3 KB
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Jegtomb
{
    class Coords
    {
        public int x, y, z;

        public Coords(int _x, int _y, int _z)
        {
            x = _x;
            y = _y;
            z = _z;
        }
    }

    class Program
    {
        private int n, m, k;
        private bool[, ,] ice;
        private Queue<Coords> q = new Queue<Coords>();
        private int[] time = new int[100];

        public Program()        
        {
            StreamReader ins = new StreamReader("jegtomb8.be");
            StreamWriter outs = new StreamWriter("jegtomb8.ki");
            string[] fLine = ins.ReadLine().Split(' ');
            m = int.Parse(fLine[0]);
            n = int.Parse(fLine[1]);
            k = int.Parse(fLine[2]);
            ice = new bool[n, m, k];

            string line;
            for (int l = 0; l < k; l++)
                // adott sík
                for (int j = 0; j < m; j++)
                {
                    // adott sorának
                    line = ins.ReadLine();
                    for (int i = 0; i < n; i++)
                    {
                        if (line[i] == '1')
                        {
                            ice[i, j, l] = true;
                            q.Enqueue( new Coords(i,j,l) );
                        }
                        else ice[i, j, l] = false;
                    }
                }

            melt( outs );
        }

        private void melt( StreamWriter outs )
        {            
            Queue<Coords> q2;
            Queue<Coords> q3 = new Queue<Coords>();
                
            Coords c;
            int i = 0;
            time[0] = q.Count;
            while (q.Count != 0)
            {
                q2 = new Queue<Coords>();
                while (q.Count != 0)
                {
                    c = q.Dequeue();
                    if (enoughAir(c.x, c.y, c.z))
                    {
                        q3.Enqueue(c); // ők olvadnak el
                    }
                    else
                    {
                        q2.Enqueue(c); // velük számolunk tovább
                    }
                }
                while (q3.Count != 0)
                {
                    c = q3.Dequeue();
                    ice[c.x, c.y, c.z] = false;
                }
                time[++i] = q2.Count; // amennyi megmaradt.
                q = q2; // maradékkal számolunk tovább.
            }

            outs.WriteLine( i );
            for( int j=0; j < i; j++ )
            {
                outs.WriteLine(time[j]);
            }
            outs.Close();
        }

        private bool enoughAir(int i, int j, int l)
        {
            int air = 0;
            if (> 0 && !ice[i, j, l - 1]) air++; // fölötte
            if (< k - 1 && !ice[i, j, l + 1]) air++; // alatta
            if (!ice[- 1, j, l]) air++;
            if (!ice[+ 1, j, l]) air++;
            if (!ice[i, j + 1, l]) air++;
            if (!ice[i, j - 1, l]) air++;

            return air >= 3;
        }

        static void Main(string[] args)
        {
            new Program();
        }
    }
}
(Vissza)