poj 2908 Quantum 广搜

Description

At the Institution for Bits and Bytes at University of Ramville, Prof. Jeremy Longword and his eight graduate students are investigating a brand new way of storing and manipulating data on magnetic disks for use in hard drives. The method is based on letting quasimagnetic quantum operations operate on the sectors on the disk, and is, of course, safer andmore reliable than any earlier invented storage method. The use of each quantum operation costs a certain amount of energy, and the more energy the storage unit consumes, the warmer it will get. Therefore, you and your research team, are assigned the task of writing a program that, given sets of possible quantum operations and their costs, can calculate the lowest possible total cost for transforming a set of data to the wanted result.

On the disk, binary words of length 1 ≤ L ≤ 20 are treated. The quantum operations are defined by strings of the same length as the binary words, and are built from the four letters N (does nothing), F (inverts one bit), S (sets a bit to 1), and C (resets a bit to 0). Each letter in the string corresponds to an operation on the bit in the binary word at the same position. The binary words are transformed one by one and the total energy cost for the transformation is calculated as the sum of the costs for the performed quantum operations.

Input

The input starts with a single positive integer N ≤ 20 on a row, deciding the number of test cases that will follow. Then, for each of the test cases:

  • One line containing three integers: Lnop and nw separated by one space.
  • L indicates the length of the binary words and the quantum operations.
  • nop (≤ 32) is the number of quantum operations that are available for use when transforming the binary words.
  • nw (≤ 20) is the number of binary words that are to be transformed in the current test case.

After this, nop rows follows, each of them containing the definition of a quantum operation followed by the energy cost 0 ≤ ci 1000 of carrying out the quantum operation. The definition and the cost are separated by a single space.

Finally, there are nw rows, each containing two binary words separated by a single space. The first of these words should, when possible, be transformed to the second using the quantum operations. The binary words are expressed as sequences of 1’ s and 0’s. After these rows, the next test case follows, if there is any.

Output

Each test case should produce a row containing a list of the energy costs of transforming each of the binary words. The costs should be separated by a single space and presented in the same order as the corresponding input. When there is no successful way of transforming a binary word, “NP”, meaningnot possible should be printed instead.

Sample Input

2
4 3 3
NFFN 1
NFNF 2
NNFN 4
0010 0100
0001 0010
0100 1000
4 4 5
CFSF 4
NNSS 3
FFFF 5
FNFN 6
1111 0000
1001 0110
0101 1000
1000 0011
0000 1001

Sample Output

1 3 NP

5 4 8 9 9

题意:

2   几组数据
如第一组数据
4每组数据的长度 3三种变换方式 3每组数据中有几组数据
NFFN 变换方式 1消耗的热量
NFNF        2
NNFN        4
0010原始数据 0100变换后的数据
0001       0010
0100       1000
将数据按要求变换,‘s’置1,‘C‘置0,'F'反转,‘N’不变化,求最小消耗的热量
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int q[50],n,m,t,v[(1<<20)+10]; //v数组用来标记是否已经出现过,尤其要注意它的长度
char a[50][50],s1[50],s2[50];
int e1,e2,flag,tt;
struct p
{
    char x[50];
    int y;
    int k; 
    friend bool operator<(p n1,p n2)
    {
        return n1.y>n2.y;  //热量小的在前
    }
};
int fun(char z[])  //可以把数据看做二进制,转化为十进制,便于标记
{
    int e=0,f=1;
    if(z[t-1]=='1')
        e+=1;
    for(int i=t-2; i>=0; i--)
    {
        f<<=1;
        if(z[i]=='1')
            e+=f;
    }
    return e;
}
void bfs()
{
    p now,tmp;
    priority_queue<p>Q;
    for(int i=0; i<t; i++)
        now.x[i]=s1[i];
    now.y=0;
    now.k=e1;
    Q.push(now);
    while(!Q.empty())
    {
        now=Q.top();
        Q.pop();
        if(now.k==e2)
        {
            flag=1;
            if(tt==0)
            {
                printf("%d",now.y);
                tt=1;
            }
            else
                printf(" %d",now.y);
            return ;
        }
        if(v[now.k]==1)
            continue;
        v[now.k]=1;  //标记的位置和以往有些不同
        for(int i=0; i<n; i++)
        {
            tmp=now;
            for(int j=0; j<t; j++)
            {
                if(a[i][j]=='S')
                    tmp.x[j]='1';
                else if(a[i][j]=='C')
                    tmp.x[j]='0';
                else if(a[i][j]=='F')
                    tmp.x[j]=(tmp.x[j]-'0')^1+'0';
            }
            tmp.k=fun(tmp.x);
            tmp.y+=q[i];
            Q.push(tmp);
        }
    }
    return ;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(a,0,sizeof(a));
        memset(q,0,sizeof(q));
        tt=0;
        scanf("%d%d%d",&t,&n,&m);
        for(int i=0; i<n; i++)
            scanf("%s%d",a[i],&q[i]);
        for(int i=0; i<m; i++)
        {
            memset(v,0,sizeof(v));
            memset(s1,0,sizeof(s1));
            memset(s2,0,sizeof(s2));
            flag=0;
            scanf("%s%s",s1,s2);
            e1=fun(s1);
            e2=fun(s2);
            bfs();
            if(flag==0)
            {
                if(tt==0)
                {
                    printf("NP");
                    tt=1;
                }
                else
                    printf(" NP");
            }
        }
        printf("\n");
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值