Welcome![Sign In][Sign Up]
Location:
Search - 棋盘法

Search list

[Other用c编写的N*N的螺旋矩阵源代码

Description:

/*
实现效果:
1 2 6 7 15
3 5 8 14 16
4 9 13 17 22
10 12 18 21 23
11 19 20 24 25
*/
#include <stdio.h>
#define N 5 //阶数,即N*N的螺旋矩阵

void main()
{
    int i, j, num=1, a[N][N];
    for(i=0; i<=N/2; i++)
    {
        for(j=i; j<N-i; j++) a[i][j]=n++;
        for(j=i+1; j<N-i; j++) a[j][N-i-1]=n++;
        for(j=N-i-2; j>i; j--) a[N-i-1][j]=n++;
        for(j=N-i-1; j>i; j--) a[j][i]=n++;
    }
    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
            printf("%2d ",a[i][j]);
        printf("\n");
    }
}
    

 

不知道叫什么,先叫它“回宫图”吧
年初的时候在贴吧瞎逛,看到了一个程序挺有意思,会输出如下的形状:
01 24 23 22 21 20 19
02 25 40 39 38 37 18
03 26 41 48 47 36 17
04 27 42 49 46 35 16
05 28 43 44 45 34 15
06 29 30 31 32 33 14
07 08 09 10 11 12 13
仔细看这个形状,数字是按顺序往里回旋的,觉得很有创意,可是一看源代码头就大了,
每个编程人都知道看别人的代码是很困难的,尤其像这种不知道思路的,所以也就放下
没管了。
昨天上物理课实在是没心思听,就想起这个程序,想了一节课,果然不负有心人,给弄出来了,这个是增强版的,可以输入1-10中的任意个数,然后生成图形。
先看代码,没有注释,所以不好看的懂。
#include<stdio.h>
main()
{
       int n,m,i,j,t,k=1;
       int a[11][11];
       clrscr();
       do{
       printf("please input a number(1-10):");
       scanf("%d",&n);
       }while(n<1||n>10);
       t=n+1;
       for(m=1;m<=t/2;m++)
         {
           for(i=m;i<=t-m;i++)
             {a[i][m]=k;k++;}
           for(j=m+1;j<=t-m;j++)
             {a[i-1][j]=k;k++;}
           for(i=n-m;i>=m;i--)
             {a[i][j-1]=k;k++;}
           for(j=n-m;j>=m+1;j--)
             {a[i+1][j]=k;k++;}
         }
       for(i=1;i<=n;i++)
         {
           for(j=1;j<=n;j++)
             {
               if(a[i][j]<=9) printf("0%d ",a[i][j]);
               else printf("%d ",a[i][j]);       }
           printf("\n");
         }
       getch();
}
就是这样的。


可以更简洁些:

#include<stdio.h>
main()
{
       int n,m,i,j,t,k=1;
       int a[11][11];
       clrscr();
       do{
       printf("please input a number(1-10):");
       scanf("%d",&n);
       }while(n<1||n>10);
       t=n+1;
       for(m=1;m<=t/2;m++)
         {
           for(i=m;i<=t-m;i++)
             a[i][m]=k++;
           for(j=m+1;j<=t-m;j++)
             a[i-1][j]=k++;
           for(i=n-m;i>=m;i--)
             a[i][j-1]=k++;
           for(j=n-m;j>=m+1;j--)
             a[i+1][j]=k++;
         }
       for(i=1;i<=n;i++)
         {
           for(j=1;j<=n;j++)
             {
               if(a[i][j]<=9) printf("0%d ",a[i][j]);
               else printf("%d ",a[i][j]);       }
           printf("\n");
         }
       getch();
}

 


 #include <stdio.h>
#define N 8
main(){
 int i,j,n=1,a[N][N];
 for(i=0;i<=N/2;i++){
  for(j=i;j<N-i;j++)
   a[i][j]=n++;
  for(j=i+1;j<N-i;j++)
   a[j][N-i-1]=n++;
  for(j=N-i-2;j>i;j--)
   a[N-i-1][j]=n++;
  for(j=N-i-1;j>i;j--)
   a[j][i]=n++;
 }
 for(i=0;i<N;i++){
  printf("\n\n");
  for(j=0;j<N;j++)
   printf("%5d",a[i][j]);
 }
}
 

 


                                马踏棋盘问题


#include <stdio.h>
#define N 5
void main(){
 int x,y;
 void horse(int i,int j);
 printf("Please input start position:");
 scanf("%d%d",&x,&y);
 horse(x-1,y-1);
}
void horse(int i,int j){
 int a[N][N]={0},start=0,
  h[]={1,2,2,1,-1,-2,-2,-1},
  v[]={2,1,-1,-2,2,1,-1,-2},
  save[N*N]={0},posnum=0,ti,tj,count=0;
 int jump(int i,int j,int a[N][N]);
 void outplan(int a[N][N]);
 a[i][j]=posnum+1;
 while(posnum>=0){
  ti=i;tj=j;
  for(start=save[posnum];start<8;++start){
   ti+=h[start];tj+=v[start];
   if(jump(ti,tj,a))
    break;
   ti-=h[start];tj-=v[start];
  }
  if(start<8){
   save[posnum]=start;
   a[ti][tj]=++posnum+1;
   i=ti;j=tj;save[posnum]=0;
   if(posnum==N*N-1){
    //outplan(a);
    count++;
   }
  }
  else{
   a[i][j]=0;
   posnum--;
   i-=h[save[posnum>;j-=v[save[posnum>;
   save[posnum]++;
  }
 }
 printf("%5d",count);
}
int jump(int i,int j,int a[N][N]){
 if(i<N&&i>=0&&j<N&&j>=0&&a[i][j]==0)
  return 1;
 return 0;
}
void outplan(int a[N][N]){
 int i,j;
 for(i=0;i<N;i++){
  for(j=0;j<N;j++)
   printf("%3d",a[i][j]);
  printf("\n");
 }
 printf("\n");
 //getchar();
}
用回溯法得到所有的解,但效率较低,只能算出5行5列的

 


Platform: | Size: 4395 | Author: good@588 | Hits:

[Windows DevelopmyFiveChess

Description: 本“五子棋“程序只编写了人机对弈部分,运用了博弈树进行搜索,在选取最优的走步时使用极大极小分析法,考虑到搜索的时间复杂度和空间复杂度,在程序中只进行了2步搜索,即计算机在考虑下一步的走法时,只对玩家进行一步的推测。(程序中的棋盘规格为15*15)-\" quintet game \" this procedure is it is it partly , is it play chess tree search for to use to play chess while being man-machine to write only, use the great extremely small analytic approach while choosing the optimum walking, consider time complexity and space complexity searched for, only search for 2 steps in the procedure , namely the computer, while considering next walking in France,
Platform: | Size: 246329 | Author: 杨杰 | Hits:

[Other resource123a20041218121959

Description: 本人很喜欢玩棋类游戏,最近花了不到半个月的时间设计制作了一个现代战棋游戏,因为时间有限现在只做了个DEMO,还没有完成美工,网络对战等功能,以后会慢慢完善。 本人因为学JAVA总共不到半年,系统学习JAVA也就三个月,所以本游戏不可避免的参考了其他棋类制作的大概框架。不过除了技术实现方面参考了他人思路以外,上层的游戏规则、棋子和棋盘等游戏蓝图都是偶独自设计出来的。(本游戏的设计创意未经本人同意不得用于商业用途,本人保持对整个游戏的所有版权) 本游戏还有很多要完善的地方,整个游戏的玩法我也没那么时间考虑,大家试玩以后希望多多提出建议。 解压缩以后有3个文件,运行方法和游戏玩法请看帮助文件。 -I like to play board games, recently spent less than half the time the design of a modern board game, the time is now limited only had 000 DEMO, is not yet complete artists, network features such as the screen, there will gradually improve. As I am learning Java less than half the total, the system will learn Java three months, the inevitable game of chess as a reference framework probably produced. However, apart from the technical accomplishment reference to the ideas of others, the upper rules of the game, the pieces and board games are even own blueprint for the design. (This game design creativity without her consent shall not be used for commercial purposes, I maintain the whole game all copyright), the game still have a lot to perfect and the rules of the game I have not had time to
Platform: | Size: 20219 | Author: 许伟 | Hits:

[Linux-Unix回溯法

Description: 用回溯法求马周游问题,马在棋盘上走字步,从马开始的位置开始周游棋盘,遍历全棋盘后回到起点,是否可行,可行的就输出路径,路径并输入尝试过的路径数,跳过的路径数-law with retrospective travel for Ma, Ma on the chessboard characters step away from the start position Ma started to travel around the chessboard, traverse the whole chessboard return to the starting point and feasible, and viable on the output path, path and enter the path tried a few, the path to skip a few
Platform: | Size: 8814 | Author: 杨小娜 | Hits:

[Other resource连连看,C源码,棋盘法,带注解

Description: 连连看,C源码,棋盘法,带注解
Platform: | Size: 83244 | Author: qyjem@126.com | Hits:

[Chess Poker games小游戏:五子棋的设计与实现

Description: 五子棋的设计与实现 本“五子棋“程序只编写了人机对弈部分,运用了博弈树进行搜索,在选取最优的走步时使用极大极小分析法,考虑到搜索的时间复杂度和空间复杂度,在程序中只进行了2步搜索,即计算机在考虑下一步的走法时,只对玩家进行一步的推测。(程序中的棋盘规格为15*15)。-renju the design and implementation of the "331" procedure prepared by the Human-Computer Chess, a game using the search tree, in the selection of the optimal use of a treadmill Minimax analysis, taking into account the complex search and space complexity of the procedure only for a two-step search, Computers in the next step in law, only the right players to step estimate. (Procedure specifications for the chessboard 15* 15).
Platform: | Size: 246784 | Author: cwm_awt | Hits:

[Linux-Unix回溯法

Description: 用回溯法求马周游问题,马在棋盘上走字步,从马开始的位置开始周游棋盘,遍历全棋盘后回到起点,是否可行,可行的就输出路径,路径并输入尝试过的路径数,跳过的路径数-law with retrospective travel for Ma, Ma on the chessboard characters step away from the start position Ma started to travel around the chessboard, traverse the whole chessboard return to the starting point and feasible, and viable on the output path, path and enter the path tried a few, the path to skip a few
Platform: | Size: 955392 | Author: 杨小娜 | Hits:

[Windows Develop20050622235246_eq

Description: 八皇后VC图形演示,算法思想:回溯法,先在第1行放上一个皇后,然后在第2行合适的位置放上一个皇后,依次类推,如果8行都放满了,说明找到了一个解,如果第好第i行的皇后后,第i+1行找不到合适的位置,这时就回到第i行,把第i行的皇后放到下一个位置,继续尝试下一行。如此反复,知道找到所有的解。注意,这种算法找的解可能有等价的,某些解可由别的解经过旋转棋盘得到。-visual demostration in vc of eight queens problem. algorithm: backtracking. First, put a queen on first line then find a suitable position on 2nd line for next queen, and so on. When all eight lines have queens, it means we have find a solution. If we put the i-th queen, but can find a suitable position on the (i+1)th line, then back to the ith line and move the queen on the ith line to next suitable position and move on to the next line. Repeat this until we have found all the solutions. Note:By this mean, some solutions maybe equivalent to others, some can be acquired by the rotation of other solutions.
Platform: | Size: 52224 | Author: huwu | Hits:

[Windows DevelopmyFiveChess

Description: 本“五子棋“程序只编写了人机对弈部分,运用了博弈树进行搜索,在选取最优的走步时使用极大极小分析法,考虑到搜索的时间复杂度和空间复杂度,在程序中只进行了2步搜索,即计算机在考虑下一步的走法时,只对玩家进行一步的推测。(程序中的棋盘规格为15*15)-" quintet game " this procedure is it is it partly , is it play chess tree search for to use to play chess while being man-machine to write only, use the great extremely small analytic approach while choosing the optimum walking, consider time complexity and space complexity searched for, only search for 2 steps in the procedure , namely the computer, while considering next walking in France,
Platform: | Size: 245760 | Author: | Hits:

[Other GamesEightQueues

Description: 八皇后VC图形演 算法思想:回溯法,先在第1行放上一个皇后,然后在第2行合适的位置放上一个皇后,依次类推,如果8行都放满了,说明找到了一个解,如果第好第i行的皇后后,第i+1行找不到合适的位置,这时就回到第i行,把第i行的皇后放到下一个位置,继续尝试下一行。如此反复,知道找到所有的解。注意,这种算法找的解可能有等价的,某些解可由别的解经过旋转棋盘得到。-eight Queen's VC graphics algorithm thinking : A Retrospective, the first one placed on a Queen's OK, Then the two firms placed on the appropriate location of an empress, followed by analogy, if eight Out filled with that found a solution. If good first section of the Queen's trip i, i-a trip to find a suitable location, this time on the trip back to the i, i section of the Queen's trip into a position and will continue to the next line. So many, find all know the solutions. Note that the algorithm to find the solution may be equivalent to the solution may be some other solution is through rotating chessboard.
Platform: | Size: 60416 | Author: duyankang | Hits:

[source in ebookhuanghouwenti

Description: 在国际象棋盘上放八个皇后,互相不能攻击,有多少种摆法? 这个是经典的8皇后问题,解决此问题的方法是回溯法。 回溯法是一种思路简单而且有效的解决问题的基础算法。 解决一个问题的时候分成n个步骤,每向后进展一个步骤,就检查一下当前的状态,如果发生了冲突,就放弃,如果到达了目标状态就记录下答案,回溯,求解下一个解。 对于8皇后问题,每个步骤就是在棋盘上放一个棋子,每放一个棋子,就检查当前的状态,有否产生攻击,如果没有攻击,就继续放下一个,如果攻击了,就回溯,如果放够八个,就打印结果,然后回溯。-err
Platform: | Size: 3072 | Author: aa | Hits:

[Mathimatics-Numerical algorithmsKnight

Description: 1. 我们采用了回溯法和贪婪策略来求解国际象棋中的骑士巡游问题。对于棋盘中的每个位置最多只有8个方向可以选择,我们可以定义两个数组var_x[MAX_DIR]和var_y[MAX_DIR]用来记录往这8个方向走相对应的坐标变化情况(其中MAX_DIR的值为8)。每走一步,都从方向0开始试探到方向7,而在这里我加入贪婪策略来提高算法的效率,即在选择方向时,我们优先选择具有如下性质的方向:当我们沿着这个方向走一步后,走到这一步后可选的方向最少(最多有8个方向选择)。这样一直往前走, 当走到一个没有方向可以选择,并且我们还没遍历整个棋盘时,我们就要往回退一步,即回溯。再从其他未试探过的方向进行试探,直到最后遍历整个棋盘或者回到起点,程序结束。-err
Platform: | Size: 113664 | Author: 张海滨 | Hits:

[Data structsHorse

Description: 马跳棋盘的回溯法程序,基于mfc,有可视化界面,可以演示程序的运行过程-Horse jumping board of backtracking procedure, based on the mfc, has visualization interface, can run the course of the demo program
Platform: | Size: 2074624 | Author: siwei | Hits:

[Mathimatics-Numerical algorithms8queen

Description: 随机生成八皇后棋盘,利用爬山法选择较优方案-Randomly generated eight Queen s chessboard, using mountain climbing method to choose better programs
Platform: | Size: 184320 | Author: ls112112 | Hits:

[CSharpss

Description: 二分搜索,棋盘法,循环日程法-Binary Search, chessboard, cyclic scheduling method
Platform: | Size: 1024 | Author: ss | Hits:

[Data structsche

Description: 问题描述:按照国际象棋的规则,车可以攻击与之处在同一行或同一列上的棋子。指南车是有方向的车。横向指南车可以攻击与之处在同一行上的棋子。纵向指南车可以攻击与之处在同一列上的棋子。指南车问题要求在m×n格的棋盘上放置指南车,并确定各指南车的攻击方向,使棋盘上不受指南车攻击的方格数最多。 编程任务:对于给定的m×n格的棋盘和2 个整数x 和y。整数x 表示棋盘上有x个规定方格应放置指南车,但攻击方向未定。整数y表示除了已规定放置位置的x个指南车外,还要在棋盘上放置y个指南车,其位置和攻击方向均未定。设计一个分支限界法,计算x+y 个指南车的放置方案,使棋盘上不受指南车攻击的方格数最多。 -Problem Description: In accordance with the rules of chess, can attack with vehicles in the same column or row on the chess pieces. Guide to car yes car have direction. Horizontal guide in the car can attack with a pawn on the same line. Vertical guide in the car can attack with a pawn on the same row. Guide to car questions asked in the m × n grid of the chessboard placed guide vehicles, and to determine the guidelines for car attack direction, so that from the chessboard grid attacks cart number. Programming tasks: For a given m × n grid of the chessboard and the two integers x and y. Integer x has x express chessboard grid of the provisions of the Guide should be placed car, but the direction of undetermined attack. Integer y that has been apart from the provisions of the x position to place a guide to the vehicle, but also in the board to place a guide y car, its location and direction of the attack were not set. Design of a branch and bound method to calculate x+ y个guide plac
Platform: | Size: 1024 | Author: yxd | Hits:

[Othermataqipan

Description: 利用回溯法+贪心算法实现马踏棋盘功能。具体描述:有一M*N的棋盘,输入马所在的起始位置,输出马走过的全部路径。(马走过棋盘上每一个点)-The use of backtracking greedy algorithm+ horse riding board features. Specific description: There is an M* N chessboard, enter the horse where the starting position, the output of all the horses walked the path. (Ma walked the board for every one point)
Platform: | Size: 454656 | Author: chenjian | Hits:

[SCMtanshishe

Description: 这是一个讲解用c语言编写的文章。使用棋盘法的贪吃蛇c代码-This is a talk with the c language articles. Snake using the chessboard method c code
Platform: | Size: 31744 | Author: suyoujiang | Hits:

[Data structsnqueens

Description: 分支限界法解决皇后问题,在nxn棋盘上放n个棋子,要求两个棋子不能放在同一列上-Queens branch and bound method to solve the problem, put the board in the nxn n-piece, two pieces can not be requested on the same column
Platform: | Size: 1183744 | Author: 王浩文 | Hits:

[OpenCVcamera-calibration

Description: 利用opencv实现相机的标定,算法采用张正友的棋盘法。结构很清晰易懂。-Opencv camera calibration algorithm using the checkerboard method of Zhang Zhengyou. The structure is clear and easy to understand.
Platform: | Size: 1027072 | Author: LBR | Hits:
« 12 3 4 5 6 7 8 »

CodeBus www.codebus.net