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. vijos1777 引水入城

    描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N行M列的矩形,其中每个格子都代表一座城市,每座城市都有一个海拔高度. 为了使居民们都尽可能 ...

  2. 【HDU1754】I Hate It(线段树)

    update:单点替换 query:区间最值 #include <iostream> #include <cstring> #include <cstdlib> # ...

  3. UVa10653.Prince and Princess

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. php 的设计模式

    1.单例模式 单例模式顾名思义,就是只有一个实例.作为对象的创建模式, 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式的要点有三个: 一是某个类只能有一个实例: ...

  5. Promise 异步执行的同步操作

    Promise 是用来执行异步操作的. 但有时一个异步操作需要等其他的异步操作完成,这时候就可以使用then来做. function loadImageAsync(url) { return new ...

  6. 在eclipse中使用svn

    作为一名程序员,svn是比较常用也必然会使用到的一个工具,它的全拼为Subversion,是一个开源的版本控制系统,可以对每次修改的文件和目录进行准确记录,以便在使用的时候及时提取.本文主要介绍如何在 ...

  7. [视频] x264 压缩笔记

    转载本站文章请注明,转载自:扶凯[http://www.php-oa.com] 本文链接: http://www.php-oa.com/2009/03/22/x264.html 象x264本身是不能直 ...

  8. Ant命令行操作

    Ant命令行操作 Ant构建文件可以将项目编译,打包,測试,它是Apache软件基金会jakarta文件夹中的一个子项目,具有跨平台性,操作简单,并且非常easy上手. 关于Ant执行,能够在项目中找 ...

  9. C#/.Net Post获取数据流的一种简单写法

    最近在弄一些第三方的平台,经常调用第三方的接口实现某些特定的功能 在实现的同时基本上都需要本地的数据经过服务器在Request到第三方的服务器中处理,再返回相应的数据结构体:json/xml 以下是我 ...

  10. CSU 1808 地铁

    题意: ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟 ...