using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; int[] inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray(); int height = inpt[0]; int width = inpt[1]; this.map = new MapState[height, width]; for(int i=0; iint.Parse(a)).ToArray(); for(int j=0; jint.Parse(a)).ToArray(); this.Proc(inpt[0]-1, inpt[1]-1, inpt[2]); } if(Group.Count == 1) { for(int i=0; ia.Group = first); this.Group[first].AddRange(this.Group[last]); this.Group.Remove(last); map[0,0].Col = newC; return; } Queue task = new Queue(); this.Group[map[r,c].Group].ForEach((a)=>{ a.Color = newC; task.Enqueue(new TaskItem(a.Row, a.Col, newC)); }); while (task.Count > 0) { TaskItem t = task.Dequeue(); int groupIndex = map[t.Row,t.Col].Group; List rin = this.GetRinsetsu(t.Row,t.Col); rin.ForEach((a)=>{ if(map[a.Row, a.Col].Group != groupIndex) { int margeIndex = map[a.Row, a.Col].Group; this.Group[margeIndex].ForEach(b=>b.Group = groupIndex); this.Group[groupIndex].AddRange(this.Group[margeIndex]); this.Group.Remove(margeIndex); } }); } } private void Grouping() { Dictionary> grp = new Dictionary>(); while (map.GetLength(0) * map.GetLength(1) > grp.Sum(a=>a.Value.Count())) { Queue task = new Queue(); Pos target = new Pos(0,0); bool isFind = false; for(int i=0; i()); grp[groupInde].Add(map[target.Row,target.Col]); while (task.Count > 0) { TaskItem t = task.Dequeue(); List nextPosList = this.GetRinsetsu(t.Row,t.Col); nextPosList.ForEach((a)=>{ if(map[a.Row, a.Col].Color == t.NewColor && map[a.Row, a.Col].Group < 0) { map[a.Row, a.Col].Group = groupInde; task.Enqueue(new TaskItem(a.Row, a.Col, map[a.Row, a.Col].Color)); grp[groupInde].Add(map[a.Row, a.Col]); } }); } } this.Group = grp; } private Dictionary> Group = new Dictionary>(); private List GetRinsetsu(int r, int c) { List ret = new List(); if(r > 0) { ret.Add(new Pos(r-1, c)); } if(r < this.map.GetLength(0) - 1) { ret.Add(new Pos(r+1, c)); } if(c > 0) { ret.Add(new Pos(r,c-1)); } if(c < this.map.GetLength(1)-1) { ret.Add(new Pos(r, c+1)); } return ret; } public struct Pos { public int Row; public int Col; public Pos(int r, int c) { this.Row = r; this.Col = c; } } private MapState[,] map; public class TaskItem { public int Col; public int Row; public int NewColor; public TaskItem(int r, int c, int newColor) { this.Row = r; this.Col = c; this.NewColor = newColor; } } public class MapState { public int Col; public int Row; public int Color; public int Group = -1; public MapState(int r, int c, int color) { this.Row = r; this.Col = c; this.Color = color; } } public class Reader { public static bool IsDebug = true; private static String PlainInput = @" 5 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 2 2 1 3 3 1 "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } } static void Main() { Program prg = new Program(); prg.Proc(); } }