I need help fixing backtracking function on code for puzzle solver.

조회 수: 4 (최근 30일)
Adrian
Adrian 2024년 4월 30일
답변: John 2024년 5월 9일
Can't find what I'm missing in the code for this backtracking function to run correctly.
% Define the missing functions
function [imax, jmax, MaxScore] = findNextCell(P, HashRow, HashCol, HashBlock)
% Find the next cell with the least number of possible values
imax = 0; jmax = 0; MaxScore = -1;
for i = 1:16
for j = 1:16
if P(i,j) == -1
score = sum(HashRow(i,:)) + sum(HashCol(j,:)) + sum(sum(HashBlock(ceil(i/4),ceil(j/4),:)));
if score > MaxScore
MaxScore = score;
imax = i;
jmax = j;
else
end
end
end
end
end
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2024년 4월 30일
What is the objective here? What is the function suppossed to be doing?
What inputs are provided to it? And what are the corresponding expected outputs?

댓글을 달려면 로그인하십시오.

답변 (2개)

Sanju
Sanju 2024년 5월 6일
Hi Adrian,
The code you provided seems to be missing the main function that calls the findNextCell function. Without the main function, it is difficult to determine the exact issue you are facing. However, based on the provided code, I can see that the else statement inside the inner loop is empty. This might cause unexpected behavior in your code. You should either remove the else statement or add the necessary code inside it.
Here is an example of how you can call the findNextCell function from a main function,
function mainFunction()
% Define your input variables
P = ... % your input matrix
HashRow = ... % your input matrix
HashCol = ... % your input matrix
HashBlock = ... % your input matrix
% Call the findNextCell function
[imax, jmax, MaxScore] = findNextCell(P, HashRow, HashCol, HashBlock);
% Display the results
.......
end
Please provide more details or the complete code if you need further assistance.
Hope this helps!

John
John 2024년 5월 9일
it seems like you're trying to implement a function findNextCell that selects the next empty cell with the least number of possible values in a 16x16 grid for a puzzle solver using backtracking.
function [imax, jmax, MaxScore] = findNextCell(P, HashRow, HashCol, HashBlock)
% Find the next cell with the least number of possible values
imax = 0;
jmax = 0;
MaxScore = Inf; % Initialize MaxScore to a large value
for i = 1:16
for j = 1:16
if P(i,j) == -1
score = sum(HashRow(i,:)) + sum(HashCol(j,:)) + sum(HashBlock(ceil(i/4),ceil(j/4),:), 'all');
if score < MaxScore
MaxScore = score;
imax = i;
jmax = j;
end
end
end
end
end
effectiveness of this function depends on how the HashRow, HashCol, and HashBlock matrices are constructed and what they represent in your puzzle solver. Ensure that these matrices are properly initialized and updated to reflect the current state of the puzzle grid.

카테고리

Help CenterFile Exchange에서 Card games에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by