mcpc2017 Honey Heist (模拟)

 

5092: Honey Heist

时间限制: 1 Sec  内存限制: 128 MB
提交: 21  解决: 9
[提交][状态][讨论版][命题人:admin]

题目描述

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 find 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 first line contains five 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 

[提交][状态]

 

 

 

【题意】有一个六边形蜂巢,蜂巢的边长为r,一共有r^3-(r-1)^3个格子,格子序号从1开始,格子中有两个点a,b分别为起点和终点,一个蚂蚁位于a点,求从a点到b点的最短路径,从一个格子到边相邻的格子的距离为1,并且蜂巢中存在x个格子没办法通过。如果求出的最短路径小于等于n,则输出该最短路径,否则输出No。

【思路】首先利用vector<>把蜂巢存储起来,然后按照边相邻得可以到达建无向图,跑一遍bfs求出a和b之间的最短路径即可。

【代码如下】

 

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;

int r,n,a,b,x,vis[3100];
vector<int>vct[1200];
int head[3100],cnt,ok[3100];
struct Edge{
    int v,next,w;
}edge[210000];
struct node{
    int x,steps;
};

void add(int u, int v, int w){
    edge[cnt].v = v;
    edge[cnt].next = head[u];
    edge[cnt].w = w;
    head[u] = cnt ++;
}

int main(){
    memset(vis,0,sizeof(vis));
    memset(head,-1,sizeof(head));
    cnt = 0;
    scanf("%d%d%d%d%d",&r,&n,&a,&b,&x);
    int d,num=1;
    for(int i = 0; i < x; i ++){
        scanf("%d",&d);
        vis[d] = 1;
    }
    d=r;
    for(int i = 1; i <= r; i ++){
        for(int j = 0; j < d; j ++) vct[i].push_back(num),num ++;
        d ++;
    }
    d = 2*r-2;
    for(int i = r+1; i <= 2*r-1; i ++){
        for(int j = 0; j < d; j ++) vct[i].push_back(num), num ++;
        d --;
    }

    /*for(int i = 1; i < 2*r; i ++){
        printf("%d:",i);
        for(int j = 0; j < vct[i].size(); j ++) printf("%d ",vct[i][j]);
        puts("");
    }*/

    int x,y;
    for(int i = 1; i < r; i ++){//up
        for(int j = 0; j < vct[i].size(); j ++){
            x = i-1; y = j;
            if(x>0&&y<vct[x].size()) add(vct[i][j],vct[x][y],1);
            y=j-1;
            if(x>0&&y>=0) add(vct[i][j],vct[x][y],1);

            x = i; y = j-1;
            if(y>=0) add(vct[i][j],vct[x][y],1);
            y = j+1;
            if(y<vct[x].size()) add(vct[i][j],vct[x][y],1);

            x = i+1; y = j;
            add(vct[i][j],vct[x][y],1);
            y = j+1;
            add(vct[i][j],vct[x][y],1);
        }
    }

    for(int j = 0; j < vct[r].size(); j ++){//mid
        x = r-1; y = j;
        if(y<vct[x].size())add(vct[r][j],vct[x][y],1);
        y = j+1;
        if(y<vct[x].size())add(vct[r][j],vct[x][y],1);

        y = j-1;
        if(y>=0) add(vct[r][j],vct[r][y],1);
        y = j+1;
        if(y<vct[r].size()) add(vct[r][j],vct[r][y],1);

        x = r+1; y = j-1;
        if(y>=0) add(vct[r][j],vct[x][y],1);
        y = j;
        if(y<vct[x].size())add(vct[r][j],vct[x][y],1);
    }

    for(int i = r+1; i <= 2*r-1; i ++){//down
        for(int j = 0; j < vct[i].size(); j ++){
            x = i-1; y = j;
            add(vct[i][j],vct[x][y],1);
            y = j+1;
            add(vct[i][j],vct[x][y],1);

            x = i; y = j-1;
            if(y>=0) add(vct[i][j],vct[x][y],1);
            y = j+1;
            if(y<vct[x].size()) add(vct[i][j],vct[x][y],1);

            x = i+1; y = j;
            if(x<2*r&&y<vct[x].size()) add(vct[i][j],vct[x][y],1);
            y = j-1;
            if(x<2*r&&y>=0) add(vct[i][j],vct[x][y],1);
        }
    }

    /*for(int i = 1; i <= r*r*r-(r-1)*(r-1)*(r-1); i ++){
        for(int j = head[i]; ~j; j = edge[j].next){
            printf("%d   %d\n",i,edge[j].v);
        }
    }*/
    memset(ok,0,sizeof(ok));
    queue<node>q;
    node no; no.x = a,no.steps=0; ok[a] = 1;
    q.push(no);
    int ans = inf;
    while(!q.empty()){
        no = q.front(); q.pop();
        if(no.steps>n) break;
        if(no.x == b){
            ans = min(ans,no.steps);
            break;
        }
        for(int i = head[no.x]; ~i; i = edge[i].next){
            int v = edge[i].v;
            if(!vis[v] && !ok[v]){
                node nod; nod.x = v, nod.steps = no.steps+1;
                q.push(nod);
                ok[v] = 1;
            }
        }
    }
    if(ans<=n) printf("%d\n",ans);
    else printf("No");
    return 0;
}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值