PROGRAM
import [Link];
public class hillClimbing {
public static void main(String[] args) {
int n,i,j;
Scanner sc=new Scanner([Link]);
[Link]("Enter number of nodes in graph");
n=[Link]();
int[][] graph=new int[n][n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
graph[i][j]=0;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
[Link]("Is "+i+" is connected to "+ j);
graph[i][j]=[Link]();
[Link]("The adjacency matrix is:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ [Link](graph[i][j]+ "\t");
}
[Link]();
}}}
OUTPUT
PROGRAM
import [Link].*;
import [Link].*;
public class Node implements Comparable<Node> {
// Id for readability of result purposes
private static int idCounter = 0;
public int id;
// Parent in the path
public Node parent = null;
public List<Edge> neighbors;
// Evaluation functions
public double f = Double.MAX_VALUE;
public double g = Double.MAX_VALUE;
// Hardcoded heuristic
public double h;
Node(double h){
this.h = h;
[Link] = idCounter++;
[Link] = new ArrayList<>();
}
@Override
public int compareTo(Node n) {
return [Link](this.f, n.f);
}
public static class Edge {
Edge(int weight, Node node){
[Link] = weight;
[Link] = node;
}
public int weight;
public Node node;
}
public void addBranch(int weight, Node node){
Edge newEdge = new Edge(weight, node);
[Link](newEdge);
}
public double calculateHeuristic(Node target){
return this.h;
}
public static Node aStar(Node start, Node target){
PriorityQueue<Node> closedList = new PriorityQueue<>();
PriorityQueue<Node> openList = new PriorityQueue<>();
start.f = start.g + [Link](target);
[Link](start);
while(![Link]()){
Node n = [Link]();
if(n == target){
return n;
}
for([Link] edge : [Link]){
Node m = [Link];
double totalWeight = n.g + [Link];
if( && ){
[Link] = n;
m.g = totalWeight;
m.f = m.g + [Link](target);
[Link](m);
} else {
if(totalWeight < m.g){
[Link] = n;
m.g = totalWeight;
m.f = m.g + [Link](target);
if([Link](m)){
[Link](m);
[Link](m);
}
}
}
}
[Link](n);
[Link](n);
}
return null;
}
public static void printPath(Node target){
Node n = target;
if(n==null)
return;
List<Integer> ids = new ArrayList<>();
while([Link] != null){
[Link]([Link]);
n = [Link];
}
[Link]([Link]);
[Link](ids);
for(int id : ids){
[Link](id + " ");
}
[Link]("");
}
public static void main(String[] args) {
Node head = new Node(3);
head.g = 0;
Node n1 = new Node(2);
Node n2 = new Node(2);
Node n3 = new Node(2);
[Link](1, n1);
[Link](5, n2);
[Link](2, n3);
[Link](1, n2);
Node n4 = new Node(1);
Node n5 = new Node(1);
Node target = new Node(0);
[Link](7,n4);
[Link](4, n5);
[Link](6, n4);
[Link](3, target);
[Link](1, n4);
[Link](3, target);
Node res = aStar(head, target);
printPath(res);
}
}
OUTPUT
PROGRAM
import [Link];
public class tic_tac_toe {
private static final char EMPTY = ' ';
private static final char PLAYER_X = 'X';
private static final char PLAYER_O = 'O';
private static final char[][] BOARD = new char[3][3];
private static final Scanner scanner = new Scanner([Link]);
public static void main(String[] args) {
initializeBoard();
char currentPlayer = PLAYER_X;
boolean gameFinished = false;
while (!gameFinished) {
displayBoard();
playerMove(currentPlayer);
gameFinished = checkWin(currentPlayer) || checkDraw();
currentPlayer = (currentPlayer == PLAYER_X) ? PLAYER_O : PLAYER_X;
displayBoard();
printResult();
[Link]();
static void initializeBoard() {
for (char[] row : BOARD) {
for (int j = 0; j < [Link]; j++) {
row[j] = EMPTY;
}
}
static void displayBoard() {
[Link](" -------------");
for (char[] row : BOARD) {
[Link]("| ");
for (char cell : row) {
[Link](cell + " | ");
[Link]("\n ------------- ");
static void playerMove(char player) {
int row, col;
do {
[Link]("Player " + player + ", enter your move (row[1-3] column[1-3]):
");
row = [Link]() - 1;
col = [Link]() - 1;
} while (!isValidMove(row, col));
BOARD[row][col] = player;
static boolean isValidMove(int row, int col) {
return row >= 0 && row < 3 && col >= 0 && col < 3 && BOARD[row][col] ==
EMPTY;
static boolean checkWin(char player) {
for (int i = 0; i < 3; i++) {
if (BOARD[i][0] == player && BOARD[i][1] == player && BOARD[i][2] == player
||
BOARD[0][i] == player && BOARD[1][i] == player && BOARD[2][i] ==
player) {
return true; // Row or Column win
return BOARD[0][0] == player && BOARD[1][1] == player && BOARD[2][2] ==
player ||
BOARD[0][2] == player && BOARD[1][1] == player && BOARD[2][0] ==
player; // Diagonal win
static boolean checkDraw() {
for (char[] row : BOARD) {
for (char cell : row) {
if (cell == EMPTY) {
return false;
return true; // Board full, game draw
static void printResult() {
if (checkWin(PLAYER_X)) {
[Link]("Player X wins!");
} else if (checkWin(PLAYER_O)) {
[Link]("Player O wins!");
} else {
[Link]("It's a draw!"); }}}
OUTPUT
PROGRAM
import [Link];
import [Link];
public class snakeandladder {
// An entry in queue used in BFS
static class qentry {
int v; // Vertex number
int dist; // Distance of this vertex from source
static int getMinDiceThrows(int move[], int n)
int visited[] = new int[n];
Queue<qentry> q = new LinkedList<>();
qentry qe = new qentry();
qe.v = 0;
[Link] = 0;
// Mark the node 0 as visited and enqueue it.
visited[0] = 1;
[Link](qe);
// Do a BFS starting from vertex at index 0
while (![Link]()) {
qe = [Link]();
int v = qe.v;
if (v == n - 1)
break;
for (int j = v + 1; j <= (v + 6) && j < n;
++j) {
if (visited[j] == 0) {
qentry a = new qentry();
[Link] = ([Link] + 1);
visited[j] = 1;
if (move[j] != -1)
a.v = move[j];
else
a.v = j;
[Link](a);
return [Link];
public static void main(String[] args)
int N = 30;
int moves[] = new int[N];
for (int i = 0; i < N; i++)
moves[i] = -1;
// Ladders
moves[2] = 21;
moves[4] = 7;
moves[10] = 25;
moves[19] = 28;
// Snakes
moves[26] = 0;
moves[20] = 8;
moves[16] = 3;
moves[18] = 6;
[Link]("Min Dice throws required is "
+ getMinDiceThrows(moves, N));
OUTPUT
PROGRAM
import [Link];
import [Link];
public class stonepaper {
private static final String[] MOVES = {"rock", "paper", "scissors"};
private static final Random random = new Random();
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("How many rounds of Rock-Paper-Scissors would you like to
play?");
int rounds = [Link]([Link]());
for (int i = 0; i < rounds; i++) {
playRockPaperScissors(scanner);
static void playRockPaperScissors(Scanner scanner) {
[Link]("Make a move! (rock/paper/scissors)");
String playerMove = [Link]();
String computerMove = MOVES[[Link]([Link])];
[Link]("Computer chose " + computerMove + "!");
if ([Link](computerMove)) {
[Link]("It's a draw!");
} else if (playerWins(playerMove, computerMove)) {
[Link]("Player wins!");
} else {
[Link]("Computer wins!");
}
static boolean playerWins(String playerMove, String computerMove) {
return ([Link]("rock") && [Link]("scissors")) ||
([Link]("paper") && [Link]("rock")) ||
([Link]("scissors") && [Link]("paper"));
OUTPUT
PROGRAM
import [Link].*;
import [Link].*;
public class N_queens {
void printSolution(int board[][], int N)
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] == 1)
[Link]("Q ");
else
[Link](". ");
}
[Link]();
}
}
boolean isSafe(int board[][], int row, int col, int N)
{
int i, j;
for (i = 0; i < col; i++)
if (board[row][i] == 1)
return false;
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j] == 1)
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j] == 1)
return false;
return true;
}
boolean solveNQUtil(int board[][], int col, int N)
{
if (col >= N)
return true;
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col, N)) {
board[i][col] = 1;
if (solveNQUtil(board, col + 1, N) == true)
return true;
board[i][col] = 0; // BACKTRACK
}
}
return false;
}
boolean solveNQ(int N)
{
int board[][] = new int[N][N];
for(int i = 0; i< N ; i++){
for(int j = 0; j < N ; j++)
board[i][j] = 0;
}
if (solveNQUtil(board, 0 , N) == false) {
[Link]("Solution does not exist");
return false;
}
printSolution(board,N);
return true;
}
public static void main(String args[])
{
N_queens Queen = new N_queens();
Scanner in = new Scanner([Link]);
[Link]("Enter the no of queens : ");
int n = [Link]();
[Link](n);
}
}
OUTPUT
PROGRAM
import [Link].*;
import [Link].*;
import [Link];
class TSP
{
static int findHamiltonianCycle(int[][] distance, boolean[] visitCity, int currPos, int cities,
int count, int cost, int hamiltonianCycle)
{
if (count == cities && distance[currPos][0] > 0)
{
hamiltonianCycle = [Link](hamiltonianCycle, cost + distance[currPos][0]);
return hamiltonianCycle;
}
for (int i = 0; i < cities; i++)
{
if (visitCity[i] == false && distance[currPos][i] > 0)
{
visitCity[i] = true;
hamiltonianCycle = findHamiltonianCycle(distance, visitCity, i, cities, count + 1,
cost + distance[currPos][i], hamiltonianCycle);
visitCity[i] = false;
}
}
return hamiltonianCycle;
}
public static void main(String[] args)
{
int cities;
Scanner sc = new Scanner([Link]);
[Link]("Enter total number of cities ");
cities = [Link]();
int distance[][] = new int[cities][cities];
for( int i = 0; i < cities; i++){
for( int j = 0; j < cities; j++){
[Link]("Distance from city"+ (i+1) +" to city"+ (j+1) +": ");
distance[i][j] = [Link]();
}
}
boolean[] visitCity = new boolean[cities];
visitCity[0] = true;
int hamiltonianCycle = Integer.MAX_VALUE;
hamiltonianCycle = findHamiltonianCycle(distance, visitCity, 0, cities, 1, 0,
hamiltonianCycle);
[Link]("Shortest path that visit all nodes is "+hamiltonianCycle);
}
}
OUTPUT