湖南大学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.
输入
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;
}
湖南大学ACM程序设计新生杯大赛(同步赛)J - Piglet treasure hunt Series 2的更多相关文章
- 湖南大学ACM程序设计新生杯大赛(同步赛)A - Array
题目描述 Given an array A with length n a[1],a[2],...,a[n] where a[i] (1<=i<=n) is positive integ ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)L - Liao Han
题目描述 Small koala special love LiaoHan (of course is very handsome boys), one day she saw N (N<1e1 ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)B - Build
题目描述 In country A, some roads are to be built to connect the cities.However, due to limited funds, ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)I - Piglet treasure hunt Series 1
题目描述 Once there was a pig, which was very fond of treasure hunting. The treasure hunt is risky, and ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)E - Permutation
题目描述 A mod-dot product between two arrays with length n produce a new array with length n. If array ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)D - Number
题目描述 We define Shuaishuai-Number as a number which is the sum of a prime square(平方), prime cube(立方), ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)H - Yuanyuan Long and His Ballons
题目描述 Yuanyuan Long is a dragon like this picture? I don’t know, ...
- 湖南大学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 ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)C - Do you like Banana ?
题目描述 Two endpoints of two line segments on a plane are given to determine whether the two segments a ...
随机推荐
- pthread在Qt+Windows下的使用
pthread是牛逼的跨平台线程库,无需多介绍. 下载pthread-win32,解压后将x86里的pthreadVC2.dll放到system32目录里,将pthreadVC2.lib放入项目中去, ...
- JS零碎小知识
filter()方法对数组进行过滤,生成新数组 var aqiNewData = aqiData.filter(function(data){ return data[1]>60; }); // ...
- DHTML中window的使用
window对象是对浏览器窗口进行操作的对象.以下列出一些常用的对象(三级为对象的方法.属性) |-navigator:是对浏览器信息进行操作的对象 |-history:包含用户浏览过的url信息 | ...
- 基本控件文档-UITextField属性---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...
- TreeMap put 操作分析
public V put(K key, V value) { //t 表示当前节点,记住这个很重要!先把TreeMap 的根节点root 的引用赋值给当前节点 TreeMap.Entry<K,V ...
- 小程序Openid 获取,服务器 encryptedData 解密 遇到的坑
获取客户 openId 和 unionId 需要以下步骤(都为必须步骤) 1.从验证从客户端传上来code, 获取sessionKey (需要配合小程序appid ,secret 发送到微信服务器) ...
- nesC编程入门
1.接口 NesC程序主要由各式组件(component)构成,组件和组件之间通过特定的接口(interface)互相沟通.一个接口内声明了提供相关服务的方法(C语言函数).例如数据读取接口(Read ...
- sqlite3在Linux下的安装和使用
自我补充:ubuntu在线安装sqlite3数据库的方法: 系统平台:ubuntu12.04 在ubuntu里面直接使用命令:sudo apt-get install sqlite3 ,出现: ...
- ProxySQL 监控和统计
ProxySQL 监控和统计 很多有价值的统计数据在stats和monitor库中. admin@127.0.0.1 [(none)]>SHOW TABLES FROM stats; +---- ...
- MySQL数据库的“十宗罪”【转】
今天就给大家列举 MySQL 数据库中最经典的十大错误案例,并附有处理问题的解决思路和方法.希望能给刚入行或数据库爱好者一些帮助,今后再遇到任何报错,我们都可以很淡定地去处理.学习任何一门技术的同时, ...