1001 拯救丁爸 BFS模板

拯救丁爸
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 24 Accepted Submission(s) : 9
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
丁爸被火星人抓走了,关在一个N * M(N,M <= 200)矩形的监狱(监狱里有墙壁、道路和警卫队)。

丁爸的学生想拯救他(到达丁爸停留的位置即视为成功)。拯救过程中若遇到警卫,则必须干掉(丁爸的学生足够强大,能够战胜所有的警卫)。假设每次向上,向下,向右,向左移动需要1个单位时间,杀死一个守卫额外需要1个单位时间。

请计算:拯救丁爸需要的最短时间(每次只能上,下,左,右移动到边界内的邻居网格)。
Input
题目包含多组测试数据。

每组数据第一行包含两个整数N和M。
然后是N行,每行包含M个字符。
其中:
“.” 代表道路,
“a”代表丁爸的位置,
每个“r”代表丁爸的一个学生,
"x"代表警卫,
“#”代表墙壁。

处理到文件的末尾。
Output
对于每组数据,请输出拯救丁爸需要的最短时间。
如果实在无法救出丁爸,请输出一句: “Poor Dingba has to stay in the prison all his life.”
每组数据输出一行。
Sample Input
7 8
#.#####.
#.a#…r.
#…#x…
…#…#.#
#…##…
.#…

Sample Output
13

#include<bits/stdc++.h>
using namespace std;
char a[210][210];
int vis[210][210];
struct node{int x,y,step;};
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int main(){int n, m;while(cin>>n>>m){memset(vis,0,sizeof(vis));int sx, ed;for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){cin>>a[i][j];if(a[i][j]=='a'){sx=i;ed=j;}}}int ans = 1e9+10;queue<node>q; q.push(node{sx,ed,0});while(q.size()){node t = q.front(); q.pop();if(a[t.x][t.y]=='r')ans = min(ans,t.step);for(int i = 0; i < 4; i++){int nx = t.x+dx[i], ny = t.y+dy[i];if(nx<1||nx>n||ny<1||ny>m||vis[nx][ny]||a[nx][ny]=='#')continue;if(a[nx][ny]=='x')q.push(node{nx,ny,t.step+2});else q.push(node{nx,ny,t.step+1});vis[nx][ny] = 1;}}if(ans==1e9+10)cout<<"Poor Dingba has to stay in the prison all his life.\n";else cout<<ans<<"\n";}return 0;
}

1002 最佳编码 哈夫曼编码(优先队列)

最佳编码
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 19 Accepted Submission(s) : 11
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
文本编码是计算机通信中的常见问题。

以文本“AAAAABCD”为例,如果使用ASCII,则一共需要64位(因为每个字符的ASCII编码都是需要8位)。

对应的,如果我们将A编码为“00”,“B”为“01”,“C”为“10”,“D”为“11”,那么我们可以只编码16位; 得到的位模式将是“0000000000011011”。然而,这仍然是一种固定长度的编码。

由于字符“A”的出现频率较高,我们是否可以通过用较少的位对它进行编码来获得更好的效果?

事实上,我们确实可以。

最佳编码是将“A”标记为“0”,“B”标记为“10”,“C”标记为“110”,“D”标记为“111”。(显然,这不是唯一的最佳编码,因为很显然,对B,C和D可以自由交换,而不增加最终编码的大小。)

使用这种编码,上述的字符串仅需要13位,编码为“0000010110111”,相对ASCII编码方案,压缩比为4.9比1。通过从左到右读取该编码,您会发现,无前缀编码可以很容易地将其解码为原始文本,即使代码具有不同的位长度。
Input
输入包含多组文本字符串,每行一个。
文本字符串将只包含大写字母、数字字符和下划线(用于代替空格)。
单词“END”表示输入结束,不做处理。
Output
对于输入中的每个文本字符串,输出8位ASCII编码的长度、最佳编码的长度,以及对应的压缩率比值,压缩比精确到小数点后一位。
Sample Input
AAAAABCD
THE_CAT_IN_THE_HAT
END
Sample Output
64 13 4.9
144 51 2.8

HDU1053
Entropy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8885 Accepted Submission(s): 3803

Problem Description
An entropy encoder is a data encoding method that achieves lossless data compression by encoding a message with “wasted” or “extra” information removed. In other words, entropy encoding removes information that was not necessary in the first place to accurately encode the message. A high degree of entropy implies a message with a great deal of wasted information; english text encoded in ASCII is an example of a message type that has very high entropy. Already compressed messages, such as JPEG graphics or ZIP archives, have very little entropy and do not benefit from further attempts at entropy encoding.

