codevs2292 图灵机游戏
来源:互联网 发布:传智播客2017java 编辑:程序博客网 时间:2024/06/07 13:27
#include<cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<algorithm>#include<string>#include<vector>#include<map>#include<set>#include<queue>#define R0(i,n) for(int i=0;i<n;++i)#define R1(i,n) for(int i=1;i<=n;++i)#define CLR(x,c) memset(x,c,sizeof x)using namespace std;int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}int a[100001],ci[100001],n;bool use[100001];void bfs(){queue<int>q; int i; q.push(a[1]); ci[a[1]]=1; use[a[1]]=true; while (!q.empty()){ int t=q.front();q.pop(); if (t==n) break; if (!use[a[t]]){ q.push(a[t]); ci[a[t]]=ci[t]+1; use[a[t]]=1; } if (t<n&&!use[t+1]){ q.push(t+1); ci[t+1]=ci[t]+1; use[t+1]=1; } if (t>1&&!use[t-1]){ q.push(t-1); ci[t-1]=ci[t]+1; use[t-1]=1; } }}int main(){ n=read(); R1(i,n)a[i]=read(); bfs(); if (n==1) printf("0\n"); else printf("%d\n",ci[n]);}
0 0
- codevs2292 图灵机游戏
- 【codevs2292】图灵机游戏
- 【codevs】2292图灵机游戏
- 【codevs 2292】图灵机游戏
- 图灵机
- 图灵机
- 图灵机
- 图灵机
- 图灵机
- 图灵机
- codevs 2292 图灵机游戏(BFS)
- Codevs 2292 图灵机游戏 解题报告
- CODE[VS] 2292 图灵机游戏 SPFA求最短路
- 什么是图灵机
- 什么是图灵机
- 图灵机模型
- 逻辑运算图灵机
- 图灵机简介
- HDU 3038 How Many Answers Are Wrong(带权并查集)
- SDWebImage使用——一个可管理远程图片加载的类库
- RabbitMQ官网教程---工作队列
- 全新的PRT技术
- Ansible学习之基础(1)
- codevs2292 图灵机游戏
- 工业企业数据库处理代码完整版本——2.匹配样本
- java基础---Stream
- [leetcode] Level Order Traversal
- iOS开发- 以图搜图功能实现 (源码+解析)
- Factorial Trailing Zeroes
- SpinnerAdapter接口
- C语言基础:C语言结构体(7) - 结构体的嵌套使用
- C语言学习笔记1