2013
11-09

# It’s not a Bug, It’s a Feature!

It is a curious fact that consumers buying a new software product generally do not expect the software to be bug-free. Can you imagine buying a car whose steering wheel only turns to the right? Or a CD-player that plays only CDs with country music on them? Probably not. But for software systems it seems to be acceptable if they do not perform as they should do. In fact, many software companies have adopted the habit of sending out patches to fix bugs every few weeks after a new product is released (and even charging money for the patches).

Tinyware Inc. is one of those companies. After releasing a new word processing software this summer, they have been producing patches ever since. Only this weekend they have realized a big problem with the patches they released. While all patches fix some bugs, they often rely on other bugs to be present to be installed. This happens because to fix one bug, the patches exploit the special behavior of the program due to another bug.

More formally, the situation looks like this. Tinyware has found a total of n bugs B = {b1, b2, …, bn} in their software. And they have released m patches p1, p2, …, pm. To apply patch pi to the software, the bugs Bi+ in B have to be present in the software, and the bugs Bi- in B must be absent (of course Bi+ ∩ Bi- = Φ). The patch then fixes the bugs Fi- in B (if they have been present) and introduces the new bugs Fi+ in B (where, again, Fi+ ∩ Fi- = Φ).

Tinyware’s problem is a simple one. Given the original version of their software, which contains all the bugs in B, it is possible to apply a sequence of patches to the software which results in a bug- free version of the software? And if so, assuming that every patch takes a certain time to apply, how long does the fastest sequence take?

The input contains several product descriptions. Each description starts with a line containing two integers n and m, the number of bugs and patches, respectively. These values satisfy 1 <= n <= 20 and 1 <= m <= 100. This is followed by m lines describing the m patches in order. Each line contains an integer, the time in seconds it takes to apply the patch, and two strings of n characters each.

The first of these strings describes the bugs that have to be present or absent before the patch can be applied. The i-th position of that string is a “+” if bug bi has to be present, a “-” if bug bi has to be absent, and a “ 0” if it doesn’t matter whether the bug is present or not.

The second string describes which bugs are fixed and introduced by the patch. The i-th position of that string is a “+” if bug bi is introduced by the patch, a “-” if bug bi is removed by the patch (if it was present), and a “0” if bug bi is not affected by the patch (if it was present before, it still is, if it wasn’t, is still isn’t).

The input is terminated by a description starting with n = m = 0. This test case should not be processed.

For each product description first output the number of the product. Then output whether there is a sequence of patches that removes all bugs from a product that has all n bugs. Note that in such a sequence a patch may be used multiple times. If there is such a sequence, output the time taken by the fastest sequence in the format shown in the sample output. If there is no such sequence, output “Bugs cannot be fixed.”.

Print a blank line after each test case.

3 3
1 000 00-
1 00- 0-+
2 0-- -++
4 1
7 0-0+ ----
0 0

Product 1
Fastest sequence takes 8 seconds.

Product 2
Bugs cannot be fixed.


//* @author:
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.PriorityQueue;

class Node implements Comparable< Node> {
int t;
int sb, sp;
int eb, ep;
public int compareTo(Node arg0) {
return (t - arg0.t);
}
}

class QNode implements Comparable< QNode> {
int state;
int t;
public QNode() {}
public QNode(QNode tt) {
t = tt.t;
state = tt.state;
}
public int compareTo(QNode arg0) {
return (t - arg0.t);
}
}

public class Main {
int m, n;
int hash[] = new int[1 << 20];
Node bug[];

int bfs() {
PriorityQueue< QNode> q = new PriorityQueue< QNode>(1 << 10);
int ans = -1;
QNode cur, now;
cur = new QNode();
cur.t = 0;
cur.state = 0;
cur.state = (1<< n)-1;
hash[cur.state] = 0;
q.offer(cur);
while (!q.isEmpty()) {
cur = q.poll();
if (cur.state == 0) {
ans = cur.t;
break;
}
for (int i = 0; i < m; i++) {
if ((~cur.state & bug[i].sp) == 0
&& (cur.state & bug[i].sb) == 0) {
now = new QNode(cur);
now.state |= bug[i].ep;
now.state &= (~bug[i].eb);
now.t += bug[i].t;
if ((hash[now.state] == -1) || (hash[now.state] > now.t)) {
hash[now.state] = now.t;
q.offer(now);
}
}
}
}
while (!q.isEmpty())
q.poll();
return ans;
}

void solve() throws IOException {
int k = 0;
if (m + n == 0)
break;
bug = new Node[m];
for (int i = 0; i < m; i++) {
bug[i] = new Node();
for (int j = 0; j < n; j++) {
if (tmp.charAt(j) == '-')
bug[i].sb |= 1 << (n - j - 1);
if (tmp.charAt(j) == '+')
bug[i].sp |= 1 << (n - j - 1);
}
for (int j = 0; j < n; j++) {
if (tmp.charAt(j) == '-')
bug[i].eb |= 1 << (n - j - 1);
if (tmp.charAt(j) == '+')
bug[i].ep |= 1 << (n - j - 1);
}
//System.out.println(bug[i].sb+" "+bug[i].sp+" "+bug[i].eb + " "+bug[i].ep);
}
Arrays.sort(bug);
Arrays.fill(hash, -1);
int ans = bfs();
System.out.println("Product " + ++k);
if (ans == -1)
System.out.println("Bugs cannot be fixed.");
else
System.out.println("Fastest sequence takes " + ans + " seconds.");
System.out.println();
}
}

public static void main(String[] args) throws IOException {
Main test = new Main();
test.solve();
}
}