湖南大学ACM程序设计新生杯大赛(同步赛)J - Piglet treasure hunt Series 2

题目描述

Once there was a pig, which was very fond of treasure hunting. One day, when it woke up, it found itself in a strange land of treasure. As for how to come in, or how to go out, no ways to know. Sad.

The good news is, it was lucky that it got the treasure map.

But there is a bad news too, this map is an encrypted map.

You can think of it as a matrix of R*C. Each grid is a number of Hexadecimal(十六进制), that is the number is between {‘0’,’1’,…’9’,’A’,’B’,…,’F’}, and the 4 length Binary number(4位二进制数) corresponding is the real treasure map.

For example, the number ‘0‘ on the encrypted graph represents the actual map ‘0000’, and ‘1‘ represents the actual map ‘0001’, ... The ‘A‘ represents the actual map ‘1010’, and ‘F‘ represents the actual map ‘1111’.

The owner of the treasure is very clever, he build some insuperable wall in the treasure to avoid stealing, we use ‘0’ to describe it. Obviously ‘1’ indicated that this grid bury exactly one diamond.

The pig can only walk up to four adjacent squares in the upper, lower, left and right directions at a time. Wherever it goes, it will dig out the diamond in this grid(if there is diamond buried in the grid) and put the diamond in its own package.

Though it has got the map, but doesn‘t know where it is in the peach blossom trap now, that means it could be at any ‘.’ in the matrix. It finds you smart to tell it how many diamonds it will get at most.

输入描述:

Multiple groups of test case. (no more than 10 groups. )The first line of each group contains two numbers R and C,(0<=R, C<=3000), representing the number of rows and the number of columns of peach blossom trap, respectively. Stop the program when R and C are both 0.Then there are next R lines, each line contains C characters, describe as above.It is guarantee all the input is legal.

输出描述:

For each test case, output the answer on each line, representing the most number of diamonds can be obtained by the pig.

示例1

输入

5 2
E8
23
52
78
01

3 1
0
4
0

0 0

输出

6
1

说明

In the first example, the real treasure map is:1110100000100011010100100111100000000001So it is possible to dig out 6 diamonds at most.

题解

$bfs$,压位。

主要是要想办法省内存,$01$矩阵可以压位,每$32$位$01$串拿一个$int$表示即可。

然后就是普通的$bfs$,队列的内存峰值也只有$1000$个位置的数量级。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 3000 + 10;
int r, c;
char s[maxn];
unsigned int m[maxn][450];
int f[maxn * 4];
int st[maxn], cnt;

int dir[4][2] = {
  {-1, 0},
  {1, 0},
  {0, -1},
  {0, 1},
};

int out(int x, int y) {
  if(x < 0 || x >= r) return 1;
  if(y < 0 || y >= 4 * c) return 1;
  return 0;
}

int Get(int x, int y) {
  unsigned int p = (unsigned)(1 << (y % 32));
  if(m[x][y / 32] & p) return 1;
  return 0;
}

void Set(int x, int y) {
  unsigned int p = (unsigned)(1 << (y % 32));
  m[x][y / 32] = m[x][y / 32] ^ p;
}

int main() {
  while(~scanf("%d%d", &r, &c)) {
    if(r == 0 && c == 0) break;
    for(int i = 0; i < r; i ++) {
      for(int j = 0; j < 400; j ++) {
        m[i][j] = 0;
      }
    }
    for(int i = 0; i < r; i ++) {
      scanf("%s", s);
      int sz = 0;
      for(int j = 0; j < c; j ++) {
        int num;
        if(s[j] >= ‘0‘ && s[j] <= ‘9‘) num = s[j] - ‘0‘;
        else num = s[j] - ‘A‘ + 10;

        cnt = 0;
        for(int k = 0; k < 4; k ++) {
          st[cnt ++] = (num & (1 << k)) ? 1 : 0;
        }
        for(int k = 3; k >= 0; k --) {
          f[sz ++] = st[k];
        }
      }
      for(int j = 0; j < sz; j ++) {
        m[i][j / 32] = m[i][j / 32] + (unsigned)(f[j] * (1 << (j % 32)));
      }
    }

    int ans = 0;

    for(int i = 0; i < r; i ++) {
      for(int j = 0; j < 4 * c; j ++) {
        if(Get(i, j) == 0) continue;
        int sum = 0;
        queue<int> Q;
        Q.push(i * 4 * c + j);
        Set(i, j);
        while(!Q.empty()) {
          int h = Q.front();
          int x = h / (4 * c);
          int y = h % (4 * c);
          Q.pop();
          sum ++;
          for(int i = 0; i < 4; i ++) {
            int tx = x + dir[i][0];
            int ty = y + dir[i][1];
            if(out(tx, ty)) continue;
            if(Get(tx, ty) == 0) continue;
            Q.push(tx * 4 * c + ty);
            Set(tx, ty);
          }
        }
        ans = max(ans, sum);
        if(ans > r*c*2) break;
      }
      if(ans > r*c*2) break;
    }

    printf("%d\n", ans);
  }
  return 0;
}

  

