题目描述

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:
11101000
00100011
01010010
01111000
00000001
So 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;
}

  

最新文章

  1. 创建Github远程仓库
  2. WF2013Low Power芯片
  3. 由一条Linux的grep命令说起
  4. Java 基础知识 练习题
  5. 用于主题检测的临时日志(d94169f9-f1c0-45a2-82d4-6edc4bd35539 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
  6. Powerdesigner设置表结构对齐方式
  7. 数据结构(动态树):COGS 27. [WC 2006] 水管局长
  8. 输入 URL 到页面完成加载过程中的所有发生的事情?
  9. WOW.js – 让页面滚动更有趣
  10. 10分钟入门spark
  11. [WinForm]C# .net防止一个程序(WinForm)重复运行的方法。
  12. JAVA 调用https接口, java.security.cert.CertificateException
  13. 使用loadrunner录制脚本的思路和注意要点
  14. 解决电脑上PPT频繁刷新的问题
  15. ldconfig命令
  16. geoserver 源码介绍
  17. &lt;%@ include&gt; &lt;jsp:include&gt;
  18. Infinite size of Hypothesis set and growth function
  19. 优化案例--改写IN条件为INNER JOIN
  20. 解方程(NOIP2014)Warning!(前方高能!!)

热门文章

  1. 关于IE浏览器输入框字体不兼容问题
  2. UVA 1575 Factors
  3. [Luogu 1967] NOIP2013 货车运输
  4. dotnet core 实践——日志组件Serilog
  5. 【BZOJ】1537: [POI2005]Aut- The Bus
  6. 数据类型的判断 --Object.prototype.toString.call(obj)精准检测对象类型
  7. 怎么让IIS7第一次访问相应速度加快
  8. Python os.path.dirname(__file__) os.path.join(str,str)
  9. 简单的企业会议管理cms后台模板——后台
  10. Django 1.10中文文档-第一个应用Part7-自定义管理站点