0

我有一个看起来像这样的网格: 在此处输入图像描述

我的程序所做的是,它将计算是否获得徽章,然后打印出是或否,如果徽章是获得或不获得特定输入(输入将是一个带有坐标的框(x, y))。但由于某种原因,当我打印输出时,我的程序只计算黄色阴影区域。

在我的代码中,我通过说:

if ((x >= 2 && x <= 19) && (y >= 2 && y <= 19))

但这似乎不能解决我的问题。注意:如果输入 (x, y) 不在区间 [1, 20] 内,则返回 -1。

这是我打印结果时得到的结果: 在此处输入图像描述

如您所见,不在边缘的字段中银色徽章的输出不显示。我似乎无法弄清楚为什么不在边缘的盒子没有被计算/打印出来。

注意:当一个领域获得两个或更多青铜徽章时,Poly 徽章会返回“是”。

注意:如果出现以下情况,任何颜色标志都会返回标志:

  1. 一个场地获得 0 个铜牌,如果所有相邻场地都没有获得铜牌(相邻场地包括西北、东北、西南和东南)

  2. 一个领域获得 1 个铜牌,并且它的整体领域得分(声明为 fieldScore)为 20 或以上。

  3. 一块田地获得 2 个青铜徽章,并且至少有以下一项是正确的:该田地没有盈利(声明为 fieldProfit),收获时间少于 80 天(声明为收获时间),或者它的阳光照射(声明为sunExposure)大于其灌溉曝光(声明为灌溉曝光)。

  4. 一个领域获得 3 个或更多青铜徽章,如果至少有两个直接邻居获得两个或更多青铜徽章。(直接邻居是北、南、东和西)。

这是我的两个银色徽章功能(多边形徽章和任何颜色徽章)的代码:

int countBadges(int x, int y) {
   int mavebadge, incirclebadge, herobadge, weatherbadge, inquattrobadge,
      total;

   mavebadge = badgeBoundaryMaven(x, y);
   incirclebadge = badgeInnerCircle(x, y);
   herobadge = badgeLocalHero(x, y);
   weatherbadge = badgeBoringWeather(x, y);
   inquattrobadge = badgeInQuattro(x, y);

   total = mavebadge + incirclebadge + herobadge + weatherbadge +
      inquattrobadge;

   return total;

   printf("Total number: %d\n", countBadges(2, 2));
}

int badgePoly(int x, int y) {
   int bronzebadges, polybadge;

   if ((x >= 1 && x <= 20) && (y >= 1 && y <= 20)) {
      bronzebadges = countBadges(x, y);
      if (bronzebadges >= 2) {
         polybadge = 1;
      }
      else {
         polybadge = 0;
      }
   }
   else {
      polybadge = -1;
   }

   return polybadge;
}


