对一道题解中*(long*)0 = 0的疑问

donghaish 2010-07-18 12:45:17
一道usaco题目的官方题解,对算法清楚,但对其中一个语句(*(long*)0=0;)感到非常困惑,这不是非法访问吗?这样做有什么其他考虑吗?

如果需要题目说明可以访问:
http://sjtulibing.spaces.live.com/blog/cns!2F17193726A8CFC0!139.entry

详细题解如下(语句已被标成红色):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef struct Rect Rect;
struct Rect {
int wid;
int ht;
};

Rect
rotate(Rect r)
{
Rect nr;

nr.wid = r.ht;
nr.ht = r.wid;
return nr;
}

int
max(int a, int b)
{
return a > b ? a : b;
}

int
min(int a, int b)
{
return a < b ? a : b;
}

int tot;
int bestarea;
int bestht[101];

void
record(Rect r)
{
int i;

if(r.wid*r.ht < tot)
*(long*)0=0;

if(r.wid*r.ht < bestarea || bestarea == 0) {
bestarea = r.wid*r.ht;
for(i=0; i<=100; i++)
bestht[i] = 0;
}
if(r.wid*r.ht == bestarea)
bestht[min(r.wid, r.ht)] = 1;
}

void
check(Rect *r)
{
Rect big;
int i;

/* schema 1: all lined up next to each other */
big.wid = 0;
big.ht = 0;
for(i=0; i<4; i++) {
big.wid += r[i].wid;
big.ht = max(big.ht, r[i].ht);
}
record(big);

/* schema 2: first three lined up, fourth on bottom */
big.wid = 0;
big.ht = 0;
for(i=0; i<3; i++) {
big.wid += r[i].wid;
big.ht = max(big.ht, r[i].ht);
}
big.ht += r[3].ht;
big.wid = max(big.wid, r[3].wid);
record(big);

/* schema 3: first two lined up, third under them, fourth to side */
big.wid = r[0].wid + r[1].wid;
big.ht = max(r[0].ht, r[1].ht);
big.ht += r[2].ht;
big.wid = max(big.wid, r[2].wid);
big.wid += r[3].wid;
big.ht = max(big.ht, r[3].ht);
record(big);

/* schema 4, 5: first two rectangles lined up, next two stacked */
big.wid = r[0].wid + r[1].wid;
big.ht = max(r[0].ht, r[1].ht);
big.wid += max(r[2].wid, r[3].wid);
big.ht = max(big.ht, r[2].ht+r[3].ht);
record(big);

/*
* schema 6: first two pressed next to each other, next two on top, like:
* 2 3
* 0 1
*/
big.ht = max(r[0].ht+r[2].ht, r[1].ht+r[3].ht);
big.wid = r[0].wid + r[1].wid;

/* do 2 and 1 touch? */
if(r[0].ht < r[1].ht)
big.wid = max(big.wid, r[2].wid+r[1].wid);
/* do 2 and 3 touch? */
if(r[0].ht+r[2].ht > r[1].ht)
big.wid = max(big.wid, r[2].wid+r[3].wid);
/* do 0 and 3 touch? */
if(r[1].ht < r[0].ht)
big.wid = max(big.wid, r[0].wid+r[3].wid);

/* maybe 2 or 3 sits by itself */
big.wid = max(big.wid, r[2].wid);
big.wid = max(big.wid, r[3].wid);
record(big);
}

void
checkrotate(Rect *r, int n)
{
if(n == 4) {
check(r);
return;
}

checkrotate(r, n+1);
r[n] = rotate(r[n]);
checkrotate(r, n+1);
r[n] = rotate(r[n]);
}

void
checkpermute(Rect *r, int n)
{
Rect t;
int i;

if(n == 4)
checkrotate(r, 0);

for(i=n; i<4; i++) {
t = r[n], r[n] = r[i], r[i] = t; /* swap r[i], r[n] */
checkpermute(r, n+1);
t = r[n], r[n] = r[i], r[i] = t; /* swap r[i], r[n] */
}
}

void
main(void)
{
FILE *fin, *fout;
Rect r[4];
int i;

fin = fopen("packrec.in", "r");
fout = fopen("packrec.out", "w");
assert(fin != NULL && fout != NULL);

for(i=0; i<4; i++)
fscanf(fin, "%d %d", &r[i].wid, &r[i].ht);

tot=(r[0].wid*r[0].ht+r[1].wid*r[1].ht+r[2].wid*r[2].ht+r[3].wid*r[3].ht);

checkpermute(r, 0);
fprintf(fout, "%d\n", bestarea);
for(i=0; i<=100; i++)
if(bestht[i])
fprintf(fout, "%d %d\n", i, bestarea/i);
exit(0);
}

...全文
94 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ayw215 2010-07-18
  • 打赏
  • 举报
回复
usaco官方的测试环境是什么?dos?
gbb21 2010-07-18
  • 打赏
  • 举报
回复
this is equal to ASSERT , used to debug.
gamemagic 2010-07-18
  • 打赏
  • 举报
回复
在DOS模拟器下则不会非法访问。具体干嘛的我不清楚
sparrow081 2010-07-18
  • 打赏
  • 举报
回复
if(r.wid*r.ht < tot) /* 这个条件能 成真吗? 注意它的实现 */
icansaymyabc 2010-07-18
  • 打赏
  • 举报
回复
即使在DOS下,那段代码也是找死

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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