English text encoded in ASCII has a high degree of entropy because all characters are encoded using the same number of bits, eight. It is a known fact that the letters E, L, N, R, S and T occur at a considerably higher frequency than do most other letters in english text. If a way could be found to encode just these letters with four bits, then the new encoding would be smaller, would contain all the original information, and would have less entropy. ASCII uses a fixed number of bits for a reason, however: it’s easy, since one is always dealing with a fixed number of bits to represent each possible glyph or character. How would an encoding scheme that used four bits for the above letters be able to distinguish between the four-bit codes and eight-bit codes? This seemingly difficult problem is solved using what is known as a “prefix-free variable-length” encoding.

In such an encoding, any number of bits can be used to represent any glyph, and glyphs not present in the message are simply not encoded. However, in order to be able to recover the information, no bit pattern that encodes a glyph is allowed to be the prefix of any other encoding bit pattern. This allows the encoded bitstream to be read bit by bit, and whenever a set of bits is encountered that represents a glyph, that glyph can be decoded. If the prefix-free constraint was not enforced, then such a decoding would be impossible.

Consider the text “AAAAABCD”. Using ASCII, encoding this would require 64 bits. If, instead, we encode “A” with the bit pattern “00”, “B” with “01”, “C” with “10”, and “D” with “11” then we can encode this text in only 16 bits; the resulting bit pattern would be “0000000000011011”. This is still a fixed-length encoding, however; we’re using two bits per glyph instead of eight. Since the glyph “A” occurs with greater frequency, could we do better by encoding it with fewer bits? In fact we can, but in order to maintain a prefix-free encoding, some of the other bit patterns will become longer than two bits. An optimal encoding is to encode “A” with “0”, “B” with “10”, “C” with “110”, and “D” with “111”. (This is clearly not the only optimal encoding, as it is obvious that the encodings for B, C and D could be interchanged freely for any given encoding without increasing the size of the final encoded message.) Using this encoding, the message encodes in only 13 bits to “0000010110111”, a compression ratio of 4.9 to 1 (that is, each bit in the final encoded message represents as much information as did 4.9 bits in the original encoding). Read through this bit pattern from left to right and you’ll see that the prefix-free encoding makes it simple to decode this into the original text even though the codes have varying bit lengths.

As a second example, consider the text “THE CAT IN THE HAT”. In this text, the letter “T” and the space character both occur with the highest frequency, so they will clearly have the shortest encoding bit patterns in an optimal encoding. The letters “C”, “I’ and “N” only occur once, however, so they will have the longest codes.

There are many possible sets of prefix-free variable-length bit patterns that would yield the optimal encoding, that is, that would allow the text to be encoded in the fewest number of bits. One such optimal encoding is to encode spaces with “00”, “A” with “100”, “C” with “1110”, “E” with “1111”, “H” with “110”, “I” with “1010”, “N” with “1011” and “T” with “01”. The optimal encoding therefore requires only 51 bits compared to the 144 that would be necessary to encode the message with 8-bit ASCII encoding, a compression ratio of 2.8 to 1.

Input
The input file will contain a list of text strings, one per line. The text strings will consist only of uppercase alphanumeric characters and underscores (which are used in place of spaces). The end of the input will be signalled by a line containing only the word “END” as the text string. This line should not be processed.

Output
For each text string in the input, output the length in bits of the 8-bit ASCII encoding, the length in bits of an optimal prefix-free variable-length encoding, and the compression ratio accurate to one decimal point.

Sample Input
AAAAABCD
THE_CAT_IN_THE_HAT
END

Sample Output
64 13 4.9
144 51 2.8

Source
Greater New York 2000

Recommend

