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.
java search 8-puzzle
add a comment |
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.
java search 8-puzzle
What is the bound forT?
– 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
add a comment |
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.
java search 8-puzzle
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
java search 8-puzzle
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 forT?
– 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
add a comment |
What is the bound forT?
– 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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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