Ai Lab
Ai Lab
Ai Lab
import java.util.Scanner;
int n,i,j;
n=sc.nextInt();
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++)
graph[i][j]=sc.nextInt();
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ System.out.print(graph[i][j]+ "\t");
}
System.out.println();
}}}
OUTPUT
PROGRAM
import java.io.*;
import java.util.*;
// 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;
this.id = idCounter++;
this.neighbors = new ArrayList<>();
}
@Override
public int compareTo(Node n) {
return Double.compare(this.f, n.f);
}
public static class Edge {
Edge(int weight, Node node){
this.weight = weight;
this.node = node;
}
while(!openList.isEmpty()){
Node n = openList.peek();
if(n == target){
return n;
}
for(Node.Edge edge : n.neighbors){
Node m = edge.node;
double totalWeight = n.g + edge.weight;
if(closedList.contains(m)){
closedList.remove(m);
openList.add(m);
}
}
}
}
openList.remove(n);
closedList.add(n);
}
return null;
}
public static void printPath(Node target){
Node n = target;
if(n==null)
return;
while(n.parent != null){
ids.add(n.id);
n = n.parent;
}
ids.add(n.id);
Collections.reverse(ids);
for(int id : ids){
System.out.print(id + " ");
}
System.out.println("");
}
public static void main(String[] args) {
Node head = new Node(3);
head.g = 0;
head.addBranch(1, n1);
head.addBranch(5, n2);
head.addBranch(2, n3);
n3.addBranch(1, n2);
n1.addBranch(7,n4);
n2.addBranch(4, n5);
n3.addBranch(6, n4);
n4.addBranch(3, target);
n5.addBranch(1, n4);
n5.addBranch(3, target);
Node res = aStar(head, target);
printPath(res);
}
}
OUTPUT
PROGRAM
import java.util.Scanner;
initializeBoard();
while (!gameFinished) {
displayBoard();
playerMove(currentPlayer);
displayBoard();
printResult();
scanner.close();
row[j] = EMPTY;
}
}
System.out.println(" -------------");
System.out.print("| ");
do {
row = scanner.nextInt() - 1;
col = scanner.nextInt() - 1;
BOARD[row][col] = player;
return row >= 0 && row < 3 && col >= 0 && col < 3 && BOARD[row][col] ==
EMPTY;
if (cell == EMPTY) {
return false;
if (checkWin(PLAYER_X)) {
System.out.println("Player X wins!");
} else if (checkWin(PLAYER_O)) {
System.out.println("Player O wins!");
} else {
System.out.println("It's a draw!"); }}}
OUTPUT
PROGRAM
import java.util.LinkedList;
import java.util.Queue;
qe.v = 0;
qe.dist = 0;
visited[0] = 1;
q.add(qe);
while (!q.isEmpty()) {
qe = q.remove();
int v = qe.v;
if (v == n - 1)
break;
++j) {
if (visited[j] == 0) {
visited[j] = 1;
if (move[j] != -1)
a.v = move[j];
else
a.v = j;
q.add(a);
return qe.dist;
int N = 30;
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;
+ getMinDiceThrows(moves, N));
OUTPUT
PROGRAM
import java.util.Random;
import java.util.Scanner;
playRockPaperScissors(scanner);
if (playerMove.equals(computerMove)) {
System.out.println("It's a draw!");
System.out.println("Player wins!");
} else {
System.out.println("Computer wins!");
}
OUTPUT
PROGRAM
import java.io.*;
import java.util.*;
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)
System.out.print("Q ");
else
System.out.print(". ");
}
System.out.println();
}
}
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) {
System.out.print("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(System.in);
System.out.println("Enter the no of queens : ");
int n = in.nextInt();
Queen.solveNQ(n);
}
}
OUTPUT
PROGRAM
import java.util.*;
import java.io.*;
import java.util.Scanner;
class TSP
{
static int findHamiltonianCycle(int[][] distance, boolean[] visitCity, int currPos, int cities,
int count, int cost, int hamiltonianCycle)
{