Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为什么大家都说数组开大一点呢?戳这里!!这题RE是很正常的,原因是大部分童鞋代码有隐藏的数组越界,实际上开100000多一点就行; 来看我的代码: void bfs(int n,int k) { d[n]=0; queue<int>q; q.push(n); v[n]=1; while(!q.empty()) { int x=q.front(); q.pop(); if(x==k) break; else { if((x+1)>=0&&(x+1)<=N&&!v[x+1]) { q.push(x+1); d[x+1]=d[x]+1; v[x+1]=1; } if((x-1)>=0&&(x-1)<=N&&!v[x-1]) { q.push(x-1); d[x-1]=d[x]+1; v[x-1]=1; } if((x*2)>=0&&(x*2)<=N&&!v[x*2]) { q.push(x*2); d[x*2]=d[x]+1; v[x*2]=1; } } } printf("%d\n",d[k]); } /在判断的时候先判断范围再判断是否访问过,比如长度为10,而当前位置在6,如果跳到12就已经超范围了,而v[2*x]数组开小了就越界了。先判断是否访问过就会存在这种隐藏的越界危险; Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator