• Greedy Snake


    Have you ever played the game "Greedy Snake"? In the game, we control the movements of the snake to eat the fruits scattered in the game field, while the snake's body gets longer whenever it eats a fruit. The goal of the game is to make the snake eat as many fruits as possible, before its head has no where to go. It would be a real challenge to let you program this game, right now.

    greedysnake.jpg

    But hey! Relax! This time you are only asked to program a simplified version of the game. In this simple version, the fruits are all over the place except at some extra obstacle cells. The snake's body will extend all the way along the path while it eats the fruits. You may take any fruit cell as the starting position of the snake. Then you have four directions to choose from: UP, DOWN, LEFT or RIGHT. Once you pick up a direction for the snake, it must keep going until it hits an obstacle or its own body. This procedure repeats until the head of the snake has no where to go -- that is, every adjacent cell of its head is either an obstacle or its own body. Your task is to minimize the number of fruits left.

    For example, let's define the game field to be a 6 by 6 maze, surrounded by obstacle cells #, with one extra obstacle @S is the starting position of the snake. All the fruits are represented by dots.

    ######
    #..S@#
    #....#
    #....#
    #....#
    ######
    
     

    Then if you decide to move DOWN, RIGHT, and UP, the results are shown below.

    ######
    #..*@#
    #..*.#
    #..*.#
    #..S.#
    ######
    
     
    ######
    #..*@#
    #..*.#
    #..*.#
    #..*S#
    ######
    
     
    ######
    #..*@#
    #..*S#
    #..**#
    #..**#
    ######
    
     

    Now the snake is stucked, the game is over, and there are 8 fruits left. However, by carefully changing your moves or choosing another starting position, you can actually control the snake to eat up all the fruits. Try it out!

    Task 1: minimize the number of fruits left, and count the number of starting positions that can lead to the optimal solution.

    Task 2: To make things more interesting, you may replace one fruit cell by an obstacle, to obtain a better result, and count the number of ways to add an obstacle that leads to the better result. In case there is no way to improve the result by adding one obstacle, you should point out this situation.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains two integers N (4), the size of the game field, and K (0), the number of extra obstacles. Then K lines follow, each gives the coordinates of an obstacle in the format "xi​​ yi​​" where 2.

    Output Specification:

    For each case, print the results in two lines. In the first line print the minimal number of fruits left, and the number of different optimal starting positions. Then in the second line, either print − if there is no way to improve the result, or two integers: the better result and the number of optimal ways to add an obstacle.

    Sample Input 1:

    6 1
    2 5
    
     

    Sample Output 1:

    0 4
    -1
    
     

    Sample Input 2:

    6 2
    2 2
    3 3
    
     

    Sample Output 2:

    2 1
    0 2
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n,m,lp;
     4 vector<bool> v;
     5 void dfs(int p,int vp,int l)
     6 {
     7     if(vp&&v[p+vp])
     8     {
     9         v[p]=false;
    10         dfs(p+vp,vp,l-1);
    11         v[p]=true;
    12     }
    13     else
    14     {
    15         if(v[p-n])
    16         dfs(p,-n,l);
    17         if(v[p-1])
    18         dfs(p,-1,l);
    19         if(v[p+1])
    20         dfs(p,1,l);
    21         if(v[p+n])
    22         dfs(p,n,l);
    23         lp=min(lp,l);
    24     }
    25 }
    26 int main()
    27 {
    28 //    freopen("data.txt","r",stdin);
    29     scanf("%d %d",&n,&m);
    30     int lf=(n-2)*(n-2)-m;
    31     vector<int> vr(lf,0);
    32     v.resize(n*n,true);
    33     for(int i=0;i<n;i++)
    34     {
    35         v[i]=false;
    36         v[i*n]=false;
    37         v[i*n+n-1]=false;
    38         v[(n-1)*n+i]=false;
    39     }
    40     int x,y;
    41     for(int i=0;i<m;i++)
    42     {
    43         scanf("%d %d",&x,&y);
    44         x--;
    45         y--;
    46         v[x*n+y]=false;
    47     }
    48     for(int i=0;i<n*n;i++)
    49     {
    50         if(v[i])
    51         {
    52             lp=lf-1;
    53             dfs(i,0,lf-1);
    54             vr[lp]++;
    55         }
    56     }
    57     int bst=0;
    58     for(int i=0;i<lf;i++)
    59     {
    60         if(vr[i])
    61         {
    62             bst=i;
    63             printf("%d %d
    ",i,vr[i]);
    64             break;
    65         }
    66     }
    67     
    68     vector<int> vbr(bst,0);
    69     lf--;
    70     for(int i=0;i<n*n;i++)
    71     {
    72         if(v[i])
    73         {
    74             v[i]=false;
    75             lp=lf-1;
    76             for(int j=0;j<n*n;j++)
    77             if(v[j]) dfs(j,0,lf-1);
    78             if(lp<bst) vbr[lp]++;
    79             v[i]=true;
    80         }
    81     }
    82     bool bbst=true;
    83     for(int i=0;i<bst&&bbst;i++)
    84     {
    85         if(vbr[i])
    86         {
    87             printf("%d %d",i,vbr[i]);
    88             bbst=false;
    89         }
    90     }
    91     if(bbst) printf("-1");
    92     return 0;
    93 }
    诚者,君子之所守也。
  • 相关阅读:
    jar-下载站点
    java-原生爬虫机制源码
    eclipse-插件安装的三种方式
    ivy,ivyde插件-eclipse
    cygwin-介绍-安装
    cygwin-使用介绍
    linux-命令全集
    jquery-遍历each
    SSD固态硬盘的闪存芯片颗粒介绍
    java-测试synchronized使用xxx.class和this使用的区别
  • 原文地址:https://www.cnblogs.com/SkystarX/p/12285797.html
Copyright © 2020-2023  润新知