查看: 1980| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[提问] 一道Paint Shop题,希望用java实现

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x

题目如下:
You run a paint shop, and there are a few different colors of paint you can prepare. Each color can be either "gloss" or "matte".
You have a number of customers, and each have some colors they like, either gloss or matte. No customer will like more than one color in matte.
You want to mix the colors, so that:
  • There is just one batch for each color, and it's either gloss or matte.
  • For each customer, there is at least one color they like.
  • You make as few mattes as possible (because they are more expensive).
Your program should accept an input file as a command line argument, and print a result to standard out. An example input file is:
5
1 M 3 G 5 G
2 G 3 M 4 G
5 M
The first line specifies how many colors there are.
Each subsequent line describes a customer. For example, the first customer likes color 1 in matte, color 3 in gloss and color 5 in gloss.
Your program should read an input file like this, and print out either that it is impossible to satisfy all the customer, or describe, for each of the colors, whether it should be made gloss or matte.
The output for the above file should be:
G G G G M
...because all customers can be made happy by every paint being prepared as gloss except number 5.
An example of a file with no solution is:
1
1 G
1 M
Your program should print
No solution exists
A slightly richer example is:
5
2 M
5 G
1 G
5 G 1 G 4 M
3 G
5 G
3 G 5 G 1 G
3 G
2 M
5 G 1 G
2 M
5 G
4 M
5 G 4 M
...which should print:
G M G M G
One more example. The input:
2
1 G 2 M
1 M
...should produce
M M

没什么思路,附上网络上找到的一个用F#做的方法,看了也没啥帮助。扶额。https://github.com/jd78/PaintShop
小白之路好艰难,觉得用啥都不行。。。。


上一篇:LC premium到期后以前做过的收费题还能看吗
下一篇:LeetCode Chrome 插件更新~可以显示部分 locked 信息
🔗
 楼主| malone1564 2016-4-22 02:50:42 | 只看该作者
全局:
没有人看一下这道题吗?
回复

使用道具 举报

🔗
stellari 2016-4-23 09:55:40 | 只看该作者
全局:
我认为此题(在面试的知识范围内)只能暴力搜索。理由是:

假设变量"x_k"代表最后输出结果中第k种颜料的类型。如果x_k = 0则表示k种颜料为Matte; x_k=1则表示其为Gloss。

对于每一位顾客,要使他至少能得到一种满意的颜料,那么当且仅当他买的那些颜料对应的x值的“逻辑或”为1时才可以。比如,对于第1个例子中第1位顾客,就必须满足(NOT x_1) OR x_2 OR x_3 = 1. ... (1)

而要使所有的顾客的要求都满足,则必须当且仅当所有的顾客的(1)式的“逻辑和”为1才行. 对于第1个例子,就是: (NOT x_1 OR x_2 OR x_3) AND (x_2 OR NOT x_3 OR x_4) AND (NOT x_5)....(2)

对于任意的逻辑表达式,我总能够设计出一套顾客需求与之对应,也就是说,此题的算法必须能够解答(2)式的一般形式才可以,这就是是著名的Boolean Satisfiability问题,是第一个被证明的NP-Complete问题。换言之,除非P = NP,否则我们不可能对paint shop这道题提出多项式解法。

因此,此题应该是NP-Hard问题。这样的话,就只能用回溯法试遍所有可能。不过实现应该不难,不妨自己动手试试看?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表