要解决的问题 [英] a problem to solve

查看:48
本文介绍了要解决的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这里有一个问题我已经为自己分配了一些有趣的东西,但是这是一个非常痛苦的事情。它来自一个

益智游戏。如果你看这张图片会有所帮助:

http://www.johnjsal.devisland.net/switches.jpg


这就是情况:图中四行中的每一行都被视为

a单''小组''。每个面板有8个''开关'',每个由
两列组成,这些列共有20个灯(10合1
列,10在另一个)。图片有希望使这个描述明确。

清楚。


阴影框表示当您选择

特定开关时哪些灯会亮起。所以,第一排中的第一个开关,如果打开

,会打开前四个灯,而不是第五个,打开

六,而不是第七,并打开8-14等,直至第20灯。


每个面板只能打开一个开关,所以最终你会有

四个开关点亮。


你拍摄的是找到四个开关的组合所以

相同位置的正好三个灯点亮没有更多或更少。

所以打开每一行中的第一个开关是行不通的,因为

意味着每个开关的第二个指示灯会点亮,而你

不能让所有四个点亮,只有三个。


所以,无论如何,我描述这一切的原因并不是你可以为我解决这个问题,因为我真的很想弄明白。我只是希望你可以给b
给我一些如何实现算法的提示。也许Python有一些功能,我可以使用它来使它比看起来更容易。


我现在的计划是对面板1进行比较,开关1,灯1

,面板2,开关1,灯1,然后面板3,开关1,灯1等,

这听起来很吓人。我不知道Python是否有办法在一步中比较

整个开关。


另外,我想知道我怎么样将交换机实现为数据

结构。现在我将它们作为32个字符串的列表,但我认为

也许某种类型的按位比较可能会有所帮助。


无论如何,任何建议如何继续进行会很棒!我希望我把它描述得很好。

解决方案

John Salerno写道:

Ok ,这是一个问题,我有点为了自己的乐趣而分配给自己,但是这让我的思绪变得非常痛苦。它来自一个益智游戏。如果你看这张图片会有所帮助:

http://www.johnjsal.devisland.net/switches.jpg

以下是这种情况:图中四行中的每一行都被认为是单一的''面板''。每个面板有八个开关,每个由两列组成,这些列共有20个灯(10个在一个列中,10个在另一个中)。图片希望这个描述清楚。

阴影框表示当您选择特定开关时哪些灯会亮起。所以,第一排中的第一个开关,如果打开
,将打开前四个灯,而不是第五个,打开第六个,而不是第七个,然后打开8-14等等到20号灯。

每个面板只能打开一个开关,所以最终你会点亮四个开关。

你是什么拍摄是为了找到四个开关的组合,以便在同一位置正好点亮三个灯,不多或少。
因此,打开每一行中的第一个开关是行不通的,因为那个
意味着每个开关的第二盏灯都会点亮,而你不能让所有四个灯点亮,只有三个。

所以无论如何,我之所以如此描述这一切并非如此,你可以为我解决它,因为我真的想弄明白。我只是希望你能给我一些关于如何实现算法的提示。也许Python有一些功能,我可以用它来使它比看起来更容易。

我现在的计划是对面板1,开关1,灯1进行比较。 >使用面板2,开关1,灯1,然后是面板3,开关1,灯1等,这听起来很可怕。我不知道Python是否有办法在一步中比较整个开关。

另外,我想知道如何将开关实现为数据
结构。现在我将它们作为32个字符串的列表,但我认为
也许某种类型的按位比较可能会有所帮助。

无论如何,任何关于如何继续进行的建议都会很棒!我希望我描述得足够好。




1.将开关实现为由位组成的整数列表

每个指示灯(1表示灯亮,0表示关闭)。

2.你可以通过比较

四个整数来找到四个灯是否打开( &运营商)。如果你得到一个非零值

你就知道,同一位置有四盏灯打开

3.在前八个开关上运行一个循环在这个循环中循环超过

第二行等,以便你得到所有可能的安排测试

(四个嵌套循环)

