0,1 값을 포함하는 int matrix 내의 위치로 역 추적하지 않고 경로를 찾는 재귀 메서드를 코딩하려고합니다. 0은 밟아도 괜찮고 1은 밟아도 괜찮습니다. 또한 50 개 이상의 이동 경로를 제한하고 있습니다.
위치는 행 및 열 값 (x, y)이있는 개체입니다. locationEquals는 두 위치가 같으면 true를 반환하고 그렇지 않으면 false를 반환하는 함수입니다. 맵 변수는 경로 찾기를 시도하는 행렬입니다.
private static List<List<Location>> options = new ArrayList<List<Location>>();
public static void PathFind(List<Location> path)
{
Location current = path.get(path.size() - 1);
boolean done = false;
if(locationEquals(current,new Location(24,38)))
{
options.add(path);
return;
}
if(path.size() > 50) done = true;
if(!done)
{
try
{
if(map[current.row][current.col + 1] == 0)
{
if(!path.contains(new Location(current.row, current.col + 1)))
{
List<Location> temp = path;
temp.add(new Location(current.row, current.col + 1));
PathFind(temp);
}
}
}
catch (Exception e){}
try
{
if(map[current.row - 1][current.col] == 0)
{
if(!path.contains(new Location(current.row - 1, current.col)))
{
List<Location> temp = path;
temp.add(new Location(current.row - 1, current.col));
PathFind(temp);
}
}
}
catch (Exception e){}
try
{
if(map[current.row][current.col - 1] == 0)
{
if(!path.contains(new Location(current.row, current.col - 1)))
{
List<Location> temp = path;
temp.add(new Location(current.row, current.col - 1));
PathFind(temp);
}
}
}
catch (Exception e){}
try
{
if(map[current.row + 1][current.col] == 0)
{
if(!path.contains(new Location(current.row + 1, current.col)))
{
List<Location> temp = path;
temp.add(new Location(current.row + 1, current.col));
PathFind(temp);
}
}
}
catch (Exception e){}
}
다음 코드를 실행 한 후 'options'가 비어 있으며 이는 방법을 찾지 못했음을 의미합니다. 그러나이 매트릭스에는 확실히 방법이 있으므로 찾을 수없는 내 코드의 버그입니다.
문제는 재귀의 다음 단계로 이동할 때마다 새 목록을 만들지 않는다는 것입니다 (임시 변수는 경로의 복사본이 아니라 경로에 대한 참조 일 뿐이므로 실제로 임시 변수가 아닙니다).
이 문제를 해결하기 위해, 나는 교체 List<Location> temp = path;
와 함께List<Location> temp = new ArrayList<>(path);
따라서 코드는 다음과 같습니다.
private static List<List<Location>> options = new ArrayList<List<Location>>();
public static void PathFind(List<Location> path) {
Location current = path.get(path.size() - 1);
boolean done = false;
if (locationEquals(current, new Location(24, 38))) {
options.add(path);
return;
}
if (path.size() > 50) done = true;
if (!done) {
try {
if (map[current.row][current.col + 1] == 0) {
if (!path.contains(new Location(current.row, current.col + 1))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row, current.col + 1));
PathFind(temp);
}
}
} catch (Exception e) {
}
try {
if (map[current.row - 1][current.col] == 0) {
if (!path.contains(new Location(current.row - 1, current.col))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row - 1, current.col));
PathFind(temp);
}
}
} catch (Exception e) {
}
try {
if (map[current.row][current.col - 1] == 0) {
if (!path.contains(new Location(current.row, current.col - 1))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row, current.col - 1));
PathFind(temp);
}
}
} catch (Exception e) {
}
try {
if (map[current.row + 1][current.col] == 0) {
if (!path.contains(new Location(current.row + 1, current.col))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row + 1, current.col));
PathFind(temp);
}
}
} catch (Exception e) {
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다