using System using System Collections Generic using System Text using

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace zadachaXwab
{
class Program
{
static void Main(string[] args)
{
List<List<string[]>> puti = new List<List<string[]>>();
List<char[]> labirint;
List<string[]> put = new List<string[]>();
int[] position;
int[] razm;
loadLabirint(out labirint, out position, out razm);
puti = findPut(position, labirint, razm, put, puti);
foreach (List<string[]> item in puti)
if (item.Count < put.Count || put.Count == 0)
put = new List<string[]>(item);
foreach (string[] item in put)
Console.WriteLine("({0},{1})", item[0], item[1]);
Console.ReadKey();
}
static List<List<string[]>> findPut(int[] pos, List<char[]> lab, int[] razm, List<string[]> put, List<List<string[]>> p)
{
if (pos[0] >= 0 &amp;&amp; pos[1] >= 0 &amp;&amp; pos[0] <= razm[0] - 1 &amp;&amp; pos[1] <= razm[1] - 1)
{
string[] pos1 = new string[] { pos[0].ToString(), pos[1].ToString() };
if (lab[pos[1]][pos[0]] == '0' &amp;&amp; !put.Exists(t => t[0] == pos1[0] &amp;&amp; t[1] == pos1[1]))
{
put.Add(pos1);
if (pos[0] == 0 || pos[1] == 0 || pos[0] == razm[0] - 1 || pos[1] == razm[1] - 1)
p.Add(new List<string[]>( put));
for (int i = -1; i <= 1; i += 2)
{
findPut(new int[] { pos[0] + i, pos[1] }, lab, razm, put, p);
findPut(new int[] { pos[0], pos[1] + i }, lab, razm, put, p);
}
put.Remove(pos1);
}
}
return p;
}
static void loadLabirint(out List<char[]> labirint, out int[] position, out int[] razm)
{
if (!File.Exists("./labirint.txt"))
{
Console.WriteLine("В папке с экзешником надо иметь файл с инфой о лабиринте");
Console.ReadKey();
Main(null);
}
StreamReader sr = File.OpenText("./labirint.txt");
razm = new int[2];
razm[0] = Convert.ToInt32(sr.ReadLine().Trim());
razm[1] = Convert.ToInt32(sr.ReadLine().Trim());
labirint = new List<char[]>();
position = new int[2];
position[0] = Convert.ToInt32(sr.ReadLine().Trim()) - 1;
position[1] = Convert.ToInt32(sr.ReadLine().Trim()) - 1;
string str = "";
while ((str = sr.ReadLine()) != null)
if (str != "")
labirint.Add(str.ToCharArray());
}
}
}