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 (1≤M≤20) is the width of the area, N (1≤N≤20) is the length of the area, and K (1≤K≤10) 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.

解题报告

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

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <queue> using namespace std;
const int maxn = + ;
int c,r,k,p; char g[maxn][maxn];
int vis[maxn][maxn][ + ];
int dir[][] = {-,,,,,-,,};
int dig_cost[ + ];
int dig_weight[ + ];
int target,sx,sy,ans; typedef struct status
{
int x,y,cost,have,allcost;
status(const int &x,const int & y,const int &cost,const int &have,const int& allcost)
{
this->x = x,this->y = y,this->cost = cost,this->have = have,this->allcost = allcost;
}
}; queue<status>q; void bfs()
{
int flag = ;
while(!q.empty())
{
status s = q.front();q.pop();
int x = s.x,y = s.y ,cost = s.cost , have = s.have , allcost = s.allcost;
if (x == sx && y == sy && !flag)
{
if (have == target && allcost <= ans)
ans = allcost;
continue;
}
if (x == sx && y == sy && flag)
flag = ;
if (g[x][y] <= 'Z' && g[x][y] >= 'A')
{
int t = g[x][y] - 'A';
if ( !((have >> t)& ) )
{
int newhave = have;
newhave |= (<<t);
int newcost = cost + dig_weight[t];
int newallcost = allcost + dig_cost[t];
if (vis[x][y][newhave] == - || newallcost < vis[x][y][newhave])
if(newallcost <= p)
{
vis[x][y][newhave] = newallcost;
q.push(status(x,y,newcost,newhave,newallcost));
}
}
}
for(int i = ; i < ; ++ i)
{
int newx = x + dir[i][],newy = y + dir[i][] , newcost = cost,newhave = have,newallcost = allcost + newcost;
if (newx >= r || newx < || newy >= c || newy < || g[newx][newy] == '#')
continue;
if (vis[newx][newy][newhave] == - || newallcost < vis[newx][newy][newhave])
if(newallcost <= p)
{
vis[newx][newy][newhave] = newallcost;
q.push(status(newx,newy,newcost,newhave,newallcost));
}
}
}
} int main(int argc , char * argv[] )
{
int Case;
scanf("%d",&Case);
while(Case--)
{
ans = 0x7fffffff;
scanf("%d%d%d%d%*c",&r,&c,&k,&p);
memset(vis,-,sizeof(vis));
for(int i = ; i < r ; ++ i)
gets(g[i]);
while(!q.empty())
q.pop();
for(int i = ; i < r ; ++ i)
for(int j = ; j < c ; ++ j)
if (g[i][j] == '*')
{
q.push(status(i,j,,,));
sx = i,sy = j;
i = r ;
break;
}
for(int i = ; i < k ; ++ i)
scanf("%d%d",&dig_cost[i],&dig_weight[i]);
target = ;
for(int i = ; i < k ; ++ i)
target |= (<<i);
bfs();
if (ans == 0x7fffffff)
printf("Impossible\n");
else
printf("%d\n",ans);
}
return ;
}

UESTC_Sea Base Exploration CDOJ 409的更多相关文章

  1. An Exploration of ARM TrustZone Technology

    墙外通道:https://genode.org/documentation/articles/trustzone ARM TrustZone technology has been around fo ...

  2. 小白解决CENTOS7错误:Cannot find a valid baseurl for repo: base/7/x86_6

    刚入手的MacBook想着学点东西,本汪还是决定玩玩CentOS服务器,安装好了VirtualBox + CentOS. 打开一看,懵逼了!命令行! 行吧,先装个图形界面: $sudo yum gro ...

  3. 分布式系列文章——从ACID到CAP/BASE

    事务 事务的定义: 事务(Transaction)是由一系列对系统中数据进行访问与更新的操作所组成的一个程序执行逻辑单元(Unit),狭义上的事务特指数据库事务. 事务的作用: 当多个应用程序并发访问 ...

  4. base的应用

    ------------父类   public class Person   {       public Person(string name,int age)    {       this.Na ...

  5. C# base 64图片编码解码

    使用WinForm实现了图片base64编码解码的 效果图: 示例base 64编码字符串: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKD ...

  6. c++ builder 2010 错误 F1004 Internal compiler error at 0x9740d99 with base 0x9

    今天遇到一个奇怪的问题,拷贝项目后,在修改,会出现F1004 Internal compiler error at 0x9740d99 with base 0x9 ,不管怎么改,删除改动,都没用,关闭 ...

  7. MVC中的BASE.ONACTIONEXECUTING(FILTERCONTEXT) 的作用

    一句话,就是调用base.OnActionExecuting(filterContext)这个后,才会执行后续的ActionFilter,如果你确定只有一个,或是不想执行后续的话,那么可以不用调用该语 ...

  8. 安装CentOS7文字界面版后,无法联网,用yum安装软件提示 cannot find a valid baseurl for repo:base/7/x86_64 的解决方法

    *无法联网的明显表现会有: 1.yum install出现 Error: cannot find a valid baseurl or repo:base 2.ping host会提示unknown ...

  9. 在ASP.NET Core中使用Angular2,以及与Angular2的Token base身份认证

    注:下载本文提到的完整代码示例请访问:How to authorization Angular 2 app with asp.net core web api 在ASP.NET Core中使用Angu ...

随机推荐

  1. OpenStackCLI调试及术语识记

    1,Project are organizational units in the cloud,and are also known as tenants or accounts.Each user ...

  2. 7款纯CSS3实现的炫酷动画应用|慕课网只学有用的!

    关于我们 | 时尚廊 ♦ 时尚廊,中国大陆地区首家以"Lounge"为概念的艺文空间 ♦  7款纯CSS3实现的炫酷动画应用|慕课网只学有用的! 7款纯CSS3实现的炫酷动画应用

  3. poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)

    Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...

  4. STS 新建mvc工程--helloworld

    File--New--Spring Template Project 选择Spring MVC Project 填写项目名称和基础的包名 Finish之后就完成了. 把项目添加到服务器,然后运行.在浏 ...

  5. 百度之星B题(组合数)

    Problem B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  6. SpringMVC+easyUI CRUD 添加数据C

    接一篇文章,今天上午实现了添加数据.以下是Jsp.里面主要是看newUser()和saveUser().注意这函数里的url,newUser()里面去掉url属性.还要注意的一个问题 <div ...

  7. 奇妙的go语言(聊天室的开发)

    [ 声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 这是一篇关于聊天室开发的博客,原来文章的地址来自于此.这篇文章非常具有代表性,对于代码中的函数 ...

  8. Android编译过程详解(二)

    通过上篇文章,我们分析了编译android时source build/envsetup.sh和lunch命令,在执行完上述两个命令后, 我们就可以进行编译android了. 1. make  执行ma ...

  9. ios 获取屏幕的属性

    屏幕尺寸     CGRect screen = [UIscreen mainScreen].bounds 状态栏尺寸  CGRect rect = [[UIApplication sharedApp ...

  10. JavaScript当中的eval函数

    eval函数 eval函数接收一个由JavaScript语句组成的字符串,并且返回字符串中最后一条语句的返回值,如果最后一条语句没有返回值,那么eval函数返回undefined.如果传递给eval函 ...