hi guys,
the problemm is that you have a horse on the chess board.You should write a function that calculate the horse's movement.I mean how many movement later you can go every point on the board.I wrote that program and wanted to share with you...
I am Turkish so I named functions in Turkish.So it can make the source misunderstable sorry for that
//--------------------------------------------------------------------------- - #include<stdio.h>
-
#include<conio.h>
-
-
int control();
-
void test(int,int);
-
int secilecek(int , int );
-
int kucuk(int *);
-
void ekranabas();
-
int hamle=0;
-
typedef struct nokta
-
{
-
int a;
-
int b;
-
} nokta;
-
nokta olasi[8];
-
int x,y,gec,q,indis,temp;
-
int matris[8][8]={0};
-
-
int main()
-
{
-
x=0;y=0;
-
-
ekranabas();
-
printf("\n\n\t\tTo watch horse's movement please press any key\n\t\t The starting point is a1");
-
getch();
-
test(x,y);
-
-
getch();
-
return 0;
-
}
-
//---------------------------------------------------------------------------
-
void test(int x,int y)
-
{
-
for(int v=0;v<8;v++)
-
{
-
olasi[v].a=8;
-
olasi[v].b=8;
-
}
-
if(x+1<8&&(x+1>-1)&&y+2<8&&y+2>-1&&(matris[x+1][y+2]==0))
-
{
-
olasi[0].a=x+1;
-
olasi[0].b=y+2;
-
}
-
if(x-1<8&&x-1>-1&&y+2<8&&y+2>-1&&(matris[x-1][y+2]==0))
-
{
-
olasi[1].a=x-1;
-
olasi[1].b=y+2;
-
}
-
if(x+1<8&&x+1>-1&&y-2<8&&y-2>-1&&(matris[x+1][y-2]==0))
-
{
-
olasi[2].a=x+1;
-
olasi[2].b=y-2;
-
}
-
if(x-1<8&&x-1>-1&&y-2<8&&y-2>-1&&(matris[x-1][y-2]==0))
-
{
-
olasi[3].a=x-1;
-
olasi[3].b=y-2;
-
}
-
if(x+2<8&&x+2>-1&&y-1<8&&y-1>-1&&(matris[x+2][y-1]==0))
-
{
-
olasi[4].a=x+2;
-
olasi[4].b=y-1;
-
}
-
if(x+2<8&&x+2>-1&&y+1<8&&y+1>-1&&(matris[x+2][y+1]==0))
-
{
-
olasi[5].a=x+2;
-
olasi[5].b=y+1;
-
}
-
if(x-2<8&&x-2>-1&&y-1<8&&y-1>-1&&(matris[x-2][y-1]==0))
-
{
-
olasi[6].a=x-2;
-
olasi[6].b=y-1;
-
}
-
if(x-2<8&&x-2>-1&&y+1<8&&y+1>-1&&(matris[x-2][y+1]==0))
-
{
-
olasi[7].a=x-2;
-
olasi[7].b=y+1;
-
}
-
-
int tut[8];
-
for(int t=0;t<8;t++)
-
{
-
if((olasi[t].a!=8))
-
tut[t]=secilecek(olasi[t].a,olasi[t].b);
-
else
-
tut[t]=9;
-
}
-
-
matris[x][y]=1;
-
temp=tut[0];
-
for(int t=0;t<8;t++)
-
if((temp>tut[t]||temp==tut[t])&&matris[olasi[t].a][olasi[t].b]==0)
-
temp=tut[t];
-
for(q=0;q<8;q++)
-
if(temp==tut[q])
-
break;
-
x=olasi[q].a;
-
y=olasi[q].b;
-
-
ekranabas();
-
hamle++;
-
-
for(int t=0;t<8;t++)
-
tut[t]=9;
-
if(!control())
-
return;
-
test(x,y);
-
-
}
-
-
int secilecek(int x, int y)
-
{
-
-
int say=0;
-
if(x+1<8&&(x+1>-1)&&y+2<8&&y+2>-1&&(matris[x+1][y+2]==0))
-
say++;
-
if(x-1<8&&x-1>-1&&y+2<8&&y+2>-1&&(matris[x-1][y+2]==0))
-
say++;
-
if(x+1<8&&x+1>-1&&y-2<8&&y-2>-1&&(matris[x+1][y-2]==0))
-
say++;
-
if(x-1<8&&x-1>-1&&y-2<8&&y-2>-1&&(matris[x-1][y-2]==0))
-
say++;
-
if(x+2<8&&x+2>-1&&y-1<8&&y-1>-1&&(matris[x+2][y-1]==0))
-
say++;
-
if(x+2<8&&x+2>-1&&y+1<8&&y+1>-1&&(matris[x+2][y+1]==0))
-
say++;
-
if(x-2<8&&x-2>-1&&y-1<8&&y-1>-1&&(matris[x-2][y+1]==0))
-
say++;
-
if(x-2<8&&x-2>-1&&y+1<8&&y+1>-1&&(matris[x-2][y+1]==0))
-
say++;
-
return say;
-
}
-
-
int kucuk(int *g)
-
{
-
-
return y;
-
}
-
-
void ekranabas()
-
{
-
clrscr();
-
-
printf("\t\t\t a b c d e f g h\n");
-
for(int i=0;i<8;i++)
-
{printf("\t\t\t%d ",i+1);
-
for(int j=0;j<8;j++)
-
if((i+j)%2)
-
{
-
if(matris[i][j]==1)
-
printf("xx ");
-
else
-
printf("\xdb\xdb\xdb");
-
}
-
else
-
if(matris[i][j]==1)
-
printf("xx ");
-
else
-
printf(" ");
-
printf("\n");
-
}
-
printf("\n movement that made so far:%d",hamle);
-
for(double g=0;g<9999999;g++);
-
-
-
//getch();
-
return ;
-
}
-
-
int control()
-
{
-
for (int i=0;i<8;i++)
-
for(int j=0;j<8;j++)
-
if(matris[i][j]==0)
-
return 1;
-
return 0;
-
}
1 4552 D_C 293
Contributor
A horse can only cover one square in one move, and there are 64 squares, you start on 1 initially, so at a minimum it can take 63 movements. You could do this problem recursively and search for a solution.
However, it may depend on the starting place too. If you just need to cover the entire board, it can be done. I suppose minimizing it would be the best answer. - int solve(int chess_board[][], int last_col, int last_row, int count)
-
{
-
if(count == 64)
-
{
-
// print the entire chess board;
-
// stopping case
-
}
-
// up 2, left 1 case
-
if((last_row > 1) && (last_col > 0))
-
{
-
if(chess_board[last_col-1][last_row-2] == 0) // not occupied
-
{
-
chess_board[last_col-1][last_row-2] = count;
-
solve(&chess_board, last_col-1, last_row-2, count+1);
-
chess_board[last_col-1][last_row-2] = 0;
-
}
-
}
-
// similarly for the other seven possibilities
-
}
-
-
int main()
-
{
-
for each row (0 - 7)
-
{
-
for each col (0 - 7)
-
{
-
chess_board[col][row] = 1;
-
solve(&chess_board, col, row, 1);
-
chess_board[row][col] = 0;
-
}
-
}
-
system("PAUSE");
-
return EXIT_SUCCESS;
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Robin Shuff |
last post by:
Hi,
I'm trying to limit the movement of the mouse cursor in using a VB app. The
idea is to stop the cursor straying on to the second monitor of a dual
screen set-up (i.e. a projector) while this program is running.
I've found and have been working with Knowledge Base Article 179192
(http://support.microsoft.com/default.aspx?scid=kb;en-us;179192) which
limits the mouse movement to inside a form, but I'd like to adapt this to be
limited...
|
by: Cardman |
last post by:
Greetings,
I am trying to solve a problem that has been inflicting my self
created Order Forms for a long time, where the problem is that as I
cannot reproduce this error myself, then it is difficult to know what
is going on.
One of these Order Forms you can see here...
http://www.cardman.co.uk/orderform.php3
|
by: sql-db2-dba |
last post by:
Does DB2 just fudge it when it is an empty table? Is there a "formula"
for average row size when you have variable length records. Or you
really have to know what your application is packing into those
varchar columns.
Bill Leung
leungb@aptea.com
|
by: Rocky A |
last post by:
I don't remember ever reading about this in any of the "how to" books
I've got or I wouldn't bother you guys (honest, I'm not being lazy
here.....OK maybe just a little, but you guys are SOOOO good)
I have a form along with it's multiple sub forms that I don't need or
want to display the record movement bar that automatically appears at
the bottom.
Is this something I can set at design time, and if so how?
|
by: mike |
last post by:
I have the opportunity to rescue a project that
uses a mouse to sense the relative position of a machine.
The hardware is built...just needs to be programmed.
Stop snickering!!! I didn't do it...I just gotta fix it.
I need to make some calculations on the measurements
and VB6 is my language.
Yes, the system mouse will corrupt the measurement,
but it's an auditing function and that's acceptable.
| |
by: hurricane_number_one |
last post by:
I am creating a simple server application, that will listen for
incoming mouse coordinates and then move the mouse accordingly. So
basically it's like a very simple VNC server without and screen
display. I have this basic part working. The problem is that response
time is really bad. It seems like the server is not receiving the data
fast enough to be able to move the mouse so that it appears to be in
sync with the movement on the client...
|
by: trixxnixon |
last post by:
I know this topic is a veritable dead horse, but I have to ask, because I am unable to find something that is close to my scenario that I can completely understand.
I have a start date field and complete date field.
I need to know how many days, minus holidays and weekends are between the two dates. i already have the table of holidays named "holiday table" with the field being named "Holidaydate".
I need this to output a number...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |