游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2400|回复: 1

无题

[复制链接]

17

主题

1629

帖子

5982

积分

论坛元老

Rank: 8Rank: 8

积分
5982
QQ
发表于 2015-7-9 10:15:51 | 显示全部楼层 |阅读模式
  1. 八数码问题是指一个3X3的9格棋盘上放置1到8这8个数字,多余一个空格,空格周围的数字可以移动到空格中
  2. 例如输入0,2,3,1,8,4,7,6,5这九个数(0表示空格位置),按输入顺序排列为setp 0,通过若干步移动就可以到达最终状态setp 2:
  3. setp 0
  4. 0 2 3
  5. 1 8 4
  6. 7 6 5
  7. setp 1
  8. 1 2 3
  9. 0 8 4
  10. 7 6 5
  11. setp 2
  12. 1 2 3
  13. 8 0 4
  14. 7 6 5

  15. 1 Create search tree Tr consisting only of the start node (root) n0. Put n0 on an ordered list called OPEN.
  16. 2 Create empty list called CLOSED.
  17. 3 If OPEN is empty, exit with failure.
  18. 4 Move the first node on OPEN, called n, to CLOSED.
  19. 5 If n is a goal node, exit with success; the solution is the path from n to n0 along the edges in Tr.
  20. 6 Expand node n by generating a set M of successors and connect them to n. Also put the members of M on OPEN.
  21. 7 Reorder the list OPEN according to a specific rule.
  22. 8 Go to step 3.

  23. // f(x) = 节点深度
  24. // g(x) = 节点中不在最终位置上的数字个数

  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #define NUM 5
  29. typedef struct bgMatrix
  30. {
  31.     int v, w;
  32.     char matrix[NUM][NUM];
  33.     int pre;
  34.     int f;
  35.     int g;
  36.     bool isVisited;
  37. }Matrix;
  38. typedef struct bgQueue
  39. {
  40.     Matrix* data;
  41.     int length;
  42.     int maxLength;
  43.     int head;
  44.     int tail;
  45. }BGQueue;
  46. typedef BGQueue* Queue;
  47. typedef struct bgStack
  48. {
  49.     Matrix* data;
  50.     int top;
  51. }BGStack;
  52. typedef BGStack* Stack;
  53. char srcMatrix[NUM][NUM] =
  54. {
  55.     {'*', '*', '*', '*', '*'},
  56.     {'*', '2', '8', '3', '*'},
  57.     {'*', '1', '6', '4', '*'},
  58.     {'*', '7', '0', '5', '*'},
  59.     {'*', '*', '*', '*', '*'}
  60. };
  61. char dstMatrix[NUM][NUM] =
  62. {
  63.     {'*', '*', '*', '*', '*'},
  64.     {'*', '1', '2', '3', '*'},
  65.     {'*', '8', '0', '4', '*'},
  66.     {'*', '7', '6', '5', '*'},
  67.     {'*', '*', '*', '*', '*'}
  68. };
  69. int dx[4] = {0, 0, -1, 1};
  70. int dy[4] = {1, -1, 0, 0};
  71. int cnt = -1;
  72. Queue queue;
  73. Stack stack;
  74. bool astar(Matrix matrix);
  75. int getG(Matrix matrix);
  76. void initQueue(int length);
  77. void putQueue(Matrix matrix);
  78. Matrix getQueue(void);
  79. int isQueueEmpty(void);
  80. int isQueueFull(void);
  81. void initStack(int length);
  82. void pushStack(Matrix matrix);
  83. Matrix popStack(void);
  84. int isStackEmpty(void);
  85. int matrixCmp(char src[][NUM], char dst[][NUM]);
  86. void matrixCpy(char dst[][NUM], char src[][NUM]);
  87. void matrixPrint(char matrix[][NUM]);
  88. int main(int argc, char *argv[]) {
  89.    
  90.     Matrix src;
  91.    
  92.     initQueue(3628800+1);
  93.     initStack(3628800+1);
  94.    
  95.     src.v = 3;
  96.     src.w = 2;
  97.     matrixCpy(src.matrix, srcMatrix);
  98.     src.pre = cnt;
  99.     src.f = 0;
  100.     src.g = getG(src);
  101.     astar(src);
  102.     return 0;
  103. }
  104. bool astar(Matrix matrix)
  105. {
  106.     Matrix t, s;
  107.    
  108.     int v, w;
  109.    
  110.     int direction;
  111.    
  112.     putQueue(matrix);
  113.    
  114.     while (!isQueueEmpty())
  115.     {
  116.         s = getQueue();
  117.         
  118.         cnt++;
  119.         
  120.         for (direction = 0; direction < 4; direction++)
  121.         {
  122.             t = s;
  123.             v = t.v + dx[direction]; w = t.w + dy[direction];
  124.             
  125.             if (srcMatrix[v][w] != '*')
  126.             {
  127.                 char c = t.matrix[t.v][t.w];
  128.                 t.matrix[t.v][t.w] = t.matrix[v][w];
  129.                 t.matrix[v][w] = c;
  130.                
  131.                 t.v = v;
  132.                 t.w = w;
  133.                 t.pre = cnt;
  134.                 t.f = s.f + 1;
  135.                 t.g = getG(t);
  136.                 t.isVisited = false;
  137.                
  138.                 int h = 0;
  139.                 for (; h < queue->tail; h++)
  140.                 {
  141.                     if (matrixCmp(queue->data[h].matrix, t.matrix))
  142.                     {
  143.                         break;
  144.                     }
  145.                 }
  146.                
  147.                 if (h == queue->tail)
  148.                 {
  149.                     putQueue(t);
  150.                     
  151.                     if (matrixCmp(t.matrix, dstMatrix))
  152.                     {
  153.                         pushStack(t);
  154.                         
  155.                         for (int father = t.pre; !matrixCmp(queue->data[father].matrix, srcMatrix); father = queue->data[father].pre)
  156.                         {
  157.                             pushStack(queue->data[father]);
  158.                         }
  159.                         
  160.                         puts("PathFind = ");
  161.                         
  162.                         matrixCpy(t.matrix, srcMatrix);
  163.                         pushStack(t);
  164.                         
  165.                         while (!isStackEmpty())
  166.                         {
  167.                             s = popStack();
  168.                             matrixPrint(s.matrix);
  169.                         }
  170.                         
  171.                         return true;
  172.                     }
  173.                 }
  174.             }
  175.         }
  176.     }
  177.    
  178.     return false;
  179. }
  180. int getG(Matrix matrix)
  181. {
  182.     int g = 0;
  183.    
  184.     for (int i = 0; i < NUM; i++)
  185.     {
  186.         for (int j = 0; j < NUM; j++)
  187.         {
  188.             if (matrix.matrix[i][j] != '*' && matrix.matrix[i][j] != dstMatrix[i][j])
  189.             {
  190.                 g++;
  191.             }
  192.         }
  193.     }
  194.    
  195.     return g;
  196. }
  197. void initQueue(int length)
  198. {
  199.     queue = malloc(sizeof(BGQueue));
  200.    
  201.     queue->data = malloc(sizeof(Matrix) * length);
  202.     queue->length = 0;
  203.     queue->maxLength = length;
  204.     queue->head = 0;
  205.     queue->tail = 0;
  206. }
  207. void putQueue(Matrix matrix)
  208. {
  209.     queue->data[queue->tail++] = matrix;
  210.     queue->tail = queue->tail % queue->maxLength;
  211.     queue->length++;
  212.    
  213.     Matrix* a = malloc(sizeof(Matrix) * queue->maxLength);
  214.     Matrix* b = malloc(sizeof(Matrix) * queue->maxLength);
  215.    
  216.     int an = 0;
  217.     int bn = 0;
  218.    
  219.     for (int i = 0; i < queue->length; i++)
  220.     {
  221.         if (queue->data[i].isVisited)
  222.         {
  223.             a[an++] = queue->data[i];
  224.         }
  225.         else
  226.         {
  227.             b[bn++] = queue->data[i];
  228.         }
  229.     }
  230.     for (int i = 0; i < bn-1; i++)
  231.     {
  232.         for (int j = bn-1; j > i; j--)
  233.         {
  234.             int h1 = b[j-1].f + b[j-1].g;
  235.             int h2 = b[j].f + b[j].g;
  236.             
  237.             if (h1 > h2)
  238.             {
  239.                 Matrix t = b[j-1];
  240.                 b[j-1] = b[j];
  241.                 b[j] = t;
  242.             }
  243.         }
  244.     }
  245.    
  246.     for (int i = 0; i < an; i++)
  247.     {
  248.         queue->data[i] = a[i];
  249.     }
  250.    
  251.     for (int i = 0; i < bn; i++)
  252.     {
  253.         queue->data[an+i] = b[i];
  254.     }
  255.     free(a);
  256.     free(b);
  257. }
  258. Matrix getQueue(void)
  259. {
  260.     queue->data[queue->head].isVisited = true;
  261.    
  262.     Matrix ret;
  263.     ret = queue->data[queue->head++];
  264.     queue->head = queue->head % queue->maxLength;
  265.    
  266.     return ret;
  267. }
  268. int isQueueEmpty(void)
  269. {
  270.     return queue->head == queue->tail;
  271. }
  272. int isQueueFull(void)
  273. {
  274.     return ((queue->tail+1) % queue->maxLength) == queue->head;
  275. }
  276. void initStack(int length)
  277. {
  278.     stack = malloc(sizeof(BGStack));
  279.    
  280.     stack->data = malloc(sizeof(Matrix) * length);
  281.     stack->top = 0;
  282.    
  283. }
  284. void pushStack(Matrix matrix)
  285. {
  286.     stack->data[stack->top++] = matrix;
  287. }
  288. Matrix popStack(void)
  289. {
  290.     Matrix ret;
  291.     ret = stack->data[--stack->top];
  292.    
  293.     return ret;
  294. }
  295. int isStackEmpty(void)
  296. {
  297.     return (stack->top == 0);
  298. }
  299. int matrixCmp(char src[][NUM], char dst[][NUM])
  300. {
  301.     int i, j, s, t, ret;
  302.    
  303.     char srcString[30] = {0};
  304.     char dstString[30] = {0};
  305.    
  306.     s = 0;
  307.     t = 0;
  308.    
  309.     for (i = 0; i < NUM; i++)
  310.     {
  311.         for (j = 0; j < NUM; j++)
  312.         {
  313.             srcString[s++] = src[i][j];
  314.             dstString[t++] = dst[i][j];
  315.         }
  316.     }
  317.    
  318.     ret = !strcmp(srcString, dstString);
  319.    
  320.     return ret;
  321. }

  322. void matrixCpy(char dst[][NUM], char src[][NUM])
  323. {
  324.     int i, j;
  325.    
  326.     for (i = 0; i < NUM; i++)
  327.     {
  328.         for (j = 0; j < NUM; j++)
  329.         {
  330.             dst[i][j] = src[i][j];
  331.         }
  332.     }
  333. }
  334. void matrixPrint(char matrix[][NUM])
  335. {
  336.     char s[60] = {0};
  337.    
  338.     int i, j, k;
  339.    
  340.     k = 0;
  341.    
  342.     for (i = 0; i < NUM; i++)
  343.     {
  344.         for (j = 0; j < NUM; j++)
  345.         {
  346.             s[k++] = matrix[i][j];
  347.         }
  348.         
  349.         s[k++] = '\r';
  350.         s[k++] = '\n';
  351.     }
  352.    
  353.     s[k++] = '\r';
  354.     s[k++] = '\n';
  355.    
  356.     puts(s);
  357.    
  358. }

  359. PathFind =
  360. *****
  361. *283*
  362. *164*
  363. *705*
  364. *****

  365. *****
  366. *283*
  367. *104*
  368. *765*
  369. *****

  370. *****
  371. *203*
  372. *184*
  373. *765*
  374. *****

  375. *****
  376. *023*
  377. *184*
  378. *765*
  379. *****

  380. *****
  381. *123*
  382. *084*
  383. *765*
  384. *****

  385. *****
  386. *123*
  387. *804*
  388. *765*
  389. *****
复制代码

17

主题

1629

帖子

5982

积分

论坛元老

Rank: 8Rank: 8

积分
5982
QQ
 楼主| 发表于 2015-7-9 10:23:16 | 显示全部楼层
给个首页推荐推荐吧
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-2-25 17:16

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表