日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

程序员的算法趣题Q68: 异性相邻的座位安排(1)

發(fā)布時(shí)間:2023/12/8 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 程序员的算法趣题Q68: 异性相邻的座位安排(1) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

1. 問(wèn)題描述

2. 解題分析

3. 代碼及測(cè)試

4. 后記


1. 問(wèn)題描述

??

????????這道題的描述應(yīng)該是有問(wèn)題的(不知道是原文的問(wèn)題還是翻譯的問(wèn)題)。

????????前面的描述中提到“前后左右的座位全是異性”和NG示例中的“前后左右全是同性”兩種情況。問(wèn)題中所要求滿(mǎn)足的“上述條件”是什么呢?僅從上下文來(lái)看第一感當(dāng)然是說(shuō)“前后左右的座位全是異性”。但是仔細(xì)一想就知道這個(gè)不對(duì),每個(gè)座位的“前后左右的座位都是異性”的安排情況只有兩種。

????????“前后左右的座位全是異性”和“前后左右全是同性”的兩種極端情況之間還有巨大的灰色空間。問(wèn)題的原意應(yīng)該是指滿(mǎn)足“任何一個(gè)座位的前后左右不全是同性(或至少有一個(gè)異性)”的情況吧?

2. 解題分析

????????先考慮一個(gè)基本方案。

????????從搜索方式來(lái)說(shuō),這個(gè)問(wèn)題與前面的Q32和Q59等題有類(lèi)似之處。只需要基于Q32或Q59的基本框架略作修改即可。

????????要點(diǎn)說(shuō)明:

  • 與Q32, Q59一樣,逐行掃描,每次向右移一格。右邊到頭即移到下一格。到達(dá)最下邊的圍欄行則表明已經(jīng)完成一次搜索,找到一個(gè)合規(guī)的安排方案
  • “違規(guī)檢查”只需要針對(duì)當(dāng)前座位的左邊座位和上邊座位,這是因?yàn)閽呙枋窍蛴摇⑾路较蜻M(jìn)行的,當(dāng)前座位的右邊和下邊還沒(méi)有安排人,所以右邊座位和下邊座位肯定還沒(méi)有違規(guī)
  • 最后還必須滿(mǎn)足男生和女生人數(shù)都必須相等
  • ??

    3. 代碼及測(cè)試

    # -*- coding: utf-8 -*- """ Created on Tue Oct 19 07:39:48 2021@author: chenxy """import sys import time import datetime import math # import random from typing import List from collections import deque import itertools as it import numpy as npH = 5 # Height, vertical W = 6 # Width, horizontal# seats initialization, with a guard band surrounding the original seats # The guard band is initialized to '-1' to simplify the judgement processing. seats = np.zeros((H+2, W+2)) seats[0,:] = -1 seats[H+1,:] = -1 seats[:,0] = -1 seats[:,W+1] = -1count = 0def isNG(h,w):if seats[h,w] == -1:return Falsereturn (seats[h+1,w]==seats[h,w] or seats[h+1,w]==-1) and \(seats[h-1,w]==seats[h,w] or seats[h-1,w]==-1) and \(seats[h,w+1]==seats[h,w] or seats[h,w+1]==-1) and \(seats[h,w-1]==seats[h,w] or seats[h,w-1]==-1) def arrange_seat(h,w, boy, girl)->int:'''Parameters----------(h,w) : The current exploration point. h represents row index, w represents col index.Returns: intThe number of total arrangement starting from the point (h,w), together with the current seats status, which is a global variable''' global count# print('h = {0}, w = {1}'.format(h,w))if h == H + 1:if boy == girl:count = count + 1# print(seats) elif w == W + 1: # Go to the next row.# Reach the right boundary, go to explore the next row from the left arrange_seat(h+1, 1, boy, girl)# elif seats[h,w] > 0: # # This grid has been occupied, move to the right one# arrange_seat(h, w+1)else:# Try to arrange boy to the current seat(h,w)seats[h,w] = 1if not (isNG(h-1,w) or isNG(h,w-1) or isNG(h,w)):arrange_seat(h,w+1, boy+1, girl) seats[h,w] = 0# Try to arrange girl to the current seat(h,w)seats[h,w] = 2if not (isNG(h-1,w) or isNG(h,w-1) or isNG(h,w)):arrange_seat(h,w+1, boy, girl+1) seats[h,w] = 0tStart = time.perf_counter() arrange_seat(1, 1, 0, 0) tCost = time.perf_counter() - tStart print('count = {0}, tCost = {1:6.3f}(sec)'.format(count,tCost))

    運(yùn)行結(jié)果:? ? ? ??

    ? ? ? ? [3,4]: count = 354, tCost = ?0.014(sec)?

    ? ? ? ? [4,5]: count = 34874, tCost = ?1.649(sec)

    ? ? ? ? [5,6]: count = 13374192, tCost = 803.130(sec)

    4. 后記

    ????????跟昨天的Q69一樣,給出一個(gè)功能正確的方案并不難,但是運(yùn)行太慢了!所以這些問(wèn)題的難點(diǎn)在于如何提高運(yùn)行效率,比如說(shuō)書(shū)中所提示的剪枝,還有memoization等啊?容我再想一想。。。

    ? ? ? ? 上一篇:Q67: 不挨著坐是一種禮節(jié)嗎?

    ? ? ? ? 下一篇:Q69: 藍(lán)白歌會(huì)(1)??? ?

    ? ? ? ? 本系列總目錄參見(jiàn):程序員的算法趣題:詳細(xì)分析和Python全解

    總結(jié)

    以上是生活随笔為你收集整理的程序员的算法趣题Q68: 异性相邻的座位安排(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。