AC日记——图灵机游戏 codevs 2292
2017-03-20 20:59
190 查看
2292 图灵机游戏
时间限制: 1 s空间限制: 64000 KB
题目等级 : 黄金 Gold
题解
查看运行结果
题目描述 Description
【Shadow 1】第二题
Shadow最近知道了图灵机是什么(Shadow:就是一行格子和一个机器头移来移去的呗!),于是他突发奇想,创造了一个新游戏——“图灵机游戏”(Shadow:好听吧?)。
游戏规则如下:
在一条长长的纸上有N个格子,每个格子上都有一个数,第i格的数记为Ai,机器头刚开始在第1格。这个游戏有两个操作:
1.如果现在在第i格,则可以移动机器头到第Ai格;
2.把某个Ai减少或增加1。
然而,fotile96看了之后却不以为然。“嗯,你挑战一下用最少次数使机器头到达第N格吧,这样好玩些……”
现在,Shadow已经快Crazy了。于是,Shadow把脸转向了你……
输入描述 Input Description
第1行,1个整数N;
第2行,N个整数Ai。
输出描述 Output Description
1行,1个整数,为最少的操作次数。
样例输入 Sample Input
5
3 4 2 5 3
样例输出 Sample Output
3
数据范围及提示 Data Size & Hint
对于30%的数据,1≤N≤10;
对于60%的数据,1≤N≤1000;
对于100%的数据,1≤N≤100000,1≤Ai≤N。
<h4>样例解释</h4>
1.先将第1格的值加1
2.跳到第4格
3.跳到第5格,结束游戏
思路:
bfs;
来,上代码:
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define maxn 100005 using namespace std; int if_z,n,ai[maxn],dis[maxn]; char Cget; bool if_[maxn]; inline void in(int &now) { now=0,if_z=1,Cget=getchar(); while(Cget>'9'||Cget<'0') { if(Cget=='-') if_z=-1; Cget=getchar(); } while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } now*=if_z; return ; } int main() { in(n);queue<int>que; for(int i=1;i<=n;i++) in(ai[i]); if_[ai[1]]=true,dis[ai[1]]=1;que.push(ai[1]); while(!que.empty()) { int now=que.front(); if(!if_[ai[now]]) { que.push(ai[now]); if_[ai[now]]=true; dis[ai[now]]=dis[now]+1; } if(now+1<=n&&!if_[now+1]) { que.push(now+1); if_[now+1]=true; dis[now+1]=dis[now]+1; } if(now-1>1&&!if_[now-1]) { que.push(now-1); if_[now-1]=true; dis[now-1]=dis[now]+1; } que.pop(); } cout<<dis ; return 0; }
相关文章推荐
- codevs2292 图灵机游戏
- 【codevs 2292】图灵机游戏
- codevs 2292 图灵机游戏
- 【codevs】2292图灵机游戏
- AC日记——接龙游戏 codevs 1051
- AC日记——绿色通道 codevs 3342
- codevs 2292 图灵机游戏(BFS)
- 2292 图灵机游戏
- AC日记——中山市选[2009]小明的游戏 bzoj 2464
- AC日记——丑数 codevs 1246
- AC日记——银河英雄传说 codevs 1540
- AC日记——线段树练习三 codevs 1082 (分块尝试)
- AC日记——国王游戏 洛谷 P1080
- AC日记——中庸之道 codevs 2021
- AC日记——矩阵取数游戏 洛谷 P1005
- AC日记——草地排水 codevs 1993
- AC日记——产生数 codevs 1009 (弗洛伊德)(组合数学)
- AC日记——[SHOI2008]小约翰的游戏John bzoj 1022
- Codevs 2292 图灵机游戏 解题报告
- 【codevs2292】图灵机游戏