1,2,2,3,4
9,8,0,1,5
3,6,1,1,2
3,3,4,4,5
例如一个二维数组,从任意一个点出发开始寻找出路径点总和大于等于2,小于等于4的路径集合,例如0-2 和 0-2-2满足条件。
递归回溯简单来说就是设置标记,向下递归,出来后还原标记和其他状态等,我这里做简单一点就只考虑4连通,8连通异曲同工而已。
直接上代码。
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
// 递归 回溯 标记
public class Dfs : MonoBehaviour
{
public int[][] arr = new int[4][]
{
new int[5]{1,2,2,3,4},
new int[5]{9,8,0,1,5},
new int[5]{3,6,1,1,2},
new int[5]{3,3,4,4,5},
};
public Vector2 begin = Vector2.zero;
private List<List<int>> result = new List<List<int>>();
private Stack<int> tmp = new Stack<int>();
private int[][] dirty = new int[4][]
{
new int[5],
new int[5],
new int[5],
new int[5],
};
// Start is called before the first frame update
void Start()
{
// 从某个数开始寻找2,4总和之间的路径
tmp.Clear();
int row = (int)begin.x;
int col = (int)begin.y;
tmp.Push(arr[row][col]);
dirty[row][col] = 1;
dfs(row, col, ref arr[row][col]);
if (result.Count > 0)
{
for (int i = 0; i < result.Count; i++)
{
StringBuilder sb = new StringBuilder();
for (int j = 0; j < result[i].Count; j++)
{
sb.Append(result[i][j] + " ");
}
Debug.LogError(sb.ToString());
}
}
}
private void dfs(int row, int col, ref int rNum)
{
// row-1
if (row - 1 >= 0 && dirty[row - 1][col] == 0)
{
if (rNum + arr[row - 1][col] > 4)
{
}
else if (rNum + arr[row - 1][col] < 2)
{
tmp.Push(arr[row - 1][col]);
rNum += arr[row - 1][col];
dirty[row - 1][col] = 1;
dfs(row - 1, col, ref rNum);
rNum -= arr[row - 1][col];
tmp.Pop();
dirty[row - 1][col] = 0;
}
else
{
tmp.Push(arr[row - 1][col]);
result.Add(StackToList(tmp));
rNum += arr[row - 1][col];
dirty[row - 1][col] = 1;
dfs(row - 1, col, ref rNum);
dirty[row - 1][col] = 0;
rNum -= arr[row - 1][col];
tmp.Pop();
}
}
// row+1
if (row + 1 < 4 && dirty[row + 1][col] == 0)
{
if (rNum + arr[row + 1][col] > 4)
{
}
else if (rNum + arr[row + 1][col] < 2)
{
tmp.Push(arr[row + 1][col]);
rNum += arr[row + 1][col];
dirty[row + 1][col] = 1;
dfs(row + 1, col, ref rNum);
dirty[row + 1][col] = 0;
rNum -= arr[row + 1][col];
tmp.Pop();
}
else
{
tmp.Push(arr[row + 1][col]);
result.Add(StackToList(tmp));
rNum += arr[row + 1][col];
dirty[row + 1][col] = 1;
dfs(row + 1, col, ref rNum);
dirty[row + 1][col] = 0;
rNum -= arr[row + 1][col];
tmp.Pop();
}
}
// col-1
if (col - 1 >= 0&&dirty[row][col-1]==0)
{
if (rNum + arr[row][col - 1] > 4)
{
}
else if (rNum + arr[row][col - 1] < 2)
{
tmp.Push(arr[row][col - 1]);
rNum += arr[row][col - 1];
dirty[row][col - 1] = 1;
dfs(row, col - 1, ref rNum);
dirty[row][col - 1] = 0;
rNum -= arr[row][col - 1];
tmp.Pop();
}
else
{
tmp.Push(arr[row][col - 1]);
result.Add(StackToList(tmp));
rNum += arr[row][col - 1];
dirty[row][col - 1] = 1;
dfs(row, col - 1, ref rNum);
dirty[row][col - 1] = 0;
rNum -= arr[row][col - 1];
tmp.Pop();
}
}
// col+1
if (col + 1 < 5&&dirty[row][col+1]==0)
{
if (rNum + arr[row][col + 1] > 4)
{
}
else if (rNum + arr[row][col + 1] < 2)
{
tmp.Push(arr[row][col + 1]);
rNum += arr[row][col + 1];
dirty[row][col + 1] = 1;
dfs(row, col + 1, ref rNum);
dirty[row][col + 1] = 0;
rNum -= arr[row][col + 1];
tmp.Pop();
}
else
{
tmp.Push(arr[row][col + 1]);
result.Add(StackToList(tmp));
rNum += arr[row][col + 1];
dirty[row][col + 1] = 1;
dfs(row, col + 1, ref rNum);
dirty[row][col + 1] = 0;
rNum -= arr[row][col + 1];
tmp.Pop();
}
}
}
private List<int> StackToList(Stack<int> stack)
{
Stack<int> tmp = new Stack<int>();
while (stack.Count > 0)
{
tmp.Push(stack.Pop());
}
List<int> list = new List<int>();
while (tmp.Count > 0)
{
list.Add(tmp.Pop());
}
for (int i = 0; i < list.Count; i++)
{
stack.Push(list[i]);
}
return list;
}
}
填入起始点,可以看到输出。