#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
struct po{
int x,time;
};
char num[10];
int a[11],map[500000];
int dir[2]={-1,1};
int escape,key;
int yu(int t)
{
int i,j,tt=t,k;
j=0;
if(tt==0)
{
if(a[0])
return 0;
else
{
key=1;
return 1;
}
}
while(tt>0)
{
num[j]=tt%10;
tt/=10;
j++;
}
key=0;
for(i=0;i<j;i++)
{
if(a[num[i]])
return 0;
key++;
}
return 1;
}
void bfs(po p,int len)
{
int i;
queue<po>que;
que.push(p);
po hd;
if(yu(p.x)){
p.time+=key;
if(p.time>=len){
printf("%d\n",len);
return ;
}
printf("%d\n",p.time);
return ;
}
int y;
while(!que.empty()){
hd=que.front();
que.pop();
if(hd.time>=len){
printf("%d\n",len);
return ;
}
for(i=0;i<2;i++){
if(hd.x==500000&&!map[499999]){
y=hd.x-1;
}
else if(hd.x==0&&!map[1]){
y=hd.x+1;
}
else
y=hd.x+dir[i];
if(y<=500000&&y>=0&&!map[y]){
po next;
next.x=y; next.time=hd.time+1;
if(yu(next.x))
{
next.time+=key;
if(next.time>=len)
{
printf("%d\n",len);
return ;
}
printf("%d\n",next.time);
return ;
}
else
{
map[next.x]=1;
que.push(next);
}
}
}
}
printf("%d\n",len);
return ;
}
int main()
{
int i,n,s,t,q,len;
while(~scanf("%d%d%d",&n,&s,&t))
{
memset(a,0,sizeof(a));
memset(map,0,sizeof(map));
for(i=0;i<n;i++){
scanf("%d",&q);
a[q]=1;//值为1的键是坏的
}
if(s==t){
printf("0\n");
continue;
}
if(s<t) //求出两个频道的距离
len=t-s;
else
len=s-t;
po p;
p.x=t, p.time=0;//初始化结构体
map[p.x]=1;//表示已经到达
bfs(p,len);//开始广搜
}
return 0;
}
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
struct po{
int x,time;
};
char num[10];
int a[11],map[500000];
int dir[2]={-1,1};
int escape,key;
int yu(int t)
{
int i,j,tt=t,k;
j=0;
if(tt==0)
{
if(a[0])
return 0;
else
{
key=1;
return 1;
}
}
while(tt>0)
{
num[j]=tt%10;
tt/=10;
j++;
}
key=0;
for(i=0;i<j;i++)
{
if(a[num[i]])
return 0;
key++;
}
return 1;
}
void bfs(po p,int len)
{
int i;
queue<po>que;
que.push(p);
po hd;
if(yu(p.x)){
p.time+=key;
if(p.time>=len){
printf("%d\n",len);
return ;
}
printf("%d\n",p.time);
return ;
}
int y;
while(!que.empty()){
hd=que.front();
que.pop();
if(hd.time>=len){
printf("%d\n",len);
return ;
}
for(i=0;i<2;i++){
if(hd.x==500000&&!map[499999]){
y=hd.x-1;
}
else if(hd.x==0&&!map[1]){
y=hd.x+1;
}
else
y=hd.x+dir[i];
if(y<=500000&&y>=0&&!map[y]){
po next;
next.x=y; next.time=hd.time+1;
if(yu(next.x))
{
next.time+=key;
if(next.time>=len)
{
printf("%d\n",len);
return ;
}
printf("%d\n",next.time);
return ;
}
else
{
map[next.x]=1;
que.push(next);
}
}
}
}
printf("%d\n",len);
return ;
}
int main()
{
int i,n,s,t,q,len;
while(~scanf("%d%d%d",&n,&s,&t))
{
memset(a,0,sizeof(a));
memset(map,0,sizeof(map));
for(i=0;i<n;i++){
scanf("%d",&q);
a[q]=1;//值为1的键是坏的
}
if(s==t){
printf("0\n");
continue;
}
if(s<t) //求出两个频道的距离
len=t-s;
else
len=s-t;
po p;
p.x=t, p.time=0;//初始化结构体
map[p.x]=1;//表示已经到达
bfs(p,len);//开始广搜
}
return 0;
}