473,776 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calculate the horse's movement

12 New Member
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

//---------------------------------------------------------------------------

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. int control();
  5. void test(int,int);
  6. int secilecek(int , int );
  7. int kucuk(int *);
  8. void ekranabas();
  9. int hamle=0;
  10. typedef struct nokta
  11. {
  12. int a;
  13. int b;
  14. } nokta;
  15. nokta olasi[8];
  16. int x,y,gec,q,indis,temp;
  17. int matris[8][8]={0};
  18.  
  19. int main()
  20. {
  21. x=0;y=0;
  22.  
  23. ekranabas();
  24. printf("\n\n\t\tTo watch horse's movement please press any key\n\t\t The starting point is a1");
  25. getch();
  26. test(x,y);
  27.  
  28. getch();
  29. return 0;
  30. }
  31. //---------------------------------------------------------------------------
  32. void test(int x,int y)
  33. {
  34. for(int v=0;v<8;v++)
  35. {
  36. olasi[v].a=8;
  37. olasi[v].b=8;
  38. }
  39. if(x+1<8&&(x+1>-1)&&y+2<8&&y+2>-1&&(matris[x+1][y+2]==0))
  40. {
  41. olasi[0].a=x+1;
  42. olasi[0].b=y+2;
  43. }
  44. if(x-1<8&&x-1>-1&&y+2<8&&y+2>-1&&(matris[x-1][y+2]==0))
  45. {
  46. olasi[1].a=x-1;
  47. olasi[1].b=y+2;
  48. }
  49. if(x+1<8&&x+1>-1&&y-2<8&&y-2>-1&&(matris[x+1][y-2]==0))
  50. {
  51. olasi[2].a=x+1;
  52. olasi[2].b=y-2;
  53. }
  54. if(x-1<8&&x-1>-1&&y-2<8&&y-2>-1&&(matris[x-1][y-2]==0))
  55. {
  56. olasi[3].a=x-1;
  57. olasi[3].b=y-2;
  58. }
  59. if(x+2<8&&x+2>-1&&y-1<8&&y-1>-1&&(matris[x+2][y-1]==0))
  60. {
  61. olasi[4].a=x+2;
  62. olasi[4].b=y-1;
  63. }
  64. if(x+2<8&&x+2>-1&&y+1<8&&y+1>-1&&(matris[x+2][y+1]==0))
  65. {
  66. olasi[5].a=x+2;
  67. olasi[5].b=y+1;
  68. }
  69. if(x-2<8&&x-2>-1&&y-1<8&&y-1>-1&&(matris[x-2][y-1]==0))
  70. {
  71. olasi[6].a=x-2;
  72. olasi[6].b=y-1;
  73. }
  74. if(x-2<8&&x-2>-1&&y+1<8&&y+1>-1&&(matris[x-2][y+1]==0))
  75. {
  76. olasi[7].a=x-2;
  77. olasi[7].b=y+1;
  78. }
  79.  
  80. int tut[8];
  81. for(int t=0;t<8;t++)
  82. {
  83. if((olasi[t].a!=8))
  84. tut[t]=secilecek(olasi[t].a,olasi[t].b);
  85. else
  86. tut[t]=9;
  87. }
  88.  
  89. matris[x][y]=1;
  90. temp=tut[0];
  91. for(int t=0;t<8;t++)
  92. if((temp>tut[t]||temp==tut[t])&&matris[olasi[t].a][olasi[t].b]==0)
  93. temp=tut[t];
  94. for(q=0;q<8;q++)
  95. if(temp==tut[q])
  96. break;
  97. x=olasi[q].a;
  98. y=olasi[q].b;
  99.  
  100. ekranabas();
  101. hamle++;
  102.  
  103. for(int t=0;t<8;t++)
  104. tut[t]=9;
  105. if(!control())
  106. return;
  107. test(x,y);
  108.  
  109. }
  110.  
  111. int secilecek(int x, int y)
  112. {
  113.  
  114. int say=0;
  115. if(x+1<8&&(x+1>-1)&&y+2<8&&y+2>-1&&(matris[x+1][y+2]==0))
  116. say++;
  117. if(x-1<8&&x-1>-1&&y+2<8&&y+2>-1&&(matris[x-1][y+2]==0))
  118. say++;
  119. if(x+1<8&&x+1>-1&&y-2<8&&y-2>-1&&(matris[x+1][y-2]==0))
  120. say++;
  121. if(x-1<8&&x-1>-1&&y-2<8&&y-2>-1&&(matris[x-1][y-2]==0))
  122. say++;
  123. if(x+2<8&&x+2>-1&&y-1<8&&y-1>-1&&(matris[x+2][y-1]==0))
  124. say++;
  125. if(x+2<8&&x+2>-1&&y+1<8&&y+1>-1&&(matris[x+2][y+1]==0))
  126. say++;
  127. if(x-2<8&&x-2>-1&&y-1<8&&y-1>-1&&(matris[x-2][y+1]==0))
  128. say++;
  129. if(x-2<8&&x-2>-1&&y+1<8&&y+1>-1&&(matris[x-2][y+1]==0))
  130. say++;
  131. return say;
  132. }
  133.  
  134. int kucuk(int *g)
  135. {
  136.  
  137. return y;
  138. }
  139.  
  140. void ekranabas()
  141. {
  142. clrscr();
  143.  
  144. printf("\t\t\t   a  b  c  d  e  f  g  h\n");
  145. for(int i=0;i<8;i++)
  146. {printf("\t\t\t%d ",i+1);
  147. for(int j=0;j<8;j++)
  148. if((i+j)%2)
  149. {
  150. if(matris[i][j]==1)
  151. printf("xx ");
  152. else
  153. printf("\xdb\xdb\xdb");
  154. }
  155. else
  156. if(matris[i][j]==1)
  157. printf("xx ");
  158. else
  159. printf("   ");
  160. printf("\n");
  161. }
  162. printf("\n movement that made so far:%d",hamle);
  163. for(double g=0;g<9999999;g++);
  164.  
  165.  
  166. //getch();
  167. return ;
  168. }
  169.  
  170. int control()
  171. {
  172. for (int i=0;i<8;i++)
  173. for(int j=0;j<8;j++)
  174. if(matris[i][j]==0)
  175. return 1;
  176. return 0;
  177. }
