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. seajs教程之seajs学习笔记 seajs.use用法

    seajs.use 用来在页面中加载模块.通过 use 方法,可以在页面中加载任意模块. 实例地址:http://www.android100.org/html/201405/23/12807.htm ...

  2. 【剑指offer】面试题34:丑数

    题目: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 思路: 第一 ...

  3. jquery插件-自定义select

        由于原生select在各个浏览器的样式不统一,特别是在IE67下直接不可以使用样式控制,当PM让你做一个样式的时候,那是相当的痛苦.最好的办法就是使用自定义样式仿select效果.这里写了一个 ...

  4. Activity的onSaveInstanceState()和onRestoreInstanceState()方法

    首先Android的Activity生命周期如下图: Activity的onSaveInstanceState()和onRestoreInstanceState()并不是生命周期方法,他们不同于onC ...

  5. Android --- 字符串\n的换行问题

    我的一段文字"测试文本\n测试文本\n测试文本\n测试文本\n测试文本\n测试文本\n". 该段文字放在一个txt文本内. 我用流读取后,显示在一个TextView里. 我期望的显 ...

  6. 《Java程序员面试笔试宝典》之Java与C/C++有什么异同

    Java与C++都是面向对象语言,都使用了面向对象思想(例如封装.继承.多态等),由于面向对象有许多非常好的特性(继承.组合等),使得二者都有很好的可重用性. 需要注意的是,二者并非完全一样,下面主要 ...

  7. NPOI之使用EXCEL模板创建报表

    因为项目中要用到服务器端创建EXCEL模板 无法直接调用EXCEL 查了下发现NPOI很方便很简单就实现了 其中走了点弯路 第一次弄的时候发现输出的值是文本不是数字型无法直接计算公式 然后又发现打开报 ...

  8. 智能路由——ESB

    SOA之我见 SOA已然是企业级开发的必定之路.有人会问:我们有了OOP,还须要SOA吗?好吧我承认,这个问题也困扰了我非常久.现现在我得出的结论是:OOP是OOP,SOA是SOA. OOP是指面向对 ...

  9. UISwitch + UIimage - 初识IOS

    这里解释一个小例子,希望对你有点帮助,利用UISwitch控制UIimage的动画效果 先定义一个数组,用来存放照片,现在定义数组有一个特别简单的方法: NSArray *image1 = @[]; ...

  10. php 二维码生成类

    <?php /** * BarcodeQR - Code QR Barcode Image Generator (PNG) * @package BarcodeQR * @category Ba ...