2292 Turing machine games, 2292 Turing Machines

Source: Internet
Author: User

2292 Turing machine games, 2292 Turing Machines
2292 Turing machine games

 

Time Limit: 1 s space limit: 64000 KB title level: Gold Title Description Description

[Shadow 1] Question 2

Shadow recently learned what a Turing machine is (Shadow: It's a line of lattice and a machine's head is moved here !), As a result, he created a new game called "Turing Machine game" (Shadow: Good news ?).

The rules are as follows:

There are N grids on a long piece of paper, each of which has a number. The number of I grids is recorded as Ai, and the machine head is in the first 1st grids. This game has two operations:

1. If the machine is in the I grid, you can move the Machine header to the Ai grid;

2. reduce or increase an Ai by 1.

However, fotile96 does not agree after reading it. "Well, you have to challenge the minimum number of requests to get the machine head to the nth level. This is a little fun ......"

Now, Shadow is almost Crazy. So Shadow turned his face to you ......

Input description Input Description1st rows, 1 integer N; 2nd rows, N integer Ai. Output description Output Description

One row, an integer, is the minimum number of operations.

Sample Input Sample Input53 4 2 5 3 sample output Sample Output

3

Data range and prompt Data Size & Hint

For 30% of the data, 1 ≤ N ≤ 10;
For 60% of data, 1 ≤ N ≤ 1000;
For 100% of the data, 1 ≤ N ≤ 100000,1 ≤ Ai ≤ N.

<H4> example 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<cstdlib> 6 using namespace std; 7 const int MAXN=10000001; 8 int n; 9 int a[MAXN];10 int vis[MAXN];11 int dis[MAXN];12 void bfs()13 {14 int step=0;15 queue<int>q;16 q.push(a[1]);17 vis[a[1]]=1;18 dis[a[1]]=1;19 while(q.size()!=0)20 {21 int p=q.front();22 if(p==n)return ;23 q.pop();24 if(vis[a[p]]==0)25 {26 q.push(a[p]);27 dis[a[p]]=dis[p]+1;28 vis[a[p]]=1;29 }30 if(vis[p+1]==0&&p<n)31 {32 q.push(p+1);33 dis[p+1]=dis[p]+1;34 vis[p+1]=1;35 //a[p]++;36 }37 if(vis[p-1]==0&&p>0)38 {39 q.push(p-1);40 dis[p-1]=dis[p]+1;41 vis[p-1]=1; 42 } 43 }44 45 }46 int main()47 {48 scanf("%d",&n);49 for(int i=1;i<=n;i++)50 {51 scanf("%d",&a[i]);52 }53 bfs();54 printf("%d",dis[n]);55 return 0;56 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.