回 帖 发 新 帖 刷新版面

主题:OJ上是怎么计算Memory的?

TOJ 1021题

#include<stdio.h>
#include<string.h>
#include<memory.h>
typedef unsigned char byte;
byte mazemap[77][10];
byte visited[77][10];
#define getmap(x,y) (mazemap[y][x/8]&(1<<(7-x%8)))
#define setmap(x,y) mazemap[y][x/8]|=1<<(7-x%8)
#define blkmap(x,y) mazemap[y][x/8]&=~(1<<(7-x%8))
#define getvis(x,y) (visited[y][x/8]&(1<<(7-x%8)))
#define setvis(x,y) visited[y][x/8]|=1<<(7-x%8)
int width,height;
/*queue define:*/
#define QueMax 3000
typedef struct
{
    short int data[QueMax][3];
    int front,rear,count;
}MyQueue;
void initQueue(MyQueue *que)
{
    que->front=que->rear=que->count=0;
}
void EnQueue(MyQueue *que,int x,int y,int path)
{
    if(que->count==QueMax)
    {
        /*error*/
        return;
    }
    /*printf("new node Entered the queue:\nx=%d,y=%d,path=%d\n",x,y,path);
    */
    que->data[que->rear][0]=x;
    que->data[que->rear][1]=y;
    que->data[que->rear][2]=path;
    que->rear++;
    que->count++;
    if(que->rear==QueMax)
        que->rear=0;
}
void DeQueue(MyQueue *que,/*out*/int *x,int *y,int *path)
{
    if(que->count==0)
    {
        /*error*/
        return;
    }
    *x=que->data[que->front][0];
    *y=que->data[que->front][1];
    *path=que->data[que->front][2];
    que->front++;
    que->count--;
    if(que->front==QueMax)
        que->front=0;
}
int EmptyQueue(MyQueue *que)
{
    return que->count==0;
}
/*queue end*/
MyQueue theque;
int main()
{
    int n,w,h,i;
    int x1,x2,y1,y2;
    scanf("%d",&n);
    for(i=0;i<n;++i)
    {
        scanf("%d%d",&w,&h);
        void readmap(int,int);
        readmap(w,h);
        /*
        void showmap();
        showmap();*/
        while(1)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            if(x1==0&&y1==0&&x2==0&&y2==0)
                break;
            int pathlen(int,int,int,int);
            printf("%d\n",pathlen(x1,y1,x2,y2));
        }
    }
    return 0;
}
int getline(char *str)
{
    char c;
    while(1)
    {
        c=getc(stdin);
        if(c==EOF)return 0;
        if(c=='\n')break;
        *str++=c;
    }
    *str='\0';
    return 1;
}
void readmap(int w,int h)
{
    int i,j;
    char buffer[256];
    memset(mazemap,0,(h+2)*10);
    for(i=0;i<h;++i)
    {
        fflush(stdin);
        getline(buffer);
        for(j=1;j<=w;++j)
            if(buffer[j-1]=='X')
                setmap(j,i+1);
    }
    width=w;
    height=h;
}

void showmap()
{
    int i,j;
    for(i=0;i<height;++i)
    {
        for(j=1;j<=width;++j)
            if(getmap(j,i+1))
                printf("X");
            else
                printf(" ");
        printf("\n");
    }
}

int pathlen(int x,int y,int x2,int y2)
{
    int path=0,scan;
    
    memset(visited,0,(height+2)*10);
    setvis(x,y);
    initQueue(&theque);
    blkmap(x2,y2);
    /*BFS:*/
    while(x!=x2||y!=y2)
    {
        path++;
        /*四个方向上扫描*/
        /*left x*/
        for(scan=x-1;scan>=0&&getmap(scan,y)==0&&getvis(scan,y)==0;scan--)
        {
            EnQueue(&theque,scan,y,path);
            setvis(scan,y);
        }
        /*right x*/
        for(scan=x+1;scan<=(width+1)&&getmap(scan,y)==0&&getvis(scan,y)==0;scan++)
        {
            EnQueue(&theque,scan,y,path);
            setvis(scan,y);
        }
        /*up y*/
        for(scan=y-1;scan>=0&&getmap(x,scan)==0&&getvis(x,scan)==0;scan--)
        {
            EnQueue(&theque,x,scan,path);
            setvis(x,scan);
        }
        /*down y*/
        for(scan=y+1;scan<=(height+1)&&getmap(x,scan)==0&&getvis(x,scan)==0;scan++)
        {
            EnQueue(&theque,x,scan,path);
            setvis(x,scan);
        }
        if(EmptyQueue(&theque))
            return 0;
        DeQueue(&theque,&x,&y,&path);
    }
    setmap(x2,y2);
    return path;
}

我怎么算也不会超过1000k,到底是怎么回事?

回复列表 (共4个回复)

沙发

我就三个全局变量

printf("%d KB",sizeof(mazemap)+sizeof(visited)+sizeof(theque));

显然出来也不可能有1M啊

板凳

去掉fflush(stdin);
否则你程序读不到数据了。

3 楼

那也应该是WrongAnswer啊~~

4 楼

谢谢kaikai~~

我来回复

您尚未登录,请登录后再回复。点此登录或注册