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=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
return ;
} int main()
{
in(n);queue<int>que;
for(int i=;i<=n;i++) in(ai[i]);
if_[ai[]]=true,dis[ai[]]=;que.push(ai[]);
while(!que.empty())
{
int now=que.front();
if(!if_[ai[now]])
{
que.push(ai[now]);
if_[ai[now]]=true;
dis[ai[now]]=dis[now]+;
}
if(now+<=n&&!if_[now+])
{
que.push(now+);
if_[now+]=true;
dis[now+]=dis[now]+;
}
if(now->&&!if_[now-])
{
que.push(now-);
if_[now-]=true;
dis[now-]=dis[now]+;
}
que.pop();
}
cout<<dis[n];
return ;
}

AC日记——图灵机游戏 codevs 2292的更多相关文章

  1. AC日记——约瑟夫问题 codevs 1282

    1282 约瑟夫问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 有编号从1到N的N个小 ...

  2. AC日记——[SCOI2009]游戏 bzoj 1025

    [SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...

  3. AC日记——国王游戏 洛谷 P1080

    国王游戏 思路: 贪心+高精: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1005 struct Dat ...

  4. AC日记——色板游戏 洛谷 P1558

    色板游戏 思路: sb题: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 struct Tre ...

  5. AC日记——[SCOI2010]游戏 bzoj 1854

    1854: [Scoi2010]游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4938  Solved: 1948[Submit][Status] ...

  6. AC日记——丑数 codevs 1246

    1246 丑数 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 对于一给定的素 ...

  7. AC日记——砍树 codevs 1388

    1388 砍树  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 伐木工人米尔科需要砍倒M米长的木 ...

  8. AC日记——元素查找 codevs 1230

    1230 元素查找  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 给出n个正整数,然后有 ...

  9. AC日记——石子归并 codevs 1048

    1048 石子归并  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 有n堆石子排成一列,每堆石子 ...

随机推荐

  1. MySQL在windows上的安装步骤

    参考文章MySQL安装及建议:https://zhuanlan.zhihu.com/p/44977117 但在进入mysql中修改root命令时,使用文章中的命令: ALTER USER 'root' ...

  2. C++简易酒店管理系统,实现(查询、入住、退房、楼层选择、退出)功能

    #include <iostream> #include <string.h> #include <stdlib.h> void enter(); void che ...

  3. overtrue/wechat 包 由 sys_get_temp_dir 引发的 the directory "c:\Windows" is not writable

    vendor\overtrue\wechat\src\Foundation\Application.php registerBase 方法 在初始化属性时 $this['cache'] = funct ...

  4. destoon 自定义session丢失

    destoon 在使用session之前 应该实例化 $s​ession = new dsession(); destoon通过配置文件加载了不同session存储方式.如果你使用session的页面 ...

  5. 配置wamp开发环境之mysql的配置

    此前我已经将wamp配置的Apache.PHP.phpmyadmin全部配置完成,以上三种配置参照 配置wamp开发环境 下面我们来看看mysql的配置,这里用的是mysql5.5.20,下载地址: ...

  6. POJ 1791 Parallelogram Counting(求平行四边形数量)

    Description There are n distinct points in the plane, given by their integer coordinates. Find the n ...

  7. PTA 7-2 符号配对

    直接用栈模拟即可,数组可做,但因为这节数据结构是栈,为了期末考试还是手写一下栈的操作,值得注意的是,这道题用gets函数在PTA上会编译错误,用scanf("%[^\n]", st ...

  8. Post页面爬取失败__编码问题

    python3爬取Post页面时, 报以下错误 "POST data should be bytes or an iterable of bytes. It cannot be of typ ...

  9. oracle 控制文件的重建

    目录 oracle 控制文件的重建 NORESETLOGS RESETLOGS oracle 控制文件的重建 不到最后时刻,如三个控制文件都已损坏,又没有控制文件的备份.还是不要重建控制文件,处理不好 ...

  10. TOJ3031: Multiple bfs

    3031: Multiple Time Limit(Common/Java):2000MS/6000MS     Memory Limit:65536KByte Total Submit: 60   ...