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;
}