鱼C论坛

 找回密码
 立即注册
查看: 1187|回复: 10

[已解决]矩阵游戏循环的条件该怎样改?

[复制链接]
发表于 2018-10-9 16:27:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在二微矩阵中   从0开始每次循环数字都+1  遇到x不能进入循环
如图  .......                                  .......                             .......                              .......
        ...xxx.                                ...xxx.                            ...xxx.                           ...xxx.
        .xx...x                                .xx...x                            .xx2..x                          .xx234x
        x....x.           第一次循环:  x..1.x.        第二次循环: x.212x.        最终循环: x3212x6
       ..x0.x.                                ..x01x.                            ..x01x.                          54x01x5
       .......                                  ....1..                              ..212..                          4321234
       xx.xxxx                              xx.xxxx                           xx.xxxx                         xx3xxxx
      







#include <stdio.h>
#include<string.h>
#define M 7

char map[M][M];

void inicarte(char arr[][M])
{
   int i,j;
   for(i=0;i<M;i++)
    for(j=0;j<M;j++)
     arr[i][j]='.';
}

int shitou(int x, int y)
{
    if(x < M || y < M)
        map[x][y]='x';
        return 1;
    if(x>M||y>M)
        return 0;
}

int lig(int x)
{
  
    return x;
}
int col(int x)
{
   
    return x;
   
}
int duqu(int x,int y)
{
    if((x>M || y>M)||map[x][y]=='.'||map[x][y]=='x')
     return -1;
    else
        return 0;
}
int xieru(int x,int y,char date)
{
    if(x>M || y>M )
        return -1;
    if(x < M && y < M && map[x][y]!='x')
        map[x][y]=date;
   
        return 0;
}
void print(char arr[][M])
{
    int i,j;
    for(i = 0; i < M; i++){
        for(j = 0; j < M; j++){
              printf("%c", arr[i][j]);}
        printf("\n");}
    printf("\n");
}


