CodeChef Chef and Digit Jumps 题解

原题链接:Chef and Digit Jumps
题意:原题中有链接。
题解:一道很明显的bfs题,就是跳就可以了,当然,跳的时候可以加一些优化,具体看代码

#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
#define Maxn 100000
char s[Maxn+5];
int n;
int a[Maxn+5];
queue<int> q;
vector<int> x[10];
bool in[Maxn+5];
bool vis[10];
void bfs(){
    q.push(1);
    a[1]=0;
    in[1]=1;
    int now;
    while(!q.empty()){
        now=q.front();
        if(now==n){
            break;
        }
        in[now]=0;
        q.pop();
        if(now-1>0&&a[now-1]>a[now]+1){
            a[now-1]=a[now]+1;
            if(!in[now-1]){
                q.push(now-1);
                in[now-1]=1;
            }
        }
        if(now+1<=n&&a[now+1]>a[now]+1){
            a[now+1]=a[now]+1;
            if(!in[now+1]){
                q.push(now+1);
                in[now+1]=1;
            }
        }
        if(vis[s[now]-'0']){
            continue;
        }//每一个数只要跳一次即可,后面来的肯定不会更优
        vis[s[now]-'0']=1;
        for(int i=0;i<(int)x[s[now]-'0'].size();i++){
            if(a[x[s[now]-'0'][i]]>a[now]+1){
                a[x[s[now]-'0'][i]]=a[now]+1;
                if(!in[x[s[now]-'0'][i]]){
                    q.push(x[s[now]-'0'][i]);
                    in[x[s[now]-'0'][i]]=1;
                }
            }
        }
    }
}
int main(){
    memset(a,0x3f,sizeof a);
    scanf("%s",s+1);
    while(s[++n]!='\0'){
        x[s[n]-'0'].push_back(n);
    }
    n--;
    bfs();
    printf("%d\n",a[n]);
    return 0;
}

原文地址:https://www.cnblogs.com/withhope/p/11141279.html

时间: 2024-10-10 07:16:44

CodeChef Chef and Digit Jumps 题解的相关文章

codechef Chef and The Right Triangles 题解

Chef and The Right Triangles The Chef is given a list of N triangles. Each triangle is identfied by the coordinates of its three corners in the 2-D cartesian plane. His job is to figure out how many of the given triangles are right triangles. A right

codechef Correctness of Knight Move题解

Chef develops his own computer program for playing chess. He is at the very beginning. At first he needs to write the module that will receive moves written by the players and analyze it. The module will receive a string and it should report at first

codechef Holes in the text 题解

Chef wrote some text on a piece of paper and now he wants to know how many holes are in the text. What is a hole? If you think of the paper as the plane and a letter as a curve on the plane, then each letter divides the plane into regions. For exampl

Codechef July Challenge 2014部分题解

Dish Owner(并查集) 链接:http://www.codechef.com/JULY14/problems/DISHOWN/ 题意分析:本题主要操作就是给出0 x y时,拥有第x道菜的厨师与拥有第y道菜的厨师pk,谁拥有的所有菜的其中一道菜(不一定是x或y)的分值比较高谁就获胜,并赢得loser的所有菜.即比较的是每个人分值最高的菜,所以对于非loser的人来说,他的分值最高的菜是不变的.综合题意用并查集易解. #include <cstdio> const int Maxn=100

codechef Row and Column Operations 题解

You are given an N × N grid initially filled by zeros. Let the rows and columns of the grid be numbered from1 to N, inclusive. There are two types of operations can be applied to the grid: RowAdd R X: all numbers in the row R should be increased by X

codechef Sums in a Triangle题解

Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear in the second line, three in the third line, etc. Develop a program which will compute the largest of the sums of numbers that appear on the paths st

codechef Little Elephant and Permutations题解

The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numbers 1, 2, ...,N. He calls a permutation A good, if the number of its inversions is equal to the number of its local inversions. The number of inversio

codechef Johnny and the Beanstalk 题解

One evening Johnny found some funny looking beens in his grandfather's garden shed, and decided to plant one of them. Next morning, to his surprise he found an enormous beanstalk growing in his back yard. Undaunted by its size, he decided to count it

codechef The Ball And Cups题解

The Ball And Cups At the end of a busy day, The Chef and his assistants play a game together. The game is not just for fun but also used to decide who will have to clean the kitchen. The Chef is a Game Master, so his concern is how to manage the game