62,614
社区成员
发帖
与我相关
我的任务
分享
//重装系统还没装java,C#的凑合一下
static void Main(string[] args) {
char[] keys = "SOLO".ToCharArray();
char[] tr0 = "CPUCY".ToCharArray();
char[] tr1 = "EKLQH".ToCharArray();
char[] tr2 = "CRSOL".ToCharArray();
char[] tr3 = "EKLQO".ToCharArray();
char[] tr4 = "PGRBC".ToCharArray();
sf(new char[][] { tr0, tr1, tr2, tr3, tr4 }, keys);
System.Console.In.Read();
}
static void sf(char[][] map, char[] key) {
for (int y = 0; y < map.Length; y++) {
for (int x = 0; x < map[y].Length; x++) {
if (map[y][x] == key[0]) {
//如果找到第一个相同,则进行查找
if (dg(map, key, x, y)) {
System.Console.WriteLine("Yes");
return;
}
}
}
}
System.Console.WriteLine("No");
}
//递归初始化
static bool dg(char[][] map, char[] key, int sx, int sy) {
List<point> points = new List<point>();
return dg(map, key, sx, sy, 0, points);
}
//递归查找
static bool dg(char[][] map, char[] key, int px, int py, int np, List<point> points) {
if (np + 1 == key.Length) return true;
points.Add(new point(px, py));
//不越界,数相符,不是已存在的点
//上
if (py - 1 >= 0 && map[py - 1][px] == key[np + 1] && !exists(points, px, py - 1)) {
return dg(map, key, px, py - 1, np + 1, new List<point>(points));
}
//下
if (py + 1 < map.Length && map[py + 1][px] == key[np + 1] && !exists(points, px, py + 1)) {
return dg(map, key, px, py + 1, np + 1, new List<point>(points));
}
//左
if (px - 1 >= 0 && map[py][px - 1] == key[np + 1] && !exists(points, px - 1, py)) {
return dg(map, key, px - 1, py, np + 1, new List<point>(points));
}
//右
if (px + 1 < map[0].Length && map[py][px + 1] == key[np + 1] && !exists(points, px + 1, py)) {
return dg(map, key, px + 1, py, np + 1, points);
}
//没有找到符合的则返回false
return false;
}
//查找数组中是否存在x,y这个位置
static bool exists(List<point> list, int x, int y) {
foreach (point p in list) {
if (p.Equals(x, y)) {
return true;
}
}
return false;
}
}
class point {
public int x;
public int y;
public point(int x, int y) {
this.x = x;
this.y = y;
}
public bool Equals(int x, int y) {
return this.x == x && this.y == y;
}
}