4.如果你发现一个案例,四个开关给出一个零值,测试你是否b / b
/ b
你需要知道:

什么是整数列表

如何按位和整数(&运算符)

如何使用索引循环列表元素

(例如使用for listIndex in range(0,8):)

如何设置整数的单个位

(例如intVal | 0x01,intVal | 0x02,intVal | 0x04等)


希望这会有所帮助


Claudio




John Salerno写道:

好的,这里是个问题我有点喜欢给自己带来乐趣,但这让我的思绪变得非常痛苦。它来自一个益智游戏。如果你看这张图片会有所帮助:

http://www.johnjsal.devisland.net/switches.jpg

以下是这种情况:图中四行中的每一行都被认为是单一的''面板''。每个面板有八个开关,每个由两列组成,这些列共有20个灯(10个在一个列中,10个在另一个中)。图片希望这个描述清楚。

阴影框表示当您选择特定开关时哪些灯会亮起。所以,第一排中的第一个开关,如果打开
,将打开前四个灯,而不是第五个,打开第六个,而不是第七个,然后打开8-14等等到20号灯。

每个面板只能打开一个开关,所以最终你会点亮四个开关。

你是什么拍摄是为了找到四个开关的组合,以便在同一位置正好点亮三个灯,不多或少。
因此,打开每一行中的第一个开关是行不通的,因为那个
意味着每个开关的第二盏灯都会点亮,而你不能让所有四个灯点亮,只有三个。

所以无论如何,我之所以如此描述这一切并非如此,你可以为我解决它,因为我真的想弄明白。我只是希望你能给我一些关于如何实现算法的提示。也许Python有一些功能,我可以用它来使它比看起来更容易。

我现在的计划是对面板1,开关1,灯1进行比较。 >使用面板2,开关1,灯1,然后是面板3,开关1,灯1等,这听起来很可怕。我不知道Python是否有办法在一步中比较整个开关。

另外,我想知道如何将开关实现为数据
结构。现在我将它们作为32个字符串的列表,但我认为
也许某种类型的按位比较可能会有所帮助。


然后你会想要将灯光表示为20位二进制数。


每个位位置对应4个灯,所以这4个灯可以点亮16种可能的方式。构建一个真值表,看看结果中有哪些
正好点亮了。


A B C D Y

0 0 0 0 0

0 0 0 1 0

0 0 1 0 0

0 0 1 1 0

0 1 0 0 0

0 1 0 1 0

0 1 1 0 0

0 1 1 1 1

1 0 0 0 0

1 0 0 1 0

1 0 1 0 0

1 0 1 1 1

1 1 0 0 0

1 1 0 1 1

1 1 1 0 1

1 1 1 1 0


布尔方程式(其中〜表示NOT,+表示OR,x表示异或)
Y的
因此


Y = ~ABCD + A~BCD + AB~CD + ABC~D


可减至


Y = CD( AxB)+ AB(CxD)


你需要为每个位置做到这一点。


现在每个组合四个开关,当你计算

20 Y值时,找一个

给你11111111111111111111。


对于eaxample,第一个在每个面板中切换(使用g hex)

将是:

A1 = 0xf5fdc
B1 = 0xddb7d
C1 = 0xf33bd
D1 = 0x77edb
Y =((C1& D1)& (A1 ^ B1))| ((A1& B1)&(C1 ^ D1))
Y
674245


但是哪个位置有3个灯点亮?


在基数2中打印Y将告诉我们:

import gmpy
print gmpy.digits(Y,2)



10100100100111000101

现在你想要A,B,C,D作为列表并调整公式Y
$ b相应的$ b。然后简单地尝试ABCD的每个组合。


无论如何,任何关于如何进行的建议都会很棒!我希望我描述得足够好。




我******** @ aol.com 写道:

然后你会想要将灯光表示为20位二进制数。

每个位对应4个灯




我不确定我理解。如果我使用一个20位的数字,那么每个开关上的每个亮点都不会对应一个灯吗?你是什​​么意思

