Question

In: Computer Science

Prolog On a 8x8 chessboard we use the predicate sq(X,Y) to identify a position (where X,...

Prolog

On a 8x8 chessboard we use the predicate sq(X,Y) to identify a position (where X, Y are the corresponding square’s co-ordinates). A chess knight can move two squares in a left-right or front-back direction and then one square in a vertical fashion (https://en.wikipedia.org/wiki/Knight_(chess)).

We have defined the knightpath/1 predicate in the following incomplete Prolog program, which succeeds if invoked with a list of squares which are a valid position sequence for a chess knight.

Fill in the (ten) blanks to complete the program.

Then, show a Prolog query which succeeds by invoking knightpath/1 with a list of at least four successive legal knight moves.

knightpath([sq(X,Y)]) :-

inboard(X),

___1___.

knightpath([S1,S2|R]) :-

jump(___2___),

knightpath(___3___).

jump(sq(X1,Y1),sq(X2,Y2)) :-

___4___,

inboard(Y1), move(Dx,Dy),

direction(Sx),

___5___,

X2 is X1+Sx*Dx,

___6___.

move(1,2).

move(___7___).

direction(1).

___8___.

___9___ (C) :-

0 < C,

___10___   .

Solutions

Expert Solution

// CPP program to find number of possible moves of knight  


#include <bits/stdc++.h> 
#define n 4 
#define m 4 
using namespace std; 

// To calculate possible moves 
int findPossibleMoves(int mat[n][m], int p, int q) 
{ 
        // All possible moves of a knight 
        int X[8] = { 2, 1, -1, -2, -2, -1, 1, 2 }; 
        int Y[8] = { 1, 2, 2, 1, -1, -2, -2, -1 }; 

        int count = 0; 

        // Check if each possible move is valid or not 
        for (int i = 0; i < 8; i++) { 

                // Position of knight after move 
                int x = p + X[i]; 
                int y = q + Y[i]; 

                // count valid moves 
                if (x >= 0 && y >= 0 && x < n && y < m 
                        && mat[x][y] == 0) 
                        count++; 
        } 

        // Return number of possible moves 
        return count; 
} 

// Driver program to check findPossibleMoves() 
int main() 
{ 
        int mat[n][m] = { { 1, 0, 1, 0 }, 
                                        { 0, 1, 1, 1 }, 
                                        { 1, 1, 0, 1 }, 
                                        { 0, 1, 1, 1 } }; 

        int p = 2, q = 2; 

        cout << findPossibleMoves(mat, p, q); 

        return 0; 
} 

Related Solutions

Let D(x, y) be the predicate defined on natural numbers x and y as follows: D(x,...
Let D(x, y) be the predicate defined on natural numbers x and y as follows: D(x, y) is true whenever y divides x, otherwise it is false. Additionally, D(x, 0) is false no matter what x is (since dividing by zero is a no-no!). Let P(x) be the predicate defined on natural numbers that is true if and only if x is a prime number. 1. Write P(x) as a predicate formula involving quantifiers, logical connectives, and the predicate D(x,...
Please explain this prolog code line by line. union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W). union([X|Y],Z,[X|W]) :- \+ member(X,Z),...
Please explain this prolog code line by line. union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W). union([X|Y],Z,[X|W]) :- \+ member(X,Z), union(Y,Z,W). union([],Z,Z).
A particle moves with position r(t) = x(t) i + y(t) j where x(t) = 10t2...
A particle moves with position r(t) = x(t) i + y(t) j where x(t) = 10t2 and y(t) = -3t + 2, with x and y in meters and t in seconds. (a) Find the average velocity for the time interval from 1.00 s to 3.00 s. (b) Find the instantaneous velocity at t = 1.00 s. (c) Find the average acceleration from 1.00 s to 3.00 s. (d) Find the instantaneous acceleration at t = 1.00 s.
Use Euler's method with step size 0.2 to estimate y(0.4), where  y(x) is the solution of the...
Use Euler's method with step size 0.2 to estimate y(0.4), where  y(x) is the solution of the initial-value problem y' = 3x − 4xy, y(0) = 0. (Round your answer to four decimal places.) y(0.4) = (b) Repeat part (a) with step size 0.1. (Round your answer to four decimal places.) y(0.4) =
Use Euler's method with the given step size to estimate y(1.4) where y(x) is the solution...
Use Euler's method with the given step size to estimate y(1.4) where y(x) is the solution of the initial-value problem y′=x−xy ,y(1)=3.
Suppose that the utility function of a consumer is U(x,y) = x ¼y ¾, where x...
Suppose that the utility function of a consumer is U(x,y) = x ¼y ¾, where x and y are the quantities of the good X and good Y consumed, respectively. The consumer's income is 400. (a) What is the demanded bundle when the price of good X is 10 and the price of good Y is 10? (b) Redo part (a) when the price of good X is doubled? (c) Redo part (a) when the price of good Y is...
Consider walks in the X-Y plane where each step is R: (x, y)→(x+1, y) or U:...
Consider walks in the X-Y plane where each step is R: (x, y)→(x+1, y) or U: (x, y)→(x, y+a), with a a positive integer. There are five walks that contain a point on the line x + y = 2, namely:  RR, RU1, U1R, U1U1, and U2. Let a_n denote the number of walks that contain a point on the line x + y = n (so a_2 = 5). Show that a_n = F_{2n}, where F_n are the Fibonacci numbers...
Let the random variable and have the joint pmf X Y f(x,y) = {x(y)^2}/c where x...
Let the random variable and have the joint pmf X Y f(x,y) = {x(y)^2}/c where x = 1, 2, 3 ; y = 1, 2, x+y<= 4, that is (x,y) are {(1,1), (1,2), (2,1), (2,2), 3,1)} (a) Find . c > 0 (b) Find . μX (c) Find . μY (d) Find . σ2 X (e) Find . σ2 Y (f) Find Cov . (X,Y ) (g) Find p , Corr . (x,y) (h) Are and X and Y independent
let the random variables x and y have the joint p.m.f f(x,y)= (x+y)/12 where x=1,2 and...
let the random variables x and y have the joint p.m.f f(x,y)= (x+y)/12 where x=1,2 and y=1,2 calculate the covariance of x and y calculate the correlation coefficient of x and y Thank you
Let F (x, y) = y sin x i – cos x j, where C is...
Let F (x, y) = y sin x i – cos x j, where C is the line segment from (π/2,0) to (π, 1). Then C F•dr is A 1 B 2 C 5/2 D 3 E 7/2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT