using System; using System.Collections.Generic; using System.Text; public class Program { public void Proc(){ Reader.IsDebug = false; // Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); int[] inpt = Reader.GetInt(); this.Width = inpt[0]; this.Height = inpt[1]; this.map = new int[Height, Width]; this.flag = new bool[Height, Width]; for(int i=0; i()); if(ret) { Console.WriteLine("possible"); // Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); return; } } } } Console.WriteLine("impossible"); } private int[,] map; private bool[,] flag; private bool GetAns(int posX, int posY, int prevX, int prevY, List log) { string key = posX + "#" + posY; this.flag[posY, posX] = true; List nextPosList = new List(); if(posX > 0) { nextPosList.Add(new int[]{posX - 1, posY }); } if(posX < Width - 1) { nextPosList.Add(new int[]{posX + 1, posY }); } if(posY > 0) { nextPosList.Add(new int[]{posX, posY - 1}); } if(posY < Height - 1) { nextPosList.Add(new int[]{posX, posY + 1}); } log.Add(key); foreach (int[] pos in nextPosList) { if(pos[0] == prevX && pos[1] == prevY) { continue; } if(this.map[pos[1], pos[0]] != this.map[posY, posX]) { continue; } string nextKey = pos[0] + "#" + pos[1]; if(log.Contains(nextKey)) { return true; } bool ret = this.GetAns(pos[0], pos[1], posX, posY, log); if(ret) { return true; } } log.Remove(key); return false; } private int Width; private int Height; public static void Main(string[] args) { Program prg = new Program(); prg.Proc(); } } class Reader { public static bool IsDebug = true; private static System.IO.StringReader sr; public static string ReadLine() { if(IsDebug) { if(sr == null) { sr = new System.IO.StringReader(initStr.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } public static int[] GetInt(char delimiter = ' ') { string[] inpt = ReadLine().Split(delimiter); int[] ret = new int[inpt.Length]; for(int i=0; i