每一位是4盏灯?


Ok, here''s a problem I''ve sort of assigned to myself for fun, but it''s
turning out to be quite a pain to wrap my mind around. It''s from a
puzzle game. It will help if you look at this image:

http://www.johnjsal.devisland.net/switches.jpg

Here''s the situation: Each of the four rows in the diagram is considered
a single ''panel''. Each panel has eight ''switches'', which are composed of
two columns each, and these columns have a total of 20 lights (10 in one
column, 10 in the other). The picture hopefully makes this description
clear.

The shaded boxes denote which lights turn on when you select that
particular switch. So, the very first switch in the first row, if turned
on, would turn on the first four lights, not the fifth, turn on the
sixth, not the seventh, and turn on 8-14, etc. up to the 20th light.

You can only turn on one switch per panel, so eventually you will have
four switches lit.

What you are shooting for is to find a combination of four switches so
that exactly three lights in the same location are lit, no more or less.
So turning on the first switch in each row would not work, because that
would mean that the second light in each switch would be lit, and you
can''t have all four lit, just three.

So anyway, the reason I describe all this isn''t so you can solve it for
me, because I really want to figure it out. I just was hoping you can
give me some tips as to how to implement the algorithm. Maybe Python has
some functions that I can use to make it easier than it seems.

My plan right now is to do a comparison of panel 1, switch 1, light 1
with panel 2, switch 1, light 1, then panel 3, switch 1, light 1, etc.,
which sounds scary. I didn''t know if Python had a way to compare the
whole switch in one step, perhaps.

Also, I was wondering how I might implement the switches as data
structures. Right now I have them as a list of 32 strings, but I thought
maybe some type of bitwise comparisons could help.

Anyway, any advice for how to proceed would be great! I hope I described
it well enough.

解决方案

John Salerno wrote:

Ok, here''s a problem I''ve sort of assigned to myself for fun, but it''s
turning out to be quite a pain to wrap my mind around. It''s from a
puzzle game. It will help if you look at this image:

http://www.johnjsal.devisland.net/switches.jpg

Here''s the situation: Each of the four rows in the diagram is considered
a single ''panel''. Each panel has eight ''switches'', which are composed of
two columns each, and these columns have a total of 20 lights (10 in one
column, 10 in the other). The picture hopefully makes this description
clear.

The shaded boxes denote which lights turn on when you select that
particular switch. So, the very first switch in the first row, if turned
on, would turn on the first four lights, not the fifth, turn on the
sixth, not the seventh, and turn on 8-14, etc. up to the 20th light.

You can only turn on one switch per panel, so eventually you will have
four switches lit.

What you are shooting for is to find a combination of four switches so
that exactly three lights in the same location are lit, no more or less.
So turning on the first switch in each row would not work, because that
would mean that the second light in each switch would be lit, and you
can''t have all four lit, just three.

So anyway, the reason I describe all this isn''t so you can solve it for
me, because I really want to figure it out. I just was hoping you can
give me some tips as to how to implement the algorithm. Maybe Python has
some functions that I can use to make it easier than it seems.

My plan right now is to do a comparison of panel 1, switch 1, light 1
with panel 2, switch 1, light 1, then panel 3, switch 1, light 1, etc.,
which sounds scary. I didn''t know if Python had a way to compare the
whole switch in one step, perhaps.

Also, I was wondering how I might implement the switches as data
structures. Right now I have them as a list of 32 strings, but I thought
maybe some type of bitwise comparisons could help.

Anyway, any advice for how to proceed would be great! I hope I described
it well enough.



1. implement the switches as a list of integers composed out of bits for
each light (1 for light turned on, 0 for off).
2. you can find if four lights are turned on by doing bit comparison of
four integers (the & operator). If you get as result a non-zero value
you know, that there are four lights in same location switched on
3. run a loop over the first eight switches and in this loop a loop over
the second row, etc., so that you get all possible arrangement tested
(four nested loops)
4. if you find a case the four switches gives a zero value, test if you
have exactly three ligths on

You need to know:
what is a list of integers
how to do bitwise and with integers (the & operator)
how to loop over list elements using their index
( e.g with "for listIndex in range(0,8):")
how to set single bits of an integer
( e.g. intVal | 0x01, intVal | 0x02, intVal | 0x04, etc. )

Hope this helps

Claudio



John Salerno wrote:

Ok, here''s a problem I''ve sort of assigned to myself for fun, but it''s
turning out to be quite a pain to wrap my mind around. It''s from a
puzzle game. It will help if you look at this image:

http://www.johnjsal.devisland.net/switches.jpg

Here''s the situation: Each of the four rows in the diagram is considered
a single ''panel''. Each panel has eight ''switches'', which are composed of
two columns each, and these columns have a total of 20 lights (10 in one
column, 10 in the other). The picture hopefully makes this description
clear.

The shaded boxes denote which lights turn on when you select that
particular switch. So, the very first switch in the first row, if turned
on, would turn on the first four lights, not the fifth, turn on the
sixth, not the seventh, and turn on 8-14, etc. up to the 20th light.

You can only turn on one switch per panel, so eventually you will have
four switches lit.

What you are shooting for is to find a combination of four switches so
that exactly three lights in the same location are lit, no more or less.
So turning on the first switch in each row would not work, because that
would mean that the second light in each switch would be lit, and you
can''t have all four lit, just three.

So anyway, the reason I describe all this isn''t so you can solve it for
me, because I really want to figure it out. I just was hoping you can
give me some tips as to how to implement the algorithm. Maybe Python has
some functions that I can use to make it easier than it seems.

My plan right now is to do a comparison of panel 1, switch 1, light 1
with panel 2, switch 1, light 1, then panel 3, switch 1, light 1, etc.,
which sounds scary. I didn''t know if Python had a way to compare the
whole switch in one step, perhaps.

Also, I was wondering how I might implement the switches as data
structures. Right now I have them as a list of 32 strings, but I thought
maybe some type of bitwise comparisons could help.
Then you''ll want to represent the lights as a 20-bit binary number.

Each bit position corresponds to 4 lamps, so there are 16 possible
ways those 4 lamps could be lit. Construct a truth table and see which
of the outcomes have exactly 3 lit.

A B C D Y
0 0 0 0 0
0 0 0 1 0
0 0 1 0 0
0 0 1 1 0
0 1 0 0 0
0 1 0 1 0
0 1 1 0 0
0 1 1 1 1
1 0 0 0 0
1 0 0 1 0
1 0 1 0 0
1 0 1 1 1
1 1 0 0 0
1 1 0 1 1
1 1 1 0 1
1 1 1 1 0

The boolean equation (where ~ means NOT, + means OR, x means XOR)
for Y is thus

Y = ~ABCD + A~BCD + AB~CD + ABC~D

which can be reduced to

Y = CD(AxB) + AB(CxD)

You''ll need to do that for each bit position.

Now for each combination of four switches, look for one that
gives you 11111111111111111111 when you calculate the
20 Y values.

For eaxample, the first switch in each panel (using hex)
would be:

A1 = 0xf5fdc
B1 = 0xddb7d
C1 = 0xf33bd
D1 = 0x77edb Y = ((C1 & D1) & (A1 ^ B1)) | ((A1 & B1) & (C1 ^ D1))
Y 674245

But which positions have 3 lights lit?

Printing Y in base 2 will tell us:
import gmpy
print gmpy.digits(Y,2)


10100100100111000101

Now you''ll want A,B,C,D to be lists and adjust the formula for Y
accordingly. Then simply try every combination of ABCD.


Anyway, any advice for how to proceed would be great! I hope I described
it well enough.




me********@aol.com wrote:

Then you''ll want to represent the lights as a 20-bit binary number.

Each bit position corresponds to 4 lamps



I''m not sure I understand that. If I use a 20-bit number, wouldn''t each
bit correspond to a single light on each switch? What do you mean that
each bit is 4 lamps?


这篇关于要解决的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