Black and white pieces exchange

Source: Internet
Author: User

Problem:

In a box with seven squares, there are 3 white balls on the left, the fourth one is empty, and the three on the right is a black ball.

Game rules:

1. Only one piece can be moved at a time

2. The pawn can move to the space, or you can skip one of the other's pieces into the space.

3. The white pawn can only move to the right, the black pawn can only move to the left, cannot skip two sub.

#include <stdio.h> #include <stdlib.h>int number;void print (int a[]), void change (int *n, int *m);/* Run this PR Ogram using the console pauser or add your own getch, System ("pause") or input loop */int main (int argc, char *argv[]) {in  T t[7] = {1,1,1,0,2,2,2};int I, flag;print (t);//If you do not complete the swap of the pawn, continue with the loop while (t[0]+t[1]+t[2]! = 6 | | t[4]+t[5]+t[6]! = 3) {flag = 1;//flag a mark that moves one step for a pawn, flag=1 means that the pawn has not moved, otherwise it has moved for (i=0; flag&& i<5; i++) if (t[i]==1 && t[i+1]== 2 && Amp t[i+2]==0) {change (&t[i], &t[i+2]);p rint (t); flag = 0;} for (i=0; flag && i<5; i++) if (t[i] = = 0 && t[i+1]==1 && t[i+2]==2) {change (&t[i], &t[i+2 ]);p rint (t); flag=0;} For (i=0, flag&&i<6; i++) if (t[i]==1&&t[i+1]==0&& (i==0 | | t[i-1]!= t[i+2]) {Change (&t[ I], &t[i+1]);p rint (t); flag=0;} for (i=0; flag&&i<6; i++) if (t[i]==0 && t[i+1]==2&& (i==5 | | t[i-1]! = t[i+2]) {Change (&t[ I], &t[i+1]);p rint (t); flag=0;}} Return 0;} void print (int a[]) {int i;printf ("No.%2d:................\n", number++);p rintf (""); for (i=0; i<=6; i++) printf ("| %c ", a[i]==1? ' * ': (a[i]==2? ') @ ': ');p rintf ("\\n............\n\n");} void change (int *n, int * m) {int temp;temp = *n;*n = *m;*m = temp;}  /*no. 0: .......... | * | * |   * | | @ | @ | @ \ n ......  No. 1: .......... | * |   * | | * | @ | @ | @ \ n ......  No. 2: .......... | * | * | @ |   * | | @ | @ \ n ......  No. 3: .......... | * | * | @ | * |   @ | | @ \ n ......  No. 4: .......... | * | * |   @ | | @ | * | @ \ n ......  No. 5: .......... |   * | | @ | * | @ | * | @ \ n ......  No.   6: .......... | | * | @ | * | @ | * | @ \ n ......  No. 7: .......... | @ |   * | | * | @ | * | @ \ n ......  No. 8: .......... | @ | * | @ |   * | | * | @ \ n ......  No. 9: .......... | @ | * | @ | * | @ |   * | \ n ...... No. 10: ......... | @ | * | @ | * |   @ | | * \ n ...... No. 11: .......... | @ | * |   @ | | @ | * | * \ n ...... No. 12: ......... |   @ | | @ | * | @ | * | * \ n ...... No. 13: ......... | @ |   @ | | * | @ | * | * \ n ...... No. 14: ......... | @ | @ | @ |   * | | * | * \ n ...... No. 15: ......... | @ | @ |   @ | | * | * |  * \ n ...... This problem is the analysis of white Chess and black rules of the walk. */


Black and white pieces exchange

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.