Honey Heist

5092: Honey Heist

时间限制: 1 Sec  内存限制: 128 MB

题目描述

0x67 is a scout ant searching for food and discovers a beehive nearby. As it approaches the honeycomb,0x67 can sense an area inside packed with dried honey that can be easily carried back to the nest and stored for winter. However, it must burrow through the honeycomb to reach the cell containing the sweet loot. If 0x67 can create a passage to the honey to help the other ants ?nd it, it will do so before returning to the nest. The cells of the honeycomb are numbered in row major order, so cell IDs can be assigned as shown below:

When 0x67 discovers the opening to the honeycomb, it enters the cell. Some ants are stronger than others, depending on their age, so 0x67 can only chew through at most N cells before its jaw wears out and must return to the nest to recuperate. The honeycomb is hexagonal, and each edge length is R cells. 0x67 enters through a hole at location A and must get to the honey at location B by chewing a path through no more than N adjacent cells. Because ants can be competitive, 0x67 wants to reach the honey by chewing through the fewest possible cells. 0x67 can also sense some of the cells are hardened with wax and impossible to penetrate, so it will have to chew around those to reach the cell at location B.

Scout ants have rudimentary computational skills, and before 0x67 begins to chew, it will work out where it needs to go, and compute K, the least number of cells it needs to chew through to get from A to B, where B is the Kth cell. If K > N, 0x67 will not be strong enough to make the tunnel. When 0x67 returns to the nest, it will communicate to its nestmates how many cells it chewed through to get to B, or will report that it could not get to the honey.

输入

The input contains two lines. The ?rst line contains ?ve blank separated integers: R N A B X
R: the length (number of cells) of each edge of the grid, where 2 ≤ R ≤ 20. The total number of cells in the grid can be determined by taking a difference of cubes, R3 ? (R ? 1)3.

N: the maximum number of cells 0x67 can chew through, where 1 ≤ N < R3 ? (R ? 1)3.
A: the starting cell ID, This cell is located on one of the grid edges: The cell has fewer than six neighbors.
B: the cell ID of the cell containing the honey, where 1 ≤ B ≤ R3 ? (R ? 1)3.
X: the number of wax-hardened cells, where 0 ≤ X < (R3 ? (R ? 1)3) ? 1.
The second line contains X integers separated by spaces, where each integer is the ID of a wax-hardened cell.
The ID’s, A, B, and all the ID’s on the second line, are distinct positive integers less than or equal to R3 ? (R ? 1)3.

输出

A single integer K if 0x67 reached the honey at cell B, where B is the Kth cell, otherwise the string No if it was impossible to reach the honey by chewing through N cells or less.

样例输入

6 6 1 45 11
15 16 17 19 26 27 52 53 58 65 74

样例输出

6

来源

mcpc2017



这题的题目意思就是找一个从A到B的最短路,其中有一些点不能走,问最短路径长度是否大于N

这一题的图和一般的搜索的图不太一样,它是一个六边形的图,但是我们仍然可以用坐标x,y来表示每一个点

其中x为第几行,y为这一行的第几个格子

于是这个题目就变成一个简单的广搜了

要注意的一点是在六边形的上半部分和下半部分x,y转移的状态是不一样的

#include<cstdio>
#include<iostream>
#include<cstring>
#define N 100000

using namespace std;
int r,n,a,b,x;
int x1,y1,x2,y2;
int check[N]= {0};
int bound[205];  //bound[i]为第i行共有多少个格子

typedef struct
{
    int x,y,step;
} ss;

int pd(int x,int y)   //用来检测x,y点是否合法
{
    if(x<1||x>2*r-1||y<1||y>bound[x])return 0;
    return 1;
}

int f(int x,int y)   // 婷姐推的公式,用来计算第x行的第y个数的序号是多少
{
    if(x<=r)return r*(x-1)+(x-1)*(x-2)/2+y;
    return (3*r-1)*r/2+(5*r-2-x)*(x-1-r)/2+y;
}

