Denis, who managed to buy flowers and sweets (you will learn about this story in the next task), went to a meeting with Nastya to invite her to become a couple. And so, they sit together in a cafe, chatting. Denis finally offers to be them together, but … Nastya doesn’t give any answer.
The poor boy was very upset due to that. He was so sad that he kicked some kind of scoreboard with numbers. The numbers are displayed in the same way as on an electronic clock: each digit position consists of 7 segments, which can be turned on or off to display different numbers. The picture shows how all 10 decimal digits are displayed:
After the kick, some sticks stopped working, that is, some sticks might stop glowing if they glowed earlier. But Denis remembered how many sticks were glowing and how many are glowing now. Let exactly k sticks break and we know which sticks are working now. Denis came up with the question: what is the maximum number that can burn on the board if you turn on exactly k sticks (which are off now)?
It is allowed that the number includes leading zeros.
Input
The first line contains integer n (1≤n≤2000) — the number of digits on scoreboard and k (0≤k≤2000) — the number of sticks that stopped working.
The next n lines contain one binary string of length 7, the i-th of which encodes the i-th digit of the scoreboard.
Each digit on the scoreboard consists of 7 sticks. We number them, as in the picture below, and let the i-th place of the binary string be 0 if the i-th stick is not glowing and 1 if it is glowing. Then a binary string of length 7 will specify which sticks glowing.
Thus, the sequences “1110111”, “0010010”, “1011101”, “1011011”, “0111010”, “1101011”, “1101111”, “1010010”, “1111111”, “1111011” encode in sequence all digits from 0 to 9 inclusive.
Output
Print a single number consisting of n digits — the maximum number that can be obtained if you turn on exactly k sticks or −1, if it is impossible to turn on exactly k sticks so that some sort of sequence appears on the scoreboard digits.
Examples
Input
1 7
0000000
Output
8
Input
2 5
0010010
0010010
Output
97
Input
3 5
0100001
1001001
1010011
Output
-1
Note
In the first test, we are obliged to include all 7 sticks and get one 8 digit on the scoreboard.
In the second test, we have sticks turned on so that units are formed. For 5 of additionally included sticks, you can get the numbers 07, 18, 34, 43, 70, 79, 81 and 97, of which we choose the maximum — 97.
In the third test, it is impossible to turn on exactly 5 sticks so that a sequence of numbers appears on the scoreboard.
思路:其实我觉得这个题目数据有点水,按理说正解是dp,但是dfs+剪枝也可以过。爆搜是肯定不行的。考虑几个剪枝的地方:
①如果当前使用的已经大于k了,那就肯定不符合题意了。
②如果当前剩下的,小于后面要使用的最小值,说明没法补全,不符合题意。
③如果当前剩下的,大于后面要使用的最大值,说明就算是全都补全,也还有剩余,不符合题意。
代码如下:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxx=2e3+100;
int smin[maxx],smax[maxx];
string s[maxx];
string ins[]={"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int n,k;
inline int fcs(string t)
{
int _min=inf;
int sum;
for(int i=0;i<10;i++)
{
if(ins[i]==t) return 0;
sum=0;
for(int j=0;j<7;j++)
{
if(t[j]=='1'&&ins[i][j]=='0')
{
sum=inf+1;
break;
}
if(t[j]=='0'&&ins[i][j]=='1') sum++;
}
_min=min(sum,_min);
}
return _min;
}
inline int judge(string ss,string tt)
{
if(ss==tt) return 0;
int sum=0;
for(int i=0;i<7;i++)
{
if(ss[i]=='1'&&tt[i]=='0') return inf;
if(ss[i]=='0'&&tt[i]=='1') sum++;
}
return sum;
}
inline int dfs(int pos,int ard,int sur,vector<int> &ans)
{
if(pos>n)
{
if(sur==0) return 1;
return 0;
}
if(ard>k) return 0;
if(sur<smin[n]-smin[pos-1]) return 0;
if(sur>smax[n]-smax[pos-1]) return 0;
for(int i=9;i>=0;i--)
{
int sum=judge(s[pos],ins[i]);
if(sum==inf) continue;
ans.push_back(i);
if(dfs(pos+1,ard+sum,sur-sum,ans)) return 1;
ans.pop_back();
}
return 0;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) cin>>s[i];
smax[0]=smin[0]=0;
int sum;
for(int i=1;i<=n;i++)
{
sum=0;
for(int j=0;j<7;j++) sum+=(s[i][j]-'0');
smax[i]=smax[i-1]+7-sum;
}
for(int i=1;i<=n;i++)
{
sum=fcs(s[i]);
smin[i]=smin[i-1]+sum;
}
vector<int> ans;
if(!dfs(1,0,k,ans)) cout<<-1<<endl;
else
{
for(int i=0;i<ans.size();i++) cout<<ans[i];
cout<<endl;
}
return 0;
}
努力加油a啊,(o)/~