1269: 爱看电视的LsF
时间限制: 1 秒 内存限制: 64 MB提交: 304 解决: 74
提交 状态
题目描述
LsF(刘师傅)非常喜欢看电视!
不幸的是,遥控器上的一些数字按钮坏了。 但他灵光一闪,如果他不能直接输入他想要看到的频道的号码,那么他可以先输入其他号码,再通过按下按钮+ 和- (这两个按钮由24K钛合金制成,永远不会坏)的方式到达所需的频道。 按钮+将数字增加1,按钮-将数字减少1。当然他依然可以使用那些完好无损的数字按钮输入号码。
他最初在第S频道,他想看第T频道。他想知道由S到T频道所需的最少按钮按压次数。
输入
输入包含多组数据。
对于每组数据,第一行是三个整数n,S,T(n≤10,0≤S,T≤500,000。 第二行是n个数字 a1,a2,...,an,表示数字 ai键已经坏了 (0≤ai≤9,ai≠aj when i≠j)。
输出
输入包含多组数据。
对于每组数据,第一行是三个整数n,S,T(n≤10,0≤S,T≤500,000。 第二行是n个数字 a1,a2,...,an,表示数字 ai键已经坏了 (0≤ai≤9,ai≠aj when i≠j)。
样例输入
10 1 1000 1 2 3 4 5 6 7 8 99 1 1000 1 2 3 4 5 6 7 8
样例输出
993
这题,暴力都能过。。。。。深搜也不难。现场去没写。
简单搜索。
#include<string>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#define ll long long
#define x first
#define y second
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int mod=11;
const int maxn=1e5+100;
int n,st,ed;
int a[11];
int judge(int x){
int t,ret=0;
if(x==0&&a[x]==0) return 1;
else if(x==0&&a[x]) return 0;
while(x){
t=x%10;
if(a[t]) return 0;
x/=10;
ret=ret+1;
}
return ret;
}
int dfs(int i){
if(ed-i==st||ed+i==st){
return i;
}
if(ed-i>=0&&judge(ed-i))
return judge(ed-i)+i;
if(judge(ed+i))
return judge(ed+i)+i;
return dfs(i+1);
}
int main(){
while(~scanf("%d %d %d",&n,&st,&ed)){
int t,ans=abs(ed-st),x;
memset(a,0,sizeof(a));
for(int i=0;i<n;++i) scanf("%d",&x),a[x]=1;
ans=dfs(0);
printf("%d\n",ans);
}
return 0;
}