Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Find the number of robots needed to walk through the rectangular grid Date: Sat, 9 Apr 2016 10:43:23 -0600 Lines: 24 Message-ID: References: <8c570da8-ab31-44f3-9fdf-83e28741ffe4@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de g9+oppdsg+O6Wlwb1Bw/XAo+mAO2D1LtKqeYWxK1tRag== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.037 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'column': 0.07; 'subject:number': 0.07; 'grid': 0.09; 'integers': 0.09; 'successive': 0.09; 'index': 0.13; '2016': 0.16; 'arbitrarily': 0.16; 'horizontally': 0.16; 'movements,': 0.16; 'nodes': 0.16; 'queue?': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'structure.': 0.16; 'subject:robots': 0.16; 'wrote:': 0.16; 'implementing': 0.18; '(in': 0.18; 'algorithm': 0.20; 'am,': 0.23; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; 'connected': 0.27; 'checking': 0.27; 'coding': 0.27; 'message- id:@mail.gmail.com': 0.27; 'joe': 0.27; 'cells': 0.29; 'url:wikipedia': 0.29; 'print': 0.30; 'url:wiki': 0.30; "i'd": 0.31; 'anyone': 0.32; 'implement': 0.32; 'run': 0.33; 'equal': 0.34; 'received:google.com': 0.35; 'direction': 0.35; 'step': 0.36; 'but': 0.36; 'needed': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'thought': 0.37; 'received:209': 0.38; 'data': 0.39; 'url:en': 0.39; 'subject:the': 0.39; 'build': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'field': 0.60; 'reach': 0.61; 'distance': 0.63; 'movement': 0.63; 'between': 0.65; 'legal': 0.66; 'direct': 0.68; 'choose': 0.68; 'sum': 0.69; 'centers': 0.72; 'walk': 0.72; 'transfer': 0.73; 'reachable': 0.84; 'standing': 0.84; 'to:name:python': 0.84; 'robot': 0.91; 'steps.': 0.91; 'divided': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=zBZVsiLs/MhpBCw3DpfNVXDRLMqq+h/L8eN1IJM8MK8=; b=X6DclcWnirp+rlhenkHgcsxF5Xt3ODBzFsZtJAayfPpMrXYB9pr0/xXO/9+ISy+gxh GvEmJAKSYUV83p20VhXno3TXdrA5oy8tijeCYV4egTPF6Ib/abFtLozN9mIIcL34rxOI iYpV1kyhDYY7Qb05kNhKfvV3YmC3BQla+R3+ok5JGJVu9v1CPpCndIqp4TuMgNCnCMvn O++ZDMY3pyvd2vlt358zbfiTvn2WFYmPHe7dRMqYGXg7miLMLZHUqf6NP/8KN/31WEm6 UDfENDtLEelApxIvHN/T2+gn3euqm5Qkun+mg+sti7CQxBk/YiwHvqv684uupNSRri3t eMhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=zBZVsiLs/MhpBCw3DpfNVXDRLMqq+h/L8eN1IJM8MK8=; b=IVojI3YH4JDIxhRdShRUxBm/md+lvji209JSQcH7mj4NgAX6PG8cpOIUCPNnF9i81S IPhe5i92jVACcALJiE2vv6pTlhISUTpDFrsF0m3r9l+tctLt4RWToYIu75k01Y2GvCQ0 NPWJivP8aS6gQLvwKfO6P+A4x5hQlM+lQcgNz4CNUlVDAWn6EnrOKY9MqawuEM6YYPpf pXrYg+XQuus8jjKt/tzMylWbeHEiIXu9UlxzDpi1Lk68fP3YLzp70FhJtmi9dDzYlYyS UaVRSt0BE4VX74mELa0siBi6SnwDISg17fUbAi5kLCPOc/fhjzOTAG8Sm7noTxsWZsig Cfxg== X-Gm-Message-State: AD7BkJIsnZPY3v9NE6aT6tzBom1lr58mYrWH2C4kXYverBIaUOZK+W1fzHmCRJ39XqhH/dugaWsI5J4Bled4vA== X-Received: by 10.50.112.10 with SMTP id im10mr9919759igb.93.1460220242505; Sat, 09 Apr 2016 09:44:02 -0700 (PDT) In-Reply-To: <8c570da8-ab31-44f3-9fdf-83e28741ffe4@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <8c570da8-ab31-44f3-9fdf-83e28741ffe4@googlegroups.com> Xref: csiph.com comp.lang.python:106746 On Sat, Apr 9, 2016 at 8:18 AM, Joe wrote: > How to find the number of robots needed to walk through the rectangular grid > The movement of a robot in the field is divided into successive steps > > In one step a robot can move either horizontally or vertically (in one row or in one column of cells) by some number of cells > > A robot can move in one step from cell X to cell Y if and only if the distance between the centers of the cells X and Y is equal to the sum of integers contained in X and Y > > Cell X is reachable for robot A if either A is currently standing in the cell X or A can reach X after some number of steps. During the transfer the robot can choose the direction (horizontal or vertical) of each step arbitrarily > [![enter image description here][1]][1] > > I started implementing it by first checking the row and print the index of the Cell X and Y where the distance is equal to the sum of integers contained in X and Y > > but after coding I found it difficult to remember the index when moving vertically > > So I thought to Build a graph where nodes are grid cells and edges are legal direct movements, then run any connected components algorithm to find which cells are reachable from each other > > > Can anyone implement it with graphs or queue? I'd use a disjoint-set data structure. The number of robots needed is equal to the number of disjoint subsets. https://en.wikipedia.org/wiki/Disjoint-set_data_structure