int main()
{
   
    inicarte(map);
    shitou(1,3);
    shitou(1,4);
    shitou(1,5);
    shitou(2,1);
    shitou(2,2);
    shitou(2,6);
    shitou(3,0);
    shitou(3,5);
    shitou(4,2);
    shitou(4,5);
    shitou(6,0);
    shitou(6,1);
    shitou(6,3);
    shitou(6,4);
    shitou(6,5);
   shitou(6,6);
    int x=lig(4);
    int y=col(3);
    xieru(x,y,48);
    int xunhuan =4;
    int i;
    for (i=0;i<xuhuan;i++){      
        if (duqu(x,y) !=-1)
            xieru(x-i,y,48+i);
            xieru(x+i,y,48+i);
            xieru(x,y-i,48+i);
           xieru(x,y+i,48+i);

        print(map);}
       return 0;
}
最佳答案
2018-10-9 23:47:08
………………你这样写代码,会让人不想看
应该是你要的东西,用递归去实现比较适合,目前来说先打好基础~
  1. #include <stdio.h>

  2. #define M 7

  3. int map[M][M];

  4. void initMap(int arr[][M],int x, int y)
  5. {
  6.         for(int i = 0; i < M; i++)
  7.                 for(int j = 0; j < M; j++)
  8.                         arr[i][j] = '.';
  9.         arr[y][x] = 0;
  10. }

  11. void placeRock(int x, int y)
  12. {
  13.     if(x < M && y < M)
  14.         map[y][x] = 'x';
  15. }

  16. void fillingMap(int arr[][M],int x, int y)
  17. {
  18.         int move_x[4] = {-1, 0, 1, 0};
  19.         int move_y[4] = {0, -1, 0, 1};
  20.         int x1,y1;
  21.        
  22.         for(int i = 0; i < 4; i++)
  23.         {
  24.                 x1 = x + move_x[i];
  25.                 y1 = y + move_y[i];
  26.        
  27.                 if(x1 >= 0 && x1 < M && y1 >= 0 && y1 < M)
  28.                 {
  29.                         if(arr[y1][x1] == 'x')
  30.                                 continue;
  31.                         if(arr[y1][x1] < arr[y][x])
  32.                                 continue;
  33.                         arr[y1][x1] = arr[y][x] + 1;
  34.                         fillingMap(arr,x1,y1);
  35.                 }
  36.        
  37.         }
  38. }


  39. void fillMap(int arr[][M])
  40. {
  41.         int x, y, flag = 0;
  42.         for(y = 0; y < M; y++)
  43.         {
  44.                 for(x = 0; x < M; x++)
  45.                 {
  46.                         if(arr[y][x] == 0)
  47.                         {
  48.                                 flag = 1;
  49.                                 break;
  50.                         }
  51.                 }
  52.                 if(flag) break;
  53.         }
  54.        
  55.         fillingMap(map,x,y);
  56. }

  57. void print(int arr[][M])
  58. {
  59.         for(int i = 0; i < M; i++)
  60.         {
  61.                 for(int j = 0; j < M; j++)
  62.                 {
  63.                         if(arr[i][j] == 'x' || arr[i][j] == '.')
  64.                                 printf("%3c ", arr[i][j]);
  65.                         else
  66.                                 printf("%3d ", arr[i][j]);
  67.                 }
  68.                 printf("\n");
  69.         }
  70.         printf("\n");
  71. }

  72. int main()
  73. {
  74.    
  75.         initMap(map,3,4);
  76.         placeRock(0,3); placeRock(0,6);        placeRock(1,6);       
  77.         placeRock(2,1);        placeRock(2,2);        placeRock(2,4);       
  78.         placeRock(3,1);        placeRock(3,6);        placeRock(4,1);
  79.         placeRock(4,6);        placeRock(5,1);        placeRock(5,3);
  80.         placeRock(5,4);        placeRock(5,6);        placeRock(6,2);
  81.         placeRock(6,6);
  82.         print(map);
  83.         fillMap(map);
  84.         print(map);
  85.        
  86.         return 0;
  87. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-9 23:47:08 | 显示全部楼层    本楼为最佳答案   
………………你这样写代码,会让人不想看
应该是你要的东西,用递归去实现比较适合,目前来说先打好基础~
  1. #include <stdio.h>

  2. #define M 7

  3. int map[M][M];

  4. void initMap(int arr[][M],int x, int y)
  5. {
  6.         for(int i = 0; i < M; i++)
  7.                 for(int j = 0; j < M; j++)
  8.                         arr[i][j] = '.';
  9.         arr[y][x] = 0;
  10. }

  11. void placeRock(int x, int y)
  12. {
  13.     if(x < M && y < M)
  14.         map[y][x] = 'x';
  15. }

  16. void fillingMap(int arr[][M],int x, int y)
  17. {
  18.         int move_x[4] = {-1, 0, 1, 0};
  19.         int move_y[4] = {0, -1, 0, 1};
  20.         int x1,y1;
  21.        
  22.         for(int i = 0; i < 4; i++)
  23.         {
  24.                 x1 = x + move_x[i];
  25.                 y1 = y + move_y[i];
  26.        
  27.                 if(x1 >= 0 && x1 < M && y1 >= 0 && y1 < M)
  28.                 {
  29.                         if(arr[y1][x1] == 'x')
  30.                                 continue;
  31.                         if(arr[y1][x1] < arr[y][x])
  32.                                 continue;
  33.                         arr[y1][x1] = arr[y][x] + 1;
  34.                         fillingMap(arr,x1,y1);
  35.                 }
  36.        
  37.         }
  38. }


  39. void fillMap(int arr[][M])
  40. {
  41.         int x, y, flag = 0;
  42.         for(y = 0; y < M; y++)
  43.         {
  44.                 for(x = 0; x < M; x++)
  45.                 {
  46.                         if(arr[y][x] == 0)
  47.                         {
  48.                                 flag = 1;
  49.                                 break;
  50.                         }
  51.                 }
  52.                 if(flag) break;
  53.         }
  54.        
  55.         fillingMap(map,x,y);
  56. }

  57. void print(int arr[][M])
  58. {
  59.         for(int i = 0; i < M; i++)
  60.         {
  61.                 for(int j = 0; j < M; j++)
  62.                 {
  63.                         if(arr[i][j] == 'x' || arr[i][j] == '.')
  64.                                 printf("%3c ", arr[i][j]);
  65.                         else
  66.                                 printf("%3d ", arr[i][j]);
  67.                 }
  68.                 printf("\n");
  69.         }
  70.         printf("\n");
  71. }

  72. int main()
  73. {
  74.    
  75.         initMap(map,3,4);
  76.         placeRock(0,3); placeRock(0,6);        placeRock(1,6);       
  77.         placeRock(2,1);        placeRock(2,2);        placeRock(2,4);       
  78.         placeRock(3,1);        placeRock(3,6);        placeRock(4,1);
  79.         placeRock(4,6);        placeRock(5,1);        placeRock(5,3);
  80.         placeRock(5,4);        placeRock(5,6);        placeRock(6,2);
  81.         placeRock(6,6);
  82.         print(map);
  83.         fillMap(map);
  84.         print(map);
  85.        
  86.         return 0;
  87. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-10 23:18:15 | 显示全部楼层
claws0n 发表于 2018-10-9 16:47
………………你这样写代码,会让人不想看
应该是你要的东西,用递归去实现比较适合,目前来说先打好基础~

感谢大佬 那我如果想要每次递归的时候都显示map那要怎么改? 像我提问那样子的图例 是把print(map)放到fillingmap函数里面吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-11 00:23:07 | 显示全部楼层
qpwoeiruyt 发表于 2018-10-10 23:18
感谢大佬 那我如果想要每次递归的时候都显示map那要怎么改? 像我提问那样子的图例 是把print(map)放到 ...

嗯,放到里面去。上面的代码有稍微修改以下,因为是混合型的地图,不然会打印太多次。
  1. #include <stdio.h>

  2. #define M 7

  3. int map[M][M];
  4. void print(int arr[][M]);

  5. void initMap(int arr[][M],int x, int y)
  6. {
  7.     for(int i = 0; i < M; i++)
  8.         for(int j = 0; j < M; j++)
  9.             arr[i][j] = '.';
  10.     arr[y][x] = 0;
  11. }

  12. void placeRock(int x, int y)
  13. {
  14.     if(x < M && y < M)
  15.         map[y][x] = 'x';
  16. }

  17. void fillingMap(int arr[][M],int x, int y)
  18. {
  19.     int move_x[4] = {-1, 0, 1, 0};
  20.     int move_y[4] = {0, -1, 0, 1};
  21.     int x1,y1;
  22.    
  23.     for(int i = 0; i < 4; i++)
  24.     {
  25.         x1 = x + move_x[i];
  26.         y1 = y + move_y[i];

  27.         if(x1 >= 0 && x1 < M && y1 >= 0 && y1 < M)
  28.         {
  29.             if(arr[y1][x1] == '.')
  30.             {
  31.                     arr[y1][x1] = arr[y][x] + 1;
  32.                     print(map);
  33.             }
  34.             else
  35.                 continue;
  36.             fillingMap(arr,x1,y1);
  37.         }
  38.     }
  39. }


  40. void fillMap(int arr[][M])
  41. {
  42.     int x, y, flag = 0;
  43.     for(y = 0; y < M; y++)
  44.     {
  45.         for(x = 0; x < M; x++)
  46.         {
  47.             if(arr[y][x] == 0)
  48.             {
  49.                 flag = 1;
  50.                 break;
  51.             }
  52.         }
  53.         if(flag) break;
  54.     }
  55.    
  56.     fillingMap(map,x,y);
  57. }

  58. void print(int arr[][M])
  59. {
  60.     for(int i = 0; i < M; i++)
  61.     {
  62.         for(int j = 0; j < M; j++)
  63.         {
  64.             if(arr[i][j] == 'x' || arr[i][j] == '.')
  65.                 printf("%3c ", arr[i][j]);
  66.             else
  67.                 printf("%3d ", arr[i][j]);
  68.         }
  69.         printf("\n");
  70.     }
  71.     printf("\n");
  72. }

  73. int main()
  74. {
  75.     initMap(map,3,4);
  76.     placeRock(0,3); placeRock(0,6); placeRock(1,6);        
  77.     placeRock(2,1); placeRock(2,2); placeRock(2,4);        
  78.     placeRock(3,1); placeRock(3,6); placeRock(4,1);
  79.     placeRock(4,6); placeRock(5,1); placeRock(5,3);
  80.     placeRock(5,4); placeRock(5,6); placeRock(6,2);
  81.     placeRock(6,6);
  82.     print(map);
  83.     fillMap(map);

  84.     return 0;
  85. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-11 02:04:51 | 显示全部楼层
claws0n 发表于 2018-10-10 17:23
嗯,放到里面去。上面的代码有稍微修改以下,因为是混合型的地图,不然会打印太多次。

等等 大佬你这个代码有点问题  第一次循环的时候 零的附近应该同时出现四个一 当左边那个因为被石头挡住了所以没有了  然后再从这三个一的上下左右出现二
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-11 13:28:24 | 显示全部楼层
claws0n 发表于 2018-10-10 17:23
嗯,放到里面去。上面的代码有稍微修改以下,因为是混合型的地图,不然会打印太多次。

等等 大佬你这个代码有点问题  第一次循环的时候 零的附近应该同时出现四个一 当左边那个因为被石头挡住了所以没有了  然后再从这三个一的上下左右出现二
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-11 13:31:12 | 显示全部楼层
qpwoeiruyt 发表于 2018-10-11 13:28
等等 大佬你这个代码有点问题  第一次循环的时候 零的附近应该同时出现四个一 当左边那个因为被石头挡住 ...

正在看,好像有些东西不能少,但是打印的次数多了,重复打印一样的东西
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-11 13:51:10 | 显示全部楼层
qpwoeiruyt 发表于 2018-10-11 13:28
等等 大佬你这个代码有点问题  第一次循环的时候 零的附近应该同时出现四个一 当左边那个因为被石头挡住 ...

输出无法看完全部,能够显示的行数有限
  1. #include <stdio.h>

  2. #define M 7

  3. int map[M][M];
  4. void print(int arr[][M]);

  5. void initMap(int arr[][M],int x, int y)
  6. {
  7.     for(int i = 0; i < M; i++)
  8.         for(int j = 0; j < M; j++)
  9.             arr[i][j] = '.';
  10.     arr[y][x] = 0;
  11. }

  12. void placeRock(int x, int y)
  13. {
  14.     if(x < M && y < M)
  15.         map[y][x] = 'x';
  16. }

  17. void fillingMap(int arr[][M],int x, int y)
  18. {
  19.     int move_x[4] = {-1, 0, 1, 0};
  20.     int move_y[4] = {0, -1, 0, 1};
  21.     int x1,y1;
  22.    
  23.     for(int i = 0; i < 4; i++)
  24.     {
  25.         x1 = x + move_x[i];
  26.         y1 = y + move_y[i];

  27.         if(x1 >= 0 && x1 < M && y1 >= 0 && y1 < M)
  28.         {
  29.             if(arr[y1][x1] == 'x')
  30.                 continue;
  31.             if(arr[y1][x1] < arr[y][x])
  32.                 continue;
  33.             if(arr[y1][x1] == arr[y][x] + 1)
  34.                 continue;

  35.             arr[y1][x1] = arr[y][x] + 1;
  36.             print(arr);         
  37.             fillingMap(arr,x1,y1);
  38.         }
  39.     }
  40. }


  41. void fillMap(int arr[][M])
  42. {
  43.     int x, y, flag = 0;
  44.     for(y = 0; y < M; y++)
  45.     {
  46.         for(x = 0; x < M; x++)
  47.         {
  48.             if(arr[y][x] == 0)
  49.             {
  50.                 flag = 1;
  51.                 break;
  52.             }
  53.         }
  54.         if(flag) break;
  55.     }
  56.    
  57.     fillingMap(map,x,y);
  58. }

  59. void print(int arr[][M])
  60. {
  61.     for(int i = 0; i < M; i++)
  62.     {
  63.         for(int j = 0; j < M; j++)
  64.         {
  65.             if(arr[i][j] == 'x' || arr[i][j] == '.')
  66.                 printf("%3c ", arr[i][j]);
  67.             else
  68.                 printf("%3d ", arr[i][j]);
  69.         }
  70.         printf("\n");
  71.     }
  72.     printf("\n");
  73. }

  74. int main()
  75. {
  76.     initMap(map,3,4);
  77.     placeRock(0,3); placeRock(0,6); placeRock(1,6);        
  78.     placeRock(2,1); placeRock(2,2); placeRock(2,4);        
  79.     placeRock(3,1); placeRock(3,6); placeRock(4,1);
  80.     placeRock(4,6); placeRock(5,1); placeRock(5,3);
  81.     placeRock(5,4); placeRock(5,6); placeRock(6,2);
  82.     placeRock(6,6);
  83.     print(map);
  84.     fillMap(map);

  85.     return 0;
  86. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-11 17:41:30 | 显示全部楼层
claws0n 发表于 2018-10-11 06:51
输出无法看完全部,能够显示的行数有限

大佬 你现在的代码是 0出现一个1然后后23456这样子进行的  但是你第一次的递归代码是0然后同时出现三个1 然后三个一的附近又同时出现2....如此类推。 我想要的是你第一次的那种
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-11 17:54:54 | 显示全部楼层
qpwoeiruyt 发表于 2018-10-11 17:41
大佬 你现在的代码是 0出现一个1然后后23456这样子进行的  但是你第一次的递归代码是0然后同时出现三个1  ...

递归不是那样实现的,所以无法实现你要的东西。你现在看到的是电脑真正执行递归的步骤。你那种是要迭代,比较麻烦一点吧?没写过
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-11 17:56:42 | 显示全部楼层
claws0n 发表于 2018-10-11 10:54
递归不是那样实现的,所以无法实现你要的东西。你现在看到的是电脑真正执行递归的步骤。你那种是要迭代, ...

原来如此  还是感谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 02:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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