Check to see if I have a valid solution to an underlying probelm in an 8-puzzle game











up vote
-2
down vote

favorite












public final boolean isValidSolution(List<T> solution) throws NullPointerException {
int inversions = 0;
for (int i = 0; i < solution.size(); i++) {
if (solution.get(i) == 0) {
continue;
}
for (int j = i; j < solution.size(); j++) {
if (solution.get(j) == 0) {
continue;
}
if (solution.get(j) < solution.get(i)) {
inversions++;
}
}
}

return (inversions & 1) == 0;
}
/**
* Checks that a solution is valid.
*
* A valid solution consists of a list of states. The list should start with
* the initial state of the underlying problem. Then, it should have one or
* more additional states. Each state should be a successor of its
* predecessor. The last state should be a goal state of the underlying
* problem.
*
* @param solution
* @return true iff this solution is a valid solution
* @throws NullPointerException
* if solution is null
*/


Now I know that the code is wrong, the solution.get(i) is incompatible for type T and int. Please help me in this method.










share|improve this question
























  • What is the bound for T?
    – user7
    Nov 8 at 16:56










  • What do you mean by the bound for T ? T could represent any class for instance in one of my test, it's List<Cell>, sometimes, List<Integer>.
    – Tirth Patel
    Nov 8 at 17:02










  • In other words what is a state as described in the problem statement
    – user7
    Nov 8 at 17:03

















up vote
-2
down vote

favorite












public final boolean isValidSolution(List<T> solution) throws NullPointerException {
int inversions = 0;
for (int i = 0; i < solution.size(); i++) {
if (solution.get(i) == 0) {
continue;
}
for (int j = i; j < solution.size(); j++) {
if (solution.get(j) == 0) {
continue;
}
if (solution.get(j) < solution.get(i)) {
inversions++;
}
}
}

return (inversions & 1) == 0;
}
/**
* Checks that a solution is valid.
*
* A valid solution consists of a list of states. The list should start with
* the initial state of the underlying problem. Then, it should have one or
* more additional states. Each state should be a successor of its
* predecessor. The last state should be a goal state of the underlying
* problem.
*
* @param solution
* @return true iff this solution is a valid solution
* @throws NullPointerException
* if solution is null
*/


Now I know that the code is wrong, the solution.get(i) is incompatible for type T and int. Please help me in this method.










share|improve this question
























  • What is the bound for T?
    – user7
    Nov 8 at 16:56










  • What do you mean by the bound for T ? T could represent any class for instance in one of my test, it's List<Cell>, sometimes, List<Integer>.
    – Tirth Patel
    Nov 8 at 17:02










  • In other words what is a state as described in the problem statement
    – user7
    Nov 8 at 17:03















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











public final boolean isValidSolution(List<T> solution) throws NullPointerException {
int inversions = 0;
for (int i = 0; i < solution.size(); i++) {
if (solution.get(i) == 0) {
continue;
}
for (int j = i; j < solution.size(); j++) {
if (solution.get(j) == 0) {
continue;
}
if (solution.get(j) < solution.get(i)) {
inversions++;
}
}
}

return (inversions & 1) == 0;
}
/**
* Checks that a solution is valid.
*
* A valid solution consists of a list of states. The list should start with
* the initial state of the underlying problem. Then, it should have one or
* more additional states. Each state should be a successor of its
* predecessor. The last state should be a goal state of the underlying
* problem.
*
* @param solution
* @return true iff this solution is a valid solution
* @throws NullPointerException
* if solution is null
*/


Now I know that the code is wrong, the solution.get(i) is incompatible for type T and int. Please help me in this method.










share|improve this question















public final boolean isValidSolution(List<T> solution) throws NullPointerException {
int inversions = 0;
for (int i = 0; i < solution.size(); i++) {
if (solution.get(i) == 0) {
continue;
}
for (int j = i; j < solution.size(); j++) {
if (solution.get(j) == 0) {
continue;
}
if (solution.get(j) < solution.get(i)) {
inversions++;
}
}
}

return (inversions & 1) == 0;
}
/**
* Checks that a solution is valid.
*
* A valid solution consists of a list of states. The list should start with
* the initial state of the underlying problem. Then, it should have one or
* more additional states. Each state should be a successor of its
* predecessor. The last state should be a goal state of the underlying
* problem.
*
* @param solution
* @return true iff this solution is a valid solution
* @throws NullPointerException
* if solution is null
*/


Now I know that the code is wrong, the solution.get(i) is incompatible for type T and int. Please help me in this method.







java search 8-puzzle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 16:53









Federico klez Culloca

15.4k134274




15.4k134274










asked Nov 8 at 16:53









Tirth Patel

11




11












  • What is the bound for T?
    – user7
    Nov 8 at 16:56










  • What do you mean by the bound for T ? T could represent any class for instance in one of my test, it's List<Cell>, sometimes, List<Integer>.
    – Tirth Patel
    Nov 8 at 17:02










  • In other words what is a state as described in the problem statement
    – user7
    Nov 8 at 17:03




















  • What is the bound for T?
    – user7
    Nov 8 at 16:56










  • What do you mean by the bound for T ? T could represent any class for instance in one of my test, it's List<Cell>, sometimes, List<Integer>.
    – Tirth Patel
    Nov 8 at 17:02










  • In other words what is a state as described in the problem statement
    – user7
    Nov 8 at 17:03


















What is the bound for T?
– user7
Nov 8 at 16:56




What is the bound for T?
– user7
Nov 8 at 16:56












What do you mean by the bound for T ? T could represent any class for instance in one of my test, it's List<Cell>, sometimes, List<Integer>.
– Tirth Patel
Nov 8 at 17:02




What do you mean by the bound for T ? T could represent any class for instance in one of my test, it's List<Cell>, sometimes, List<Integer>.
– Tirth Patel
Nov 8 at 17:02












In other words what is a state as described in the problem statement
– user7
Nov 8 at 17:03






In other words what is a state as described in the problem statement
– user7
Nov 8 at 17:03



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212512%2fcheck-to-see-if-i-have-a-valid-solution-to-an-underlying-probelm-in-an-8-puzzle%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212512%2fcheck-to-see-if-i-have-a-valid-solution-to-an-underlying-probelm-in-an-8-puzzle%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word