hautoj1269 爱看电视的LsF BFS

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值