20180310

#include "stdafx.h"

#define MAX_MAP_SIZE 64
#define ZONE_SIZE 4

char zone[ZONE_SIZE][ZONE_SIZE];
int hisindex=0;
char hiszone[100000][ZONE_SIZE][ZONE_SIZE];

extern void randomscan(char zone[ZONE_SIZE][ZONE_SIZE]);


int table_produce(
int calc_and_upmap(int N,char guess[64][64],char zone[4][4])
{
   int i,j,x,y;
   int inner=0;
   int stx,sty;
   int meetnum;
   int upnum=0;//初始化为0
   for(stx=0;stx<=N-4;stx+=2){
      for(sty=0;sty<=N-4;sty+=2){
         meetnum=0;
         for(i=0;i<ZONE_SIZE;i++){
            for(j=0;j<ZONE_SIZE;j++){
               if(guess[stx+i][sty+j]==zone[i][j])
                  meetnum++;
            }
         }
         if(meetnum>=8){
            for(i=0;i<4;i++)
               for(j=0;j<4;j++)
                  guess[stx+i][sty+j]=zone[i][j];
            inner=1;
            upnum=16-meetnum;
            break;//找到一个匹配就要中断
         }
      }
      if(inner==1){
         break;
      }
   }
   return upnum;
}

//保存random,这里可以优化为不重复的zone
void save_zone_info(char zone[4][4])
{
   int i,j;
   for(i=0;i<4;i++){
      for(j=0;j<4;j++){
         hiszone[hisindex][i][j]=zone[i][j];
      }
   }
   hisindex+=1;
}

int update_by_hiszone(int N, char guess[64][64])
{
   int i,j;
   int index;
   int upnum=0;
   char hzone[4][4];
   for(index=0;index<hisindex;index++){
      for(i=0;i<ZONE_SIZE;i++){
         for(j=0;j<ZONE_SIZE;j++){
            hzone[i][j]=hiszone[index][i][j];
         }
      }
      upnum=calc_and_upmap(N,guess,hzone);
      if(upnum>0)//首先要记得这里如果upnum大于0,说明已经找到匹配,这里开始写成8,导致死循环。
         break; //因为题目保证了只能匹配一块区域,所以这里要中断
   }
   return upnum;
}

void reconstruct(int N, char guess[MAX_MAP_SIZE][MAX_MAP_SIZE])//map=guess_map
{
   int upnum;
   int leftnum=(N-4)*(N-4);
   char zone[4][4];
   int hisupnum;
   while(leftnum != 0){
      randomscan(zone);
      upnum=calc_and_upmap(N,guess,zone);
      if(upnum!=0)//有更新
      {
         leftnum-=upnum;//更新剩余的方格
         hisupnum=0;
         do{
            hisupnum=update_by_hiszone(N,guess);//用历史zone更新
            leftnum-=hisupnum;
         }while(hisupnum>0);
      }
      save_zone_info(zone);
   }
   hisindex=0;//每个case结束后要复位
}

猜你喜欢

转载自dorfli.iteye.com/blog/2414455