首页 > 解决方案 > C 游戏和矩阵

问题描述

我有一个问题,我不知道如何解决。

我有这个矩阵(原始照片在https://i.stack.imgur.com/Z7Ssu.png):

{
  {   0,   1,   0,   2,   0,   3,   0,   0,   0,   0,   0,   0,   0,   0,   0},
  {   0,   4,   5,   6,   5,   4,   0,   0,   0,   0,   0,   0,   0,   0,   0},
  {   0,   0,   0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
  {   0,   0,   0,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,   0},
  {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  19,   0},
  {   0,   0,   0,   0,   0,   0,   0,  26,  25,  24,  23,  22,  21,  20,   0},
  {   0,   0,   0,   0,   0,   0,   0,  27,   0,   0,   0,   0,   0,   0,   0},
  {   0,   0,   0,   0,  41,  42,  43, 255,   0,   0,   0,   0,   0,   0,   0},
  {   0,   0,   0,   0,  40,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
  {   0,   0,   0,   0,  39,   0,   0,  24,  23,  22,   0,  16,  15,  14,   0},
  {   0,  35,  36,  37,  38,   0,   0,  25,   0,  21,   0,  17,   0,  13,   0},
  {   0,  34,   0,   0,   0,  28,  27,  26,   0,  20,  19,  18,   0,  12,   0},
  {   0,  33,  32,  31,  30,  29,   0,   0,   0,   0,   0,   0,   0,  11,   0},
  {   0,   0,   0,   0,   0,   0,   0,   4,   5,   6,   7,   8,   9,  10,   0},
  {   0,   0,   0,   0,   0,   0,   0, 254,   0,   0,   0,   0,   0,   0,   0},
}

士兵在矩阵中的数字 254 处生成,然后在此之后,他将跟随代表道路的数字。

士兵必须遵循的道路将是数字 4,然后是 5,然后是 6,以此类推……为此,士兵将前往他周围数字最高的点。

士兵用一个数字表示,我们可以说是 230。我想做一个功能,只移动一个数字。例如,如果士兵生成,那么他在数字 254 处,因此将不再有数字 254,因为数字 230 将具有意义。当我调用函数时,士兵,所以数字 230 将是 4。再一次,当我调用函数时,他将是 5。每次士兵移动时,他所在的点将是等于他移动到负1的数字。

这就是我所做的:

int point_around[4];
for( i=0 ; i < 15 ; i++){
    for( j=0 ; j < 15 ; j++){
        if( matrix[i][j] == 230){
                
                /* in point_around array we have the points around */
                get_point_around(matrix[i][j], point_around);

                /* looking for the max point, around the number 230 */
                /* sort the array to have the max point */
                size_tab=4;
                while( size_tab> 1 ){ 
                    for( k=0 ; k <  size_tab-1 ; k++ ){
                        if( point_around[k] > point_around[k+1] ){ 
                            tmp = point_around[k];
                            point_around[k] = point_around[k+1];
                            point_around[k+1] = tmp;
                        }
                    }
                    size_tab--;
                }

                max = point_around[3];
               
                if( max == matrix[i-1][j] ){
                    matrix[i-1][j] = 230;
                } 
                else if( max == matrix[i][j+1] ){
                    matrix[i][j+1] = 230; 
                }
                else if( max == matrix[i+1][j] ){
                    matrix[i+1][j] = 230;
                }  
                else {
                    matrix[i][j-1] = 230;
                }  
                                    
                matrix[i][j] = max - 1;
    }
}
                
              

我的问题是当他向右移动时,所以我调用一次函数让他移动一次,由于 j 循环,他会移动多次。我不知道我该如何解决这个问题。

标签: cmatrix

解决方案


看起来您只想在找到匹配项后完成双循环...各种选择:您可以通过将其放入函数中来关注其中一个评论(然后您可以在找到后“返回”比赛)。或者你可以通过休息来调整你所拥有的;并检查外循环:

 int point_around[4];
 int max=-1;    // while <0 we know we've not found a match.
 for( i=0 ; i < 15 && max<0 ; i++){   // the max<0 checks in case we found a match
     for( j=0 ; j < 15 ; j++){
     ...
         matrix[i][j] = max - 1;
         break;

休息; 完成 j 循环,但您仍将处于 i 循环中,因此需要对 i 循环进行额外检查以检测您是否应该完成循环。我使用 max 作为测试(因为一旦找到匹配项它就会变为正数),但您可以使用单独的变量来代替。


推荐阅读