Oct 19 '06 #1
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.

Expand|Select|Wrap|Line Numbers
  1. int solve(int chess_board[][], int last_col, int last_row, int count)
  2. {
  3.   if(count == 64)
  4.   {
  5.     // print the entire chess board;
  6.     // stopping case
  7.   }
  8.   // up 2, left 1 case
  9.   if((last_row > 1) && (last_col > 0))
  10.   {
  11.     if(chess_board[last_col-1][last_row-2] == 0) // not occupied
  12.     {
  13.       chess_board[last_col-1][last_row-2] = count;
  14.       solve(&chess_board, last_col-1, last_row-2, count+1);
  15.       chess_board[last_col-1][last_row-2] = 0;
  16.     }
  17.   }
  18.   // similarly for the other seven possibilities
  19. }
  20.  
  21. int main()
  22. {
  23.   for each row (0 - 7)
  24.   {
  25.     for each col (0 - 7)
  26.     {
  27.       chess_board[col][row] = 1;
  28.       solve(&chess_board, col, row, 1);
  29.       chess_board[row][col] = 0;
  30.     }
  31.   }
  32.   system("PAUSE");
  33.   return EXIT_SUCCESS;
  34. }
Oct 19 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6946
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...
53
5745
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
7
19989
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
1
1807
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?
4
6977
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.
5
2331
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...
8
25487
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...
0
9628
marktang
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...
0
10289
Oralloy
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...
0
10120
jinu1996
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...
1
10061
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,...
0
9923
tracyyun
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...
0
6722
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();...
0
5367
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...
0
5493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3622
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.