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 ;
}