Professional Documents
Culture Documents
Programtodesigntic 141126220143 Conversion Gate01 PDF
Programtodesigntic 141126220143 Conversion Gate01 PDF
On
Artificial Intelligence Lab
Submitted to
Bon Maharaj Engineering College, Vrindavan (Mathura)
Affiliated to Uttar Pradesh Technical University, Lucknow (UP)
In partial fulfillment of the degree of
Bachelor of Technology
In
Computer Science & Engineering
(8thSEM)
Submitted By
Vasundhara Ghosh
Roll No: 1036710012
BON MAHARAJ ENGINEERING COLLEGE
VRINDAVAN (MATHURA)
1
Page
INDEX
S. NO. Title Page No. Remarks
#include<stdio.h>
#include<conio.h>
void Board();
voidPlayerX();
voidPlayerO();
voidPlayer_win();
void check();
int win=0,wrong_X=0,wrong_O=0,chk=0;
charname_X[30];
charname_O[30];
intpos_for_X[3][3];
intpos_for_O[3][3];
intpos_marked[3][3];
void main()
{
inti,ch,j;
charans;
/* clrscr();
printf("\n\t\t\t\tTIC TAC TOE");
printf("\n\t\t\t\t");
for(i=1;i<=11;i++)
{
delay(10000);
printf("*");
}*/
do
{
clrscr();
printf("\n\t\t\t\tTIC TAC TOE");
printf("\n\t\t\t\t");
for(i=1;i<=11;i++)
{
delay(10000);
printf("*");
}
printf("\n1.Start The Game");
printf("\n2.Quit The Game");
printf("\nEnter your choice(1-2) : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
3
chk=0;
Page
win=0;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
pos_for_X[i][j]=0;
pos_for_O[i][j]=0;
pos_marked[i][j]=0;
}
}
printf("\n\n");
clrscr();
printf("\nEnter the name of the player playing
for \'X\': ");
fflush(stdin);
gets(name_X);
printf("\nEnter the name of the player playing
for \'O\': ");
fflush(stdin);
gets(name_O);
Board();
for(;;)
{
if(win==1)
break;
check();
if(chk==9)
{
printf("\n\t\t\t MATCH DRAWS!!");
printf("\nPress any key....");
break;
}
else
chk=0;
printf("\nTURN FOR %s:",name_X);
PlayerX();
do
{
if(wrong_X!=1)
break;
wrong_X=0;
printf("\nTURN FOR %s:",name_X);
PlayerX();
}while(wrong_X==1);
check();
if(chk==9)
{
printf("\n\t\t\tMATCH DRAWS");
printf("\nPress any key....");
break;
}
else
chk=0;
printf("\nTURN FOR %s:",name_O);
4
PlayerO();
Page
do
{
if(wrong_O!=1)
break;
wrong_O=0;
printf("\nTURN FOR %s:",name_O);
PlayerO();
}while(wrong_O==1);
}
Board();
if(win!=1)
{
printf("\n\t\t\tMATCH DRAWS!!");
printf("\nPress any key.......");
}
getch();
break;
case 2:
printf("\n\n\n\t\t\tThank You For Playing The
Game.");
printf("\n\t\t\t###############################");
getch();
exit(1);
break;
}
printf("\nWant To Play(Y/N) ? ");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y' || ans=='Y');
}
void Board()
{
inti,j;
clrscr();
printf("\n\t\t\t\tTIC TAC TOE BOARD");
printf("\n\t\t\t\t*****************");
printf("\n\n\n");
printf("\n\t\t\t 1\t 2\t 3");
for(i=1;i<=3;i++)
{
printf("\n \t\t\t _____________________________");
printf("\n \t\t\tº\t º\t º\t º");
printf("\n\t\t%d\t",i);
for(j=1;j<=3;j++)
{
if(pos_for_X[i][j]==1)
{
printf(" X");
printf(" ");
}
5
else if(pos_for_O[i][j]==1)
Page
{
printf(" O");
printf(" ");
}
else
{
printf(" ");
continue;
}
}
printf("\n\t\t\tº\t º\t º\t º");
}
printf("\n\t\t\t------------------------------");
Player_win();
}
voidPlayerX()
{
introw,col;
if(win==1)
return;
printf("\nEnter the row no. : ");
fflush(stdin);
scanf("%d",&row);
printf("Enter the column no. : ");
fflush(stdin);
scanf("%d",&col);
if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 ||
col>3)
{
printf("\nWRONG POSITION!! Press any key.....");
wrong_X=1;
getch();
Board();
}
else
{
pos_for_X[row][col]=1;
pos_marked[row][col]=1;
Board();
}
}
voidPlayerO()
{
introw,col;
if(win==1)
return;
printf("\nEnter the row no. : ");
scanf("%d",&row);
printf("Enter the column no. : ");
scanf("%d",&col);
if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 ||
col>3)
{
6
wrong_O=1;
getch();
Board();
}
else
{
pos_for_O[row][col]=1;
pos_marked[row][col]=1;
Board();
}
}
voidPlayer_win()
{
int i;
for(i=1;i<=3;i++)
{
if(pos_for_X[i][1]==1 &&pos_for_X[i][2]==1
&&pos_for_X[i][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key............");
return;
}
}
for(i=1;i<=3;i++)
{
if(pos_for_X[1][i]==1 &&pos_for_X[2][i]==1
&&pos_for_X[3][i]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key............");
return;
}
}
if(pos_for_X[1][1]==1 &&pos_for_X[2][2]==1
&&pos_for_X[3][3]==1)
{
win=1;
printf("\n\nRESULTL: %s wins!!",name_X);
printf("\nPress any key......");
return;
}
else if(pos_for_X[1][3]==1 &&pos_for_X[2][2]==1 &&
pos_for_X[3][1]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key.....");
return;
}
for(i=1;i<=3;i++)
{
7
if(pos_for_O[i][1]==1 &&pos_for_O[i][2]==1
Page
&&pos_for_O[i][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
for(i=1;i<=3;i++)
{
if(pos_for_O[1][i]==1 &&pos_for_O[2][i]==1
&&pos_for_O[3][i]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
if(pos_for_O[1][1]==1 &&pos_for_O[2][2]==1
&&pos_for_O[3][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
else if(pos_for_O[1][3]==1 &&pos_for_O[2][2]==1 &&
pos_for_O[3][1]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
void check()
{
inti,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(pos_marked[i][j]==1)
chk++;
else
continue;
}
}
}
8
Page
2. Program for Breath first search
#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
voidbfs(int v)
{
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main()
{
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n Bfs is not possible");
getch();
}
9
Page
3. Program to implement Depth –first-search
#include<stdio.h>
#include<conio.h>
char stack[20];
int top=-1, n;
chararr[20];
chardfs(int );
charajMat[20][20];
char b[20];
void display();
int p=0;
int main()
{
char v;
int l=0;
printf("Enter the number of nodes in a graph");
scanf("%d",&n);
printf("Enter the value of node of graph");
for(int i=0; i<n; i++)
{
scanf("%s",&b[i]);
}
char k=b[0];
printf("Enter the value in adjancency matrix in from of 'Y' or
'N'\n");
printf("\nIf there is an edge between the two vertices then
enter 'Y' or 'N'\n");
for(int i=0; i<n; i++)
printf(" %c ",b[i]);
for(int i=0;i<n; i++)
{
printf("\n%c ",b[i]);
for(int j=0; j<n; j++)
{
printf("%c ",v=getch());
ajMat[i][j]=v;
}
printf("\n\n");
}
for(int i=0;i<n;i++)
{
l=0;
while(k!=b[l])
10
l++;
k=dfs(l);
Page
}
display();
getch();
}
void display()
{
printf(" DFS of Graph : ");
for(int i=0; i<n; i++)
printf("%c ",arr[i]);
}
void push(char val)
{
top=top+1;
stack[top]=val;
}
char pop()
{
return stack[top];
}
boolunVisit(char val)
{
for(int i=0; i<p; i++)
if(val==arr[i])
return false;
for(int i=0; i<=top; i++)
if(val==stack[top])
return false;
return true;
}
chardfs(int i)
{
int k;
char m;
if(top==-1)
{
push(b[i]);
}
m=pop();
top--;
arr[p]=m;
p++;
for(int j=0; j<n; j++)
{
if(ajMat[i][j]=='y')
{
if(unVisit(b[j]))
{
push(b[j]);
}
}
}
11
return stack[top];
Page
}
4. Program to N-Queen’s Problem
#include<stdio.h>
#include<conio.h>
#include<math.h>
char a[10][10];
int n;
voidprintmatrix()
{
inti,j;
printf("n");
for(i=0;i <n;i++)
{
for(j=0;j <n;j++)
printf("%ct",a[i][j]);
printf("nn");
}
}
//------------------------------------------
intgetmarkedcol(int row)
{
inti,j;
for(i=0;i <n;i++)
if(a[row][i]=='Q')
{
return(i);
break;
}
}
//------------------------------------------
}
Page
//------------------------------------------
voidnqueen(int row)
{
inti,j;
if(row < n)
{
for(i=0;i <n;i++)
{
if(feasible(row,i))
{
a[row][i]='Q';
nqueen(row+1);
a[row][i]='.';
}
}
}
else
{
printf("nThe solution is:- ");
printmatrix();
}
}
//---------------------------------
void main()
{
inti,j;
clrscr();
for(i=0;i <n;i++)
for(j=0;j <n;j++)
a[i][j]='.';
nqueen(0);
getch();
} 13
Page
5. Program to implement N-Queen problem by iterative method
#include
#include
#include
#define TRUE 1
#define FALSE 0
voidprint_solution(intn,int x[])
{
char c[10][10];
inti,j;
for(i=1;i<=n;i++)
{
for(j=1; j<=n; j++)
{
c[i][j]=’-’;
}
}
for(i=1;i<=n;i++)
{
c[i][x[i]]=’Q’;
}
for( i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(“\t%c”,c[i][j]);
}
printf(“\n”);
}
}
int place(int x[],int k)
{ int i;
for(i=1;i<k;i++)
{
if(x[i]==x[k]||i-x[i]==k-x[k]||i+x[i]==k+x[k])
{
return FALSE;
}
}
return TRUE;
}
voidnqueens(int n)
{
int x[10];
int count=0;
14
int k=1;
x[k]=0;
Page
while(k!=0)
{
x[k]=x[k]+1;
while((x[k]<=n)&&(!place(x,k)))
{
x[k]=x[k]+1;
}
if(x[k]<=n)
{
if(k==n)
{
count++;
printf(“\n\tSolution %d is : \n\n\n”,count);
print_solution(n,x);
}
else
{
k++;
x[k]=0;
}
}
else
{
k–;
}
}
return;
}
void main()
{
int n;
clock_tstart,end;
clrscr();
start=clock();
printf(“\n Enter the no. of Queens : “);
scanf(“%d”,&n);
printf(“\n\n\t\t\t USING %d QUEEN’S STRATEGY \n\n”,n);
nqueens(n);
end=clock();
printf(“\n The Time Complexity is : %f msec.”,(end-
start)/CLK_TCK);
getch();
}
15
Page
6. WAP to solution of n-queen problem using array
#include<stdio.h>
#include<conio.h>
#include<math.h>
int a[30],count=0;
int place(int pos)
{
int i;
for(i=1;i<pos;i++)
{
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
return 0;
}
return 1;
}
void print_sol(int n)
{
int i,j;
count++;
printf("\n\nSolution #%d:\n",count);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i]==j)
printf("Q\t");
else
printf("*\t");
}
printf("\n");
}
}
void queen(int n)
{
int k=1;
a[k]=0;
while(k!=0)
{
a[k]=a[k]+1;
while((a[k]<=n)&&!place(k))
a[k]++;
if(a[k]<=n)
{
if(k==n)
16
print_sol(n);
else
Page
{
k++;
a[k]=0;
}
}
else
k--;
}
}
void main()
{
int i,n;
clrscr();
printf("Enter the number of Queens\n");
scanf("%d",&n);
queen(n);
printf("\nTotal solutions=%d",count);
getch();
}
17
Page
7. WAP to implement max -min problem
#include<stdio.h>
/*
* return a 2 element array
* First element is highest of list
* Second elements is lowest of list
*
*/
int* maxmin(const int list[], const int low, const int high, int
max, int min)
{
int mid,max1,min1;
static int maxAndMin[2]; // to hold the max and min value of
list
else
Page
{
// if list is not small, divide list into sub-problems
// Find where to split the set
mid = (low + high) / 2;
// Solve the sub-problems
max1 = list[mid+1];
min1 = list[mid+1];
maxmin(list, low, mid, max, min);
maxmin(list, mid+1, high, max1, min1);
// Combine the solutions
tempmax = getMax(tempmax, max1);
tempmin = getMin(tempmin, min1);
}
maxAndMin[0] = tempmax;
maxAndMin[1] = tempmin;
return maxAndMin;
}
/*
* returns the highest element between first and second
*
*/
int getMax(int first, int second)
{
return first > second ? first : second;
}
/*
* returns the lowest element between first and second
*
*/
int getMin(int first, int second)
{
return first < second ? first : second;
}
int main(void)
{
int list[] = {10, 23, 24, 56, 67, 78, 90};
int *values;
int size = ARRAY_SIZE(list);
return true;
}
}
Page
// Try different vertices as a next candidate in Hamiltonian
Cycle.
// We don't try for 0 as we included 0 as starting point in in
hamCycle()
for (int v = 1; v < V; v++)
{
/* Check if this vertex can be added to Hamiltonian Cycle */
if (isSafe(v, graph, path, pos))
{
path[pos] = v;
printSolution(path);
return true;
}
return 0;
}
22
Page