int bfs()
{
    if(x1==x2&&y1==y2)return 0;
    ss team[N];
    int c1=0,c2=1;

    team[0].x=x1;
    team[0].y=y1;
    team[0].step=0;
    check[f(x1,y1)]=1;

    while(c1<c2)    //这里就对六边形的上半部分和下半部分做了不同的搜索策略
    {
        ss now=team[c1];
        c1++;

// printf("%d %d %d\n",now.x,now.y,f(now.x,now.y));

        if(now.x<=r&&(now.x-1,now.y-1)&&check[f(now.x-1,now.y-1)]==0)
        {
            team[c2].x=now.x-1;
            team[c2].y=now.y-1;
            team[c2].step=now.step+1;
            check[f(team[c2].x,team[c2].y)]=1;
            if(team[c2].x==x2&&team[c2].y==y2)return team[c2].step;
            c2++;
        }

        if(now.x<=r&&pd(now.x-1,now.y)&&check[f(now.x-1,now.y)]==0)
        {
            team[c2].x=now.x-1;
            team[c2].y=now.y;
            team[c2].step=now.step+1;
            check[f(team[c2].x,team[c2].y)]=1;
            if(team[c2].x==x2&&team[c2].y==y2)return team[c2].step;
            c2++;
        }

        if(now.x>r&&(now.x-1,now.y+1)&&check[f(now.x-1,now.y+1)]==0)
        {
            team[c2].x=now.x-1;
            team[c2].y=now.y+1;
            team[c2].step=now.step+1;
            check[f(team[c2].x,team[c2].y)]=1;
            if(team[c2].x==x2&&team[c2].y==y2)return team[c2].step;
            c2++;
        }

        if(now.x>r&&pd(now.x-1,now.y)&&check[f(now.x-1,now.y)]==0)
        {
            team[c2].x=now.x-1;
            team[c2].y=now.y;
            team[c2].step=now.step+1;
            check[f(team[c2].x,team[c2].y)]=1;
            if(team[c2].x==x2&&team[c2].y==y2)return team[c2].step;
            c2++;
        }

        if(pd(now.x,now.y-1)&&check[f(now.x,now.y-1)]==0)
        {
            team[c2].x=now.x;
            team[c2].y=now.y-1;
            team[c2].step=now.step+1;
            check[f(team[c2].x,team[c2].y)]=1;
            if(team[c2].x==x2&&team[c2].y==y2)return team[c2].step;
            c2++;
        }

        if(pd(now.x,now.y+1)&&check[f(now.x,now.y+1)]==0)
        {
            team[c2].x=now.x;
            team[c2].y=now.y+1;
            team[c2].step=now.step+1;
            check[f(team[c2].x,team[c2].y)]=1;
            if(team[c2].x==x2&&team[c2].y==y2)return team[c2].step;
            c2++;
        }

        if(now.x<r&&pd(now.x+1,now.y)&&check[f(now.x+1,now.y)]==0)
        {
            team[c2].x=now.x+1;
            team[c2].y=now.y;
            team[c2].step=now.step+1;
            check[f(team[c2].x,team[c2].y)]=1;
            if(team[c2].x==x2&&team[c2].y==y2)return team[c2].step;
            c2++;
        }

        if(now.x<r&&pd(now.x+1,now.y+1)&&check[f(now.x+1,now.y+1)]==0)
        {
            team[c2].x=now.x+1;
            team[c2].y=now.y+1;
            team[c2].step=now.step+1;
            check[f(team[c2].x,team[c2].y)]=1;
            if(team[c2].x==x2&&team[c2].y==y2)return team[c2].step;
            c2++;
        }

        if(now.x>=r&&pd(now.x+1,now.y)&&check[f(now.x+1,now.y)]==0)
        {
            team[c2].x=now.x+1;
            team[c2].y=now.y;
            team[c2].step=now.step+1;
            check[f(team[c2].x,team[c2].y)]=1;
            if(team[c2].x==x2&&team[c2].y==y2)return team[c2].step;
            c2++;
        }

        if(now.x>=r&&pd(now.x+1,now.y-1)&&check[f(now.x+1,now.y-1)]==0)
        {
            team[c2].x=now.x+1;
            team[c2].y=now.y-1;
            team[c2].step=now.step+1;
            check[f(team[c2].x,team[c2].y)]=1;
            if(team[c2].x==x2&&team[c2].y==y2)return team[c2].step;
            c2++;
        }

    }

    return -1;

}

int main()
{

    scanf("%d %d %d %d %d",&r,&n,&a,&b,&x);

    for(int i=0; i<x; i++)
    {
        int aa;
        scanf("%d",&aa);
        check[aa]=1;
    }

    for(int i=1; i<=r; i++)
    {
        bound[i]=i+r-1;
        for(int j=1; j<=i+r-1; j++)
        {
            if(f(i,j)==a)
            {
                x1=i;
                y1=j;
            }
            else if(f(i,j)==b)
            {
                x2=i;
                y2=j;
            }
        }
    }

    for(int i=r+1; i<=2*r-1; i++)
    {
        bound[i]=r+r-2+r+1-i;
        for(int j=1; j<=r+r-2+r+1-i; j++)
        {
            if(f(i,j)==a)
            {
                x1=i;
                y1=j;
            }
            else if(f(i,j)==b)
            {
                x2=i;
                y2=j;
            }
        }
    }

    int ans=bfs();

    if(ans==-1||ans>n)printf("No");
    else
        printf("%d",ans);

    return 0;

}

