| ||||||||||
| 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 | |||||||||
关于判题系统处理use[0]的疑问 关于use[0],我认为应该是use[sx]=1;(sx表示输入的起点)但是发现有解题报告里面写use[0]=1;于是我把2中情况都试了,发现都可以过,这个是为什么呢?难道是我理解错了?目前刚学习宽搜,还希望大家多多指教~3q
代码来自网上的解题报告。还没有自己写。
#include<stdio.h>
#include<string.h>
typedef struct space
{
int x;
int step;
}space;
space q[500005];
bool use[500005];
int sx,ex;
int BFS()
{
int i,x,top=1,base=0;
space a;
while(top!=base)
{
if(q[base].x==ex)
return q[base].step;
a=q[base++];
for(i=0;i<3;i++)
{
if(i==0)
x=a.x+1;
else if(i==1)
x=a.x-1;
else if(i==2)
x=a.x*2;
if(x>=0&&x<=500000&&!use[x])//这个一个关于越界和枝剪以及是否访问过的判断
{
use[x]=1;
q[top].x=x;
q[top].step=a.step+1;
if(ex==x)
return q[top].step;
top++;//这个东西不能乱放,一定要在if{}里面!
}
}
}
return -1;
}
int main()
{
int min;
while(scanf("%d%d",&sx,&ex)!=EOF)
{
memset(use,0,sizeof(use));
q[0].x=sx;
q[0].step=0;
use[0]=1;
// use[sx]=1;
if(sx>=ex)
{
printf("%d\n",sx-ex);
continue;
}
min=BFS();
printf("%d\n",min);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator