一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 644|回复: 2
收起左侧

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

[复制链接] |试试Instant~ |关注本帖
malone1564 发表于 2016-4-18 06:00:14 | 显示全部楼层 |阅读模式

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

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

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
小白之路好艰难,觉得用啥都不行。。。。

 楼主| 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问题。这样的话,就只能用回溯法试遍所有可能。不过实现应该不难,不妨自己动手试试看?
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-4 15:22

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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