//求哈夫曼编码的长度:用优先队列模拟哈夫曼树的建立过程,只要将每次最小的出队的两个元素合成一个新的大数,然后放进优先队列中,直到只剩下一个元素(根节点)为止。
#include<bits/stdc++.h>
using namespace std;
int cnt[50];
int main(){string s;while(cin>>s){if(s=="END")break;memset(cnt,0,sizeof(cnt));for(int i = 0; i < s.size(); i++){if(s[i]=='_')cnt[26]++;else cnt[s[i]-'A']++;}priority_queue<int,vector<int>,greater<int> >q;for(int i = 0; i <= 26; i++){if(cnt[i])q.push(cnt[i]);}int sum = 0;while(q.size()>1){int x = q.top();  q.pop();int y = q.top();  q.pop();q.push(x+y);sum += (x+y);}if(sum==0)sum=s.size();//只有同一种元素printf("%d %d %.1lf\n",s.size()*8,sum,s.size()*8.0/sum);}return 0;
}

HDOJ水题集合5:杂题相关推荐

  1. [HZWER NOIP模拟题][杂题][防骗题]数列

    简要题意: 长度为n的数列,有m个询问,每组询问a,b,c,表示要求出最小的i满足a*(i+1)*xi^2+(b+1)*i*xi+c+i=0,为保证强制在线,实际的a=a+lastans(b,c同理) ...

  2. 【刷题记录】杂题记录

    1.[bzoj 4552][Tjoi2016&Heoi2016]排序 题意:给出一个1到n的全排列,现在对这个全排列序列进行m次局部排序.排序分为两种:(0,l,r)表示将区间[l,r]的数字 ...

  3. AcWing蓝桥杯AB组辅导课10、疑难杂题

    文章目录 前言 例题1:AcWing 1242. 修改数组(并查集) 分析 题解:单链表式并查集 例题2:AcWing 1234. 倍数问题(背包问题+贪心) 分析 题解1:01背包问题,三维解法(贪 ...

  4. DSt:数据结构的最强学习路线之数据结构知识讲解与刷题平台、刷题集合、问题为导向的十大类刷题算法(数组和字符串、栈和队列、二叉树、堆实现、图、哈希表、排序和搜索、动态规划/回溯法/递归/贪心/分治)总

    DSt:数据结构的最强学习路线之数据结构知识讲解与刷题平台.刷题集合.问题为导向的十大类刷题算法(数组和字符串.栈和队列.二叉树.堆实现.图.哈希表.排序和搜索.动态规划/回溯法/递归/贪心/分治)总 ...

  5. NOIP历年第二轮入门组真题集合

    NOIP历年第二轮入门组真题集合 年份 题目 题解链接 2000 T1-计算器的改良 T2- 税收与补贴问题 T3-乘积最大 T4-单词接龙 题解链接 2001 T1-数的记数 T2-最大公约数和最小 ...

  6. 杂题记录及简要题解(一)

    一些前几天做过的还不错的但是不是太想专门花一整篇博客的篇幅去写的题就简要地记录在这里. 说是简要题解,其实写得还是挺详细的.之后的杂题记录可能就会写得简略一点. CF1060E Sergey and ...

  7. 贼全面的计算机考研数据结构算法题集合(408+自命题均可)

    文章目录 Code 数组 合并排序的数组 约瑟夫环问题--高效解法 栈 栈实现队列 最小栈 逆波兰表达式求值 队列 设计循环队列 链表 删除链表节点 删除链表中间节点 删除链表的倒数第n个节点 删除链 ...

  8. arrays中copyof复制两个数组_C语言100题集合026-使用指针交换两个数组中的最大值

    系列文章<C语言经典100例>持续创作中,欢迎大家的关注和支持. 喜欢的同学记得点赞.转发.收藏哦- 后续C语言经典100例将会以pdf和代码的形式发放到公众号 欢迎关注:计算广告生态 即 ...

  9. c语言 空格_C语言100题集合-ex003

    系列文章<C语言经典100例>持续创作中,欢迎大家的关注和支持. 喜欢的同学记得点赞收藏哦- 1 题目 函数:fun() 功能:统计一行字符串单词的个数,作为函数值返回 描述:一行字符串在 ...

  10. 网易2017春招笔试真题编程题集合

    网易2017春招笔试真题编程题集合 题目来源:牛客网 https://www.nowcoder.com/profile/7952866/test/7811777/83061 1.双核处理 题目描述 一 ...

最新文章

  1. Pandas使用to_dict函数将dataframe转化为字典(dict)格式数据并指定orientation参数生成不同形式的字典
  2. linux RTX2080显卡驱动
  3. Thread-方法以及wait、notify简介
  4. IOS debug网络PonyDebugger 实践篇
  5. CSS+DIV定位分析(relative,absolute,static,fixed)
  6. BMI指数与体重的关系
  7. 使用Angular可重用Component思路实现一个自带图标(icon)的input控件
  8. Java中的13个原子操作类
  9. Redis笔记之基本数据结构 链表
  10. websocket学习总结记录
  11. 毕设日志5.12凌晨
  12. 5G 商用时,我们所处的智能城市会是什么样?
  13. Greenplum:你不可不知的实施与维护最佳实践
  14. CMOS中的 latch-up 闩锁效应、添加tap解决latch-up、使用combained area绘制TAP TAP的作用 IC后端版图【VLSI】
  15. 机器学习——鸢尾花数据集
  16. 鸿蒙超级终端使用教程,超级终端怎么用(超级终端使用配置教程)
  17. WPF界面设计的方法
  18. 整理了20个数据可视化大屏模板,拿来即用
  19. 通过Vue解决跨域问题(proxy配置代理)
  20. 欢迎大家加入Xcode公社

热门文章

  1. IPython 的使用
  2. 深度学习实践指南(四)—— 一个典型的深度学习的实践流程
  3. 数据结构在程序中的实现及表现形式
  4. 从“递归结构”到解汉诺塔问题的求解
  5. matplotlib —— fill between
  6. python 删除set指定值_python中的变量和数据类型(三)
  7. python随机数据库_Python实现生成随机数据插入mysql数据库的方法
  8. 从零开始学习python编程-从0开始的Python学习014面向对象编程(推荐)
  9. python画散点图-python画时间序列散点图
  10. python能做什么程序-Python能做什么,能够开发什么项目?