两行看不懂的代码,请路过的朋友指点一下

syjchyx 2010-05-09 06:51:53
这段代码看了半天,也没看明白红色那两行代码是什么意思。请明白的朋友指点一下,或者告诉我哪里有类似代码的教程,十分感谢。


这里是题目
Problem

A magician does the following magic trick. He puts W white balls and B black balls in his hat and asks someone from the audience, say Bob, to remove pairs of balls in whatever order Bob would desire. After removing a pair of balls, Bob is asked to place a white ball back into the hat if they are the same color. Otherwise he is asked to place a black ball into the hat.

When Bob is left with only one ball in the hat, he asks the magician what color the last ball is. Needless to say, the magician can't see the order by which Bob does the replacements.

The problem is that the magician, like most magicians, is old and sometimes forgets how to do the trick. Being the kind person you are, you are going to help the magician.

For each pair of numbers (W,B) you are asked to output one of the following:

"WHITE" - if the last ball in the hat will be white for sure.
"BLACK" - if the last ball in the hat will be black for sure.
"UNKNOWN" - if you can't be sure of the last ball's color.

Input

The first line of the input file contains the number of cases, N. N test cases follow.

Each case contains W and B on a line separated by a space.

Output

For each input case, you should output:

Case #X: Ywhere X is the number of the test case and Y is either "WHITE", "BLACK" or "UNKNOWN" as explained above. (quotes for clarity)

Limits

0 < N ≤ 1000
W + B > 0

Small dataset

0 ≤ W ≤ 1000
0 ≤ B ≤ 1000

Large dataset

0 ≤ W ≤ 109
0 ≤ B ≤ 109

Sample


Input

Output

2
3 1
3 6
Case #1: BLACK
Case #2: WHITE



下面是某高手写的代码
-------------------------------

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forv(i, v) forn(i, v.size())
#define for1(i, n) for (int i = 1; i <= int(n); i++)

#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair

typedef pair<int, int> pii;
typedef vector<int> VI;
typedef long long ll;

#define MASK (1 << 15)
#define NMAX 17
#define INF 1000006

int n, k;
int d[NMAX][MASK];
int x[NMAX], y[NMAX];

void solve(int test)
{
printf("Case #%d: ", test);
scanf("%d %d", &n, &k);
forn(i, n)
{
scanf("%d %d", &x[i], &y[i]);
}
forn(mask, (1 << n))
{
if (mask == 0) continue;
int l = INF, r = -INF, u = -INF, down = INF;
forn(i, n)
{
if (mask & (1 << i))
{
l = min(l, x[i]);
r = max(r, x[i]);
u = max(u, y[i]);
down = min(down, y[i]);
}
}
d[1][mask] = max(u - down, r - l);
}

for (int i = 2; i <= k; i++)
{
forn(mask, (1 << n))
{
if (mask == 0) continue;
d[i][mask] = INF;
for (int m1 = (mask - 1) & mask; m1 > 0; m1 = (m1 - 1) & mask)
{
d[i][mask] = min(d[i][mask], max(d[i - 1][m1], d[1][m1 ^ mask]));
}

}
}

printf("%d\n", d[k][(1 << n) - 1]);
}
int main()
{
freopen("F:\\Projects\\C_GoogleCodeJam\\C_GoogleCodeJam\\C_GoogleCodeJam\\Debug\\B-small-practice.in", "rt", stdin);
freopen("F:\\Projects\\C_GoogleCodeJam\\C_GoogleCodeJam\\C_GoogleCodeJam\\Debug\\output.txt", "wt", stdout);

int tc; scanf("%d", &tc);
forn(it, tc)
{
solve(it + 1);
}
return 0;
}
...全文
145 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
syjchyx 2010-05-11
  • 打赏
  • 举报
回复 1
[Quote=引用 2 楼 kenyyy 的回复:]
for (int m1 = (mask - 1) & mask; m1 > 0; m1 = (m1 - 1) & mask)
枚举了mask 2进制数位数上1的所有组合,可以理解成把0去掉后的全排列
m1 ^ mask为m1相对于mask的互补,就是剩下的m1没有取到的1
[/Quote]

非常感谢,终于明白了
kenyyy 2010-05-10
  • 打赏
  • 举报
回复
for (int m1 = (mask - 1) & mask; m1 > 0; m1 = (m1 - 1) & mask)
枚举了mask 2进制数位数上1的所有组合,可以理解成把0去掉后的全排列
m1 ^ mask为m1相对于mask的互补,就是剩下的m1没有取到的1

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