Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1559149
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2018-03-10 13:28:24




  1. #include "stdafx.h"

  2. #define MAX_MAP_SIZE 64
  3. #define ZONE_SIZE 4

  4. char zone[ZONE_SIZE][ZONE_SIZE];
  5. int hisindex=0;
  6. char hiszone[100000][ZONE_SIZE][ZONE_SIZE];

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

  8. int calc_and_upmap(int N,char guess[64][64],char zone[4][4])
  9. {
  10.    int i,j,x,y;
  11.    int inner=0;
  12.    int stx,sty;
  13.    int meetnum;
  14.    int upnum=0;//初始化为0
  15.    for(stx=0;stx<=N-4;stx+=2){
  16.       for(sty=0;sty<=N-4;sty+=2){
  17.          meetnum=0;
  18.          for(i=0;i<ZONE_SIZE;i++){
  19.             for(j=0;j<ZONE_SIZE;j++){
  20.                if(guess[stx+i][sty+j]==zone[i][j])
  21.                   meetnum++;
  22.             }
  23.          }
  24.          if(meetnum>=8){
  25.             for(i=0;i<4;i++)
  26.                for(j=0;j<4;j++)
  27.                   guess[stx+i][sty+j]=zone[i][j];
  28.             inner=1;
  29.             upnum=16-meetnum;
  30.             break;//找到一个匹配就要中断
  31.          }
  32.       }
  33.       if(inner==1){
  34.          break;
  35.       }
  36.    }
  37.    return upnum;
  38. }

  39. //保存random,这里可以优化为不重复的zone
  40. void save_zone_info(char zone[4][4])
  41. {
  42.    int i,j;
  43.    for(i=0;i<4;i++){
  44.       for(j=0;j<4;j++){
  45.          hiszone[hisindex][i][j]=zone[i][j];
  46.       }
  47.    }
  48.    hisindex+=1;
  49. }

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

  68. void reconstruct(int N, char guess[MAX_MAP_SIZE][MAX_MAP_SIZE])//map=guess_map
  69. {
  70.    int upnum;
  71.    int leftnum=(N-4)*(N-4);
  72.    char zone[4][4];
  73.    while(leftnum != 0){
  74.       randomscan(zone);
  75.       upnum=calc_and_upmap(N,guess,zone);
  76.       if(upnum!=0)//有更新
  77.       {
  78.          leftnum-=upnum;//更新剩余的方格
  79.          leftnum-=update_by_hiszone(N,guess);//用历史zone更新
  80.       }
  81.       save_zone_info(zone);
  82.    }
  83.    hisindex=0;//每个case结束后要复位
  84. }




  1. #ifndef _CRT_SECURE_NO_WARNINGS
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #endif

  4. #include <stdio.h>

  5. #define MAX_MAP_SIZE        64
  6. #define ZONE_SIZE            4

  7. #define MAX_SCAN_COUNT        100000

  8. extern void reconstruct(int N, char map[MAX_MAP_SIZE][MAX_MAP_SIZE]);

  9. static char map[MAX_MAP_SIZE][MAX_MAP_SIZE];
  10. static int N;
  11. static int seed;
  12. static int scancount;

  13. static int pseudo_rand() {
  14.     seed = seed * 214013 + 2531011;
  15.     return (seed >> 16) & 0x7fff;
  16. }

  17. void randomscan(char zone[ZONE_SIZE][ZONE_SIZE]) {
  18.     if (scancount >= MAX_SCAN_COUNT)
  19.         return;

  20.     int x0 = (pseudo_rand() % ((N - ZONE_SIZE) / 2 + 1)) * 2; // x0 = 0, 2, 4, ..., N - 4
  21.     int y0 = (pseudo_rand() % ((N - ZONE_SIZE) / 2 + 1)) * 2; // y0 = 0, 2, 4, ..., N - 4
  22.     
  23.     for (int y = 0; y < ZONE_SIZE; ++y)
  24.     for (int x = 0; x < ZONE_SIZE; ++x)
  25.         zone[y][x] = map[y + y0][x + x0];
  26.     
  27.     ++scancount;
  28. }

  29. static void makemap() {    
  30.     for (int y = 0; y < N; ++y)
  31.     for (int x = 0; x < N; ++x)
  32.         map[y][x] = pseudo_rand() % 15 + 1;
  33. }

  34. static bool compare(char map1[MAX_MAP_SIZE][MAX_MAP_SIZE], char map2[MAX_MAP_SIZE][MAX_MAP_SIZE]) {
  35.     for (int y = 0; y < N; ++y)
  36.     for (int x = 0; x < N; ++x)
  37.         if (map1[y][x] != map2[y][x])
  38.             return false;
  39.     
  40.     return true;
  41. }

  42. int main() {
  43.     int TC, totalscore, totalscan;

  44.     //freopen("sample_input.txt", "r", stdin);

  45.     setbuf(stdout, NULL);
  46.     scanf("%d", &TC);

  47.     totalscore = totalscan = 0;
  48.     for (int testcase = 1; testcase <= TC; ++testcase) {
  49.         char guess[MAX_MAP_SIZE][MAX_MAP_SIZE];
  50.         int min_scan_count;
  51.         
  52.         scanf("%d %d %d", &N, &seed, &min_scan_count);

  53.         makemap();
  54.         
  55.         scancount = 0;
  56.         
  57.         for (int y = 0; y < N; ++y)
  58.         for (int x = 0; x < N; ++x)
  59.             guess[y][x] = 0;
  60.         
  61.         for (int k = 0; k < N; ++k) {
  62.             guess[k][0] = map[k][0];
  63.             guess[0][k] = map[0][k];
  64.             guess[k][1] = map[k][1];
  65.             guess[1][k] = map[1][k];
  66.             guess[k][N - 1] = map[k][N - 1];
  67.             guess[N - 1][k] = map[N - 1][k];
  68.             guess[k][N - 2] = map[k][N - 2];
  69.             guess[N - 2][k] = map[N - 2][k];
  70.         }
  71.         
  72.         reconstruct(N, guess);
  73.         
  74.         if (!compare(map, guess))
  75.             scancount = MAX_SCAN_COUNT;
  76.         totalscan += scancount;
  77.         
  78.         int score;
  79.         if (TC == 5) { // sample testcase
  80.             score = scancount < MAX_SCAN_COUNT ? 100 : 0;
  81.             totalscore += score;
  82.         } else { // eval testcase
  83.             score = scancount <= min_scan_count + (min_scan_count / 2) ? 100 : 0;
  84.             totalscore += score;
  85.         }
  86.         
  87.         printf("#%d %d\n", testcase, score);
  88.     }

  89.     if (totalscan > MAX_SCAN_COUNT) totalscan = MAX_SCAN_COUNT;
  90.     printf("total score = %d, total scan = %d\n", totalscore / TC, totalscan);
  91.     return 0;
  92. }
  93. /*
  94. 5
  95. 8 26585 3
  96. 12 24059 28
  97. 16 26751 50
  98. 20 19164 100
  99. 24 10446 153
  100. */

阅读(1393) | 评论(1) | 转发(0) |
0

上一篇:[Leetcode]0069二分法

下一篇:POJ 2454

给主人留下些什么吧!~~

zhln2018-03-19 18:03:13

hisupnum=0;
do{
   hisupnum=update_by_hiszone(N,guess);//用历史zone更新
   leftnum-=hisupnum;
}while(hisupnum>0);