时间: 2024-10-07 05:59:01

湖南大学ACM程序设计新生杯大赛(同步赛)J - Piglet treasure hunt Series 2的相关文章

湖南大学ACM程序设计新生杯大赛(同步赛)L - Liao Han

题目描述 Small koala special love LiaoHan (of course is very handsome boys), one day she saw N (N<1e18) guys standing in a row, because the boys had some strange character,The first time to Liao them will not be successful, but the second time will be su

湖南大学ACM程序设计新生杯大赛(同步赛)B - Build

题目描述 In country  A, some roads are to be built to connect the cities.However, due to limited funds, only some roads can be built.That is to say,if the limit is 100$, only roads whose cost are no more than 100$ can be built. Now give you n cities, m r

湖南大学ACM程序设计新生杯大赛(同步赛)G - The heap of socks

题目描述 BSD is a lazy boy. He doesn't want to wash his socks, but he will have a data structure called 'socks heap'.By maintaining this structure, he can quickly pick out two of the most smelless socks from millions of socks everyday. As one of his good

湖南大学第十四届ACM程序设计新生杯 Dandan&#39;s lunch

Dandan's lunch Description: As everyone knows, there are now n people participating in the competition. It was finally lunch time after 3 hours of the competition. Everyone brought a triangular bread. When they were going to eat bread, some people fo

湖南大学第十四届ACM程序设计新生杯 E.Easy Problem

E.Easy Problem Description: Zghh likes number, but he doesn't like writing problem description. So he will just give you a problem instead of telling a long story for it. Now given a positive integer x and k digits a1,a2,...,ak, can you find a positi

华中农业大学第四届程序设计大赛网络同步赛 J

Problem J: Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1766  Solved: 299[Submit][Status][Web Board] Description Giving a number sequence A with length n, you should choosing m numbers from A(ignore the order) which can form an

H. GSS and Simple Math Problem--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 题目描述 Given n positive integers , your task is to calculate the product of these integers, The answer is less than 输入描述: The first line of input is an integer n, the i-th of the following n lines contains the

陕西师范大学第七届程序设计竞赛网络同步赛 J 黑猫的小老弟【数论/法拉数列/欧拉函数】

链接:https://www.nowcoder.com/acm/contest/121/J来源:牛客网 题目描述 大家知道,黑猫有很多的迷弟迷妹,当然也有相亲相爱的基友,这其中就有一些二五仔是黑猫的小老弟.小老弟是如何产生的呢?聪明的iko告诉黑猫,其实是有规律的(她怎么知道???)! 一开始,有两个原始二五仔,代号0/1和1/1, 从原始二五仔到第n代小老弟,每代相邻两个小老弟a/b和c/d,产生一个新的小老弟(a+c)/(b+d),成为下一代新成员.将每一代的小老弟代号约分(包括0/1,1/

哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练. 题解: E666 这个题是让求有多少个子串只含有6.寻找连续的6,然后用n*(n+1)/2求出这一段的子串个数,然后把每一段连续的加起来. 做的时候wa了很多次,原来是在n*(n+1)的地方已经超过int型了,所以需要设置类型为long long. #include <cstdio> #inc