zoukankan      html  css  js  c++  java
  • UESTC_Sea Base Exploration CDOJ 409

    When the scientists explore the sea base, they use a kind of auto mobile robot, which has the mission to collect the swatches of resource of the sea base. The collections of the resource are taken to the research ship and classified, and then the research ship goes back to the mainland -- the research centre where scientists can do further research.

    The robots have equipments to collect and store the resource, but the equipments have limited capability. Only a small quantity of each kind of recourse is enough for scientific research. So, once the robot has collected one kind of resource, it needs not to collect more. The capability of the robot is fixed and the same as the number of kinds of resource the scientists have already known. So, the robot will collect a list of resource and come back with fruitful results. The resource is buried beneath the surface of the sea base, and the quantity is always enough for the robot to collect if the map indicates that there are some. If the robot doesn't want to collect the resource underneath its location, it can leave it ignored and pass the square freely.

    The robot needs a unit electric power to move from a square to another when its container is vacant and only can it move to a square adjacent to it. It needs Ai units to dig and collect the resource marked i. Each of the resource has its weight, so the robot costs Bi units of power per move after it has collected resource i.

    During the sea base walk, the robot carries a battery with a certain units(P) of electric power, and the power of it need to be economized, the scientists ask you to calculate the minimal quantity of power the robot will use to collect all kinds of resource and back to the ship.

    Input

    The first line of the input is an integer T which indicates the number of test cases.

    Each of the cases tells the map of the sea base you will explore, The first line will be the M,N,K,P,M (1M20) is the width of the area, N (1N20) is the length of the area, and K (1K10) is the number of kinds of resource, P is the certain capacity of the battery.

    Then follows M lines characters indicating the map, each line contains N characters.

    There are four kinds of characters, .*# and capital letters.

    The symbol . indicate that free space. The * indicates where the research ship located, notice that once the robot moves back to this area, it will be fetched back to the main ship automatically. You can assume there is only one * on one map.The # indicates that the space is blocked of some reason, the capital letters indicate K kinds of resource, and you can assume that there are always K kinds of capital letters (alphabetically from A).

    The next K lines follows two integers each line, Ai and Bi.

    Output

    For each input set output a number of the minimal quantity of power on one single line. Print a warning Impossible if the minimal quantity of power needed exceeds the capacity of the battery or it's impossible for the robot to accomplish the mission.

    解题报告

    二进制压下状态。。注意下细节,到基地就强制传送,其他就没啥了

      1 #include <iostream>
      2 #include <algorithm>
      3 #include <cstring>
      4 #include <queue>
      5 
      6 using namespace std;
      7 const int maxn = 20 + 4;
      8 int c,r,k,p;
      9 
     10 char g[maxn][maxn];
     11 int  vis[maxn][maxn][1024 + 10];
     12 int  dir[4][2] = {-1,0,1,0,0,-1,0,1};
     13 int  dig_cost[10 + 5];
     14 int  dig_weight[10 + 5];
     15 int  target,sx,sy,ans;
     16 
     17 
     18 typedef struct status
     19 {
     20 int x,y,cost,have,allcost;
     21 status(const int &x,const int & y,const int &cost,const int &have,const int& allcost)
     22  {
     23      this->x = x,this->y = y,this->cost = cost,this->have = have,this->allcost = allcost;
     24  }    
     25 };
     26 
     27 
     28 queue<status>q;
     29 
     30 
     31 
     32 void bfs()
     33 {
     34   int flag = 1;
     35   while(!q.empty())
     36    {
     37         status s = q.front();q.pop();
     38         int x = s.x,y = s.y ,cost = s.cost , have = s.have , allcost = s.allcost;
     39         if (x == sx && y == sy && !flag)
     40          {
     41               if (have == target && allcost <= ans)
     42                ans = allcost;
     43               continue;
     44       }
     45     if (x == sx && y == sy && flag)
     46      flag = 0;
     47     if (g[x][y] <= 'Z' && g[x][y] >= 'A')
     48          {
     49                     int t = g[x][y] - 'A';
     50                     if ( !((have >> t)&1 ) )
     51                      {
     52                          int newhave = have;
     53                          newhave |= (1<<t);
     54                          int newcost = cost + dig_weight[t];
     55                          int newallcost = allcost + dig_cost[t];
     56                          if (vis[x][y][newhave] == -1 || newallcost < vis[x][y][newhave])
     57                       if(newallcost <= p)
     58                   {
     59                           vis[x][y][newhave] = newallcost;
     60                           q.push(status(x,y,newcost,newhave,newallcost));
     61                   }
     62               }
     63       }
     64         for(int i = 0 ; i < 4 ; ++ i)
     65          {
     66               int newx = x + dir[i][0],newy = y + dir[i][1] , newcost = cost,newhave = have,newallcost = allcost + newcost;
     67               if (newx >= r || newx < 0 || newy >= c || newy < 0 || g[newx][newy] == '#')
     68                continue;
     69               if (vis[newx][newy][newhave] == -1 || newallcost < vis[newx][newy][newhave])
     70                if(newallcost <= p)
     71            { 
     72                    vis[newx][newy][newhave] = newallcost;
     73                    q.push(status(newx,newy,newcost,newhave,newallcost));
     74            }
     75       }
     76    }
     77 }
     78 
     79 
     80 int main(int argc , char * argv[] )
     81 {
     82  int Case;
     83  scanf("%d",&Case);
     84  while(Case--)
     85   {
     86       ans = 0x7fffffff;
     87       scanf("%d%d%d%d%*c",&r,&c,&k,&p);
     88       memset(vis,-1,sizeof(vis));
     89       for(int i = 0 ; i < r ; ++ i)
     90        gets(g[i]);
     91       while(!q.empty())
     92        q.pop();
     93       for(int i = 0 ; i < r ; ++ i)
     94        for(int j = 0 ; j < c ; ++ j)
     95         if (g[i][j] == '*')
     96          {
     97               q.push(status(i,j,1,0,0));
     98               sx = i,sy = j;
     99               i = r ;
    100               break;
    101        }
    102     for(int i = 0 ; i < k ; ++ i)
    103      scanf("%d%d",&dig_cost[i],&dig_weight[i]);
    104     target = 0;
    105     for(int i = 0 ; i < k ; ++ i)
    106      target |= (1<<i);
    107     bfs();
    108     if (ans == 0x7fffffff)
    109      printf("Impossible
    ");
    110     else
    111      printf("%d
    ",ans);
    112   }
    113  return 0;
    114 }
    No Pain , No Gain.
  • 相关阅读:
    Django的路由系统
    Django的View(视图)
    Django模板语言相关内容
    pip国内镜像
    TestNG 入门教程
    Spring MVC
    Git:代码冲突常见解决方法
    运行Maven项目时出现invalid LOC header (bad signature)错误,Tomcat不能正常启动
    annotation(@Retention@Target)详解
    JavaWeb:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
  • 原文地址:https://www.cnblogs.com/Xiper/p/4465682.html
Copyright © 2011-2022 走看看