原文地址:https://www.cnblogs.com/tian-luo/p/8904565.html

时间: 2024-10-10 17:44:46

Honey Heist的相关文章

Sweet as honey!

原文 In 1963 a Lancaster bomber crashed on Wallis island, a remote place in the south Pacific, a long way west of Samoa. The plane wasn't too badly damaged, but over the years, the crash was forgotten and the wreck remained undisturbed. Then in 1989, t

武汉科技大学ACM:1005: Soapbear and Honey

Problem Description Soapbear is the mascot of WHUACM team. Like other bears, Soapbear loves honey very much, so he decides to get some honeycombs for honey. There are N honeycombs hanging on some trees, numbered from 1 to N, and the height of honeyco

Jewel heist

题意:珠宝大盗Arsen Lupin偷珠宝.在展厅内,每颗珠宝有个一个坐标为(xi,yi)和颜色ci. Arsen Lupin发明了一种设备,可以抓取平行x轴的一条线段下的所有珠宝而不触发警报, 唯一的限制是抓取的珠宝不能不能有所有的m种颜色.询问能抓取的最大珠宝数量. 分析:要决策的东西有两个,一是这条线段的y坐标,二是线段的x的范围. 枚举线段的y坐标,线段宽度要保证下方不能有所有的颜色,这需要知道颜色的关于x的坐标信息, 为了x的坐标信息的重复利用,从小到大枚举y. 对于一个固定的yi,怎

VMware Coding Challenge: The Heist

类似BackpackII问题 1 static int maximize_loot(int[] gold, int[] silver) { 2 int[][] res = new int[gold.length+silver.length+1][10001]; 3 res[0][0] = 0; 4 for (int i=1; i<=gold.length; i++) { 5 for (int j=0; j<=10000; j++) { 6 res[i][j] = Math.max(res[i-

u3d honey hex framework 代码解读记录(二)

// file: WorldOven.cs /// <summary> /// If baking were requested this function will handle starting all required processes and default settings required for initial pass /// </summary> /// <returns></returns> void Update() { // 如果有

Genome Sequencing ofMuseumSpecimens Reveals Rapid Changes in the Genetic Composition of Honey Bees in California

Abstract 在自然生态系统和管理生态系统中,Apis mellifera都是极具影响力的传粉者.在北美,这一物种已经从欧洲和非洲的各种不同来源的种群中被多次引入.从那时起,野生动物种群已经扩展到许多不同的环境,跨越了它们广泛的引进范围.在这里,我们使用了历史博物馆标本和来自美国加利福尼亚州的新收集的现代种群进行了全基因组测序,来分析群体统计学和选择对过去105年的时间里对引进种群的影响.我们发现,北加州和南加州的群体都表现出明显的基因变化,但变化的方式不同.自20世纪60年代以来,在北部种

根据76大细分词性对单词进行归组(二)

词性的重要性不言而喻,尤其是对于自然语言处理来说,哪怕就是记单词,根据词性对单词进行归组也是非常有帮助的. superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 各大词性及其包括的词: 32.N-COUNT-COLL(可数集合名词) (词数:50) 1 aristocracy army array audience band 2 cast chapter command commission committee 3 co

为何大量网站不能抓取?爬虫突破封禁的6种常见方法

在互联网上进行自动数据采集(抓取)这件事和互联网存在的时间差不多一样长.今天大众好像更倾向于用“网络数据采集”,有时会把网络数据采集程序称为网络机器人(bots).最常用的方法是写一个自动化程序向网络服务器请求数据(通常是用 HTML 表单或其他网页文件),然后对数据进行解析,提取需要的信息. 本文假定读者已经了解如何用代码来抓取一个远程的 URL,并具备表单如何提交及 JavaScript 在浏览器如何运行的机制.想更多了解网络数据采集基础知识,可以参考文后的资料. 在采集网站的时会遇到一些比

Python学习笔记四:for循环

一个非常简单的示例: 1 for i in range(0, 10, 2): #0到10为循环范围,2为循环步长 2 print("Count: ", i) 使用for循环优化前面的例子: age_konishi = 23 for i in range(3): #默认步长为1 guess_age = int(input("Age: ")) if guess_age == age_konishi: print("Yoshi! Sugoi!") br