int badgeAnyColor(int x, int y) {
   int bronzebadges, northbadges, northeastbadges, northwestbadges, eastbadges,
      westbadges, southbadges, southeastbadges, southwestbadges, colorbadge;

   double fs, ht, fp, sunexp, irrexp;

   if ((x >= 1 && x <= 20) && (y >= 1 && y <= 20)) {
      fs = fieldScore(x, y);
      ht = harvestTime(x, y);
      fp = fieldProfit(x, y);
      sunexp = sunExposure(x, y);
      irrexp = irrigationExposure(x, y);
      bronzebadges = countBadges(x, y);
      northbadges = countBadges(x, y + 1);
      northeastbadges = countBadges(x + 1, y + 1);
      northwestbadges = countBadges(x - 1, y + 1);
      eastbadges = countBadges(x + 1, y);
      westbadges = countBadges(x - 1, y);
      southbadges = countBadges(x - 1, y - 1);
      southeastbadges = countBadges(x + 1, y - 1);
      southwestbadges = countBadges(x - 1, y - 1);

      switch (bronzebadges) {
         case 0: {
            if (x == 1 && y == 1){
               if (northbadges == 0 && northeastbadges == 0 &&
                  eastbadges == 0){
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if (x == 20 && y == 1) {
               if (northbadges == 0 && northwestbadges == 0 &&
                  westbadges == 0) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
           }
            if (x == 1 && y == 20) {
               if (eastbadges == 0 && southeastbadges == 0 &&
                  southbadges == 0) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
           }
            if (x == 20 && y == 20) {
               if (westbadges == 0 && southwestbadges == 0 &&
                  southbadges == 0) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if ((x >= 2 && x <= 19) && (y == 1)) {
               if (westbadges == 0 && northwestbadges == 0 && northbadges == 0
                  && northeastbadges == 0 && eastbadges == 0) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if ((x >= 2 && x <= 19) && (y == 20)) {
               if (westbadges == 0 && southwestbadges == 0 && southbadges == 0
                  && southeastbadges == 0 && eastbadges == 0) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if ((x == 20) && (y >= 2 && y <= 19)) {
               if (northbadges == 0 && northwestbadges == 0 &&
                  westbadges == 0) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if ((x == 1) && (y >= 2 && y <= 19)) {
               if (northbadges == 0 && northeastbadges == 0 && eastbadges == 0
                  && southeastbadges == 0 && southbadges == 0) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if ((x >= 2 && x <= 19) && (y >= 2 && y <= 19)) {
               if (northbadges == 0 && northwestbadges == 0 && westbadges == 0
                  && southwestbadges == 0 && southbadges == 0 &&
                     southeastbadges == 0 && eastbadges == 0 &&
                        northeastbadges == 0) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            break;
         }
         case 1: {
            if (fs >= 20) {
               colorbadge = 1;
            }
            else {
               colorbadge = 0;
            }
            break;
         }
         case 2: {
            if (fp <= 0 || ht < 80 || sunexp > irrexp) {
               colorbadge = 1;
            }
            else {
               colorbadge = 0;
            }
            break;
         }
         case 3:
         case 4:
         case 5: {
            if ((x >= 2 && x <= 19) && y == 1) {
               if (((westbadges + northbadges) >= 2) || ((northbadges +
                  eastbadges) >= 2) || ((westbadges + eastbadges) >= 2)) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if (x == 1 && y == 1) {
               if ((northbadges + eastbadges) >= 2) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if (x == 20 && y == 1) {
               if ((northbadges + westbadges) >= 2) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if (x == 1 && y == 20) {
               if ((southbadges + eastbadges) >= 2) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if (x == 20 && y == 20) {
               if ((southbadges + westbadges) >= 2) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if ((x >= 2 && x <= 19) && y == 20) {
               if (((westbadges + southbadges) >= 2) || ((southbadges +
                  eastbadges) >= 2) || ((westbadges + eastbadges) >= 2)) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if ((x == 1) && (y >= 2 && y <= 19)) {
               if (((northbadges + eastbadges) >= 2) || ((eastbadges +
                  southbadges) >= 2) || ((northbadges + southbadges) >= 2)) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if ((x == 20) && (y >= 2 && y <= 19)) {
               if (((northbadges + westbadges) >= 2) || ((westbadges +
                  southbadges) >= 2) || ((northbadges + southbadges) >= 2)) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            if ((x >= 2 && x <= 19) && (y >= 2 && y <= 19)) {
               if (((northbadges + westbadges) >= 2) || ((westbadges +
                  southbadges) >= 2) || ((southbadges + eastbadges) >= 2) ||
                     ((northbadges + southbadges) >= 2) || ((westbadges +
                        eastbadges) >= 2)) {
                  colorbadge = 1;
               }
               else {
                  colorbadge = 0;
               }
            }
            break;
         }
      }
   }
   else {
      colorbadge = -1;
   }
   return colorbadge;
}
4

1 回答 1

1

您的 countBadges(x,y) 函数在 printf 之前有一个 return 语句,因此 printf 永远不会打印。你应该把它移到 return 上面,如果你不需要它,把它注释掉。您可能对 printf 有其他问题。

int countBadges(int x, int y)
{
    int mavebadge, incirclebadge, herobadge, weatherbadge, inquattrobadge,
        total;

    mavebadge = badgeBoundaryMaven(x, y);
    incirclebadge = badgeInnerCircle(x, y);
    herobadge = badgeLocalHero(x, y);
    weatherbadge = badgeBoringWeather(x, y);
    inquattrobadge = badgeInQuattro(x, y);

    total = mavebadge + incirclebadge + herobadge + weatherbadge +
        inquattrobadge;

    printf("Total number: %d\n", countBadges(2, 2));
    return total;
}

您经常检查您的网格位置,所以我建议使用一个函数(或宏)ongrid(x,y),并使用它来检查有效的网格边界位置,

int ongrid(x,y)
{
    return ( (x>=1) && (x<=20) && (y>=1) && (y<=20) );
}

您还应该检查您的边界条件,

  if( y + 1<=20 )
      northbadges = countBadges(x, y + 1);
  if( x + 1<=20 && y + 1<=20 )
     northeastbadges = countBadges(x + 1, y + 1);
  if( x - 1>=1 && y + 1<=20 )
     northwestbadges = countBadges(x - 1, y + 1);
  if( x + 1<=20 )
     eastbadges = countBadges(x + 1, y);
  if( x - 1<=20 )
     westbadges = countBadges(x - 1, y);
  if( y - 1>=1 )
     southbadges = countBadges(x, y - 1); //this value was probably not right
  if( x + 1<=20 && y - 1>=1 )
     southeastbadges = countBadges(x + 1, y - 1);
  if( x - 1>=1 && y - 1>=1 )
     southwestbadges = countBadges(x - 1, y - 1);

为了更清晰,用 ongrid() 函数重写,

  if( ongrid(x,y + 1) )
      northbadges = countBadges(x, y + 1);
  if( ongrid(x + 1,y + 1) )
     northeastbadges = countBadges(x + 1, y + 1);
  if( ongrid(x - 1,y + 1) )
     northwestbadges = countBadges(x - 1, y + 1);
  if( ongrid(x + 1,y) )
     eastbadges = countBadges(x + 1, y);
  if( ongrid(x - 1,y) )
     westbadges = countBadges(x - 1, y);
  if( ongrid(x,y - 1) )
     southbadges = countBadges(x, y - 1);
  if( ongrid(x + 1,y - 1) )
     southeastbadges = countBadges(x + 1, y - 1);
  if( ongrid(x - 1,y - 1) )
     southwestbadges = countBadges(x - 1, y - 1);

您的青铜徽章切换语句没有默认值;如果您的徽章数量与明确说明的不同,您需要处理默认值,

     default : {
         break;
     }
于 2013-10-24T22:09:06.470 回答