Coder Social home page Coder Social logo

tallman's Introduction

โจทย์: การทดลองของทอลแมน

ทอลแมนจับหนูมาทำการทดลองวิ่งในเขาวงกตสังเกตดูพฤติกรรมในการเรียนรู้ของหนู โดยการเอาอาหารมาล่อไว้ในเขาวงกต แล้วปล่อยหนูวิ่ง ปรากฏว่า หนูสามารถไปหาอาหารได้ทุกครั้ง

มีวันหนึ่ง หนูตัวที่ทอลแมนใช้ในการทดลองเกิดตาย ทอลแมนไปได้หนูตัวใหม่มา ทอลแมนใช้หนูตัวใหม่กับการทดลองแบบเดิม แต่หนูตัวนี้ฉลาดมากกว่าที่ทอลแมนคิดไว้ หนูตัวนี้วางแผนไว้ล่วงหน้าว่าถ้าเส้นทางที่เอามาให้วิ่ง มีเส้นทางที่ไปถึงอาหารได้จริง ๆ ถึงจะวิ่งหา แต่ถ้าไปไม่ถึงก็จะไม่ออกวิ่ง

Input:

  • แผนที่ที่เป็น array 2 มิติขนาด m*n ที่มีค่าเป็น 0 และ 1 โดยที่ 0 หมายถึงพื้นที่ว่าง และ 1 หมายถึงกำแพง
  • ขนาดของแผนที่ m, n
  • ตำแหน่งเริ่มต้นของหนู x, y
  • ตำแหน่งของอาหารล่อ p, q

Output: True - ถ้ามีเส้นทางเดินจากจุด x, y บนแผนที่ ไปยังอาหารที่จุด p, q ได้ False - ถ้าไม่มีเส้นทางไหนจากจุด x, y ไปถึงจุด p, q

เช่น

[
  [0, 0, 0, 0, 0, 0, 0, 0],
  [1, 1, 0, 1, 1, 1, 1, 0],
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 1, 1, 1, 1, 1, 1, 1],
  [0, 0, 0, 0, 1, 0, 0, 0],
  [1, 0, 1, 1, 0, 1, 1, 0],
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 1, 1, 1, 0, 1, 1, 0]
],
8, 8,
0, 0,
4, 5
-> True

Usage

private val map1: Array<IntArray> = arrayOf(
    intArrayOf(0, 0, 0, 0, 0, 0, 0, 0),
    intArrayOf(1, 1, 0, 1, 1, 1, 1, 0),
    intArrayOf(0, 0, 0, 0, 0, 0, 0, 0),
    intArrayOf(0, 1, 1, 1, 1, 1, 1, 1),
    intArrayOf(0, 0, 0, 0, 1, 0, 0, 0),
    intArrayOf(1, 0, 1, 1, 0, 1, 1, 0),
    intArrayOf(0, 0, 0, 0, 0, 0, 0, 0),
    intArrayOf(0, 1, 1, 1, 0, 1, 1, 0)
)

val rat = TallManRat.throwInMaze(map = map1, food = 4 to 5, position = 0 to 0)
println(rat.think(SpeakOptions.ALL_ROUTE)) // Available options are: ALL_ROUTE, BEST_ROUTE

Output

All route

(0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (2, 2) -> (2, 1) -> (2, 0) -> (3, 0) -> (4, 0) -> (4, 1) -> (5, 1) -> (6, 1) -> (6, 2) -> (6, 3) -> (6, 4) -> (6, 5) -> (6, 6) -> (6, 7) -> (5, 7) -> (4, 7) -> (4, 6) -> (4, 5)
Total steps: 21
(0, 0) -> (0, 1) -> (0, 2) -> (0, 3) -> (0, 4) -> (0, 5) -> (0, 6) -> (0, 7) -> (1, 7) -> (2, 7) -> (2, 6) -> (2, 5) -> (2, 4) -> (2, 3) -> (2, 2) -> (2, 1) -> (2, 0) -> (3, 0) -> (4, 0) -> (4, 1) -> (5, 1) -> (6, 1) -> (6, 2) -> (6, 3) -> (6, 4) -> (6, 5) -> (6, 6) -> (6, 7) -> (5, 7) -> (4, 7) -> (4, 6) -> (4, 5)
Total steps: 31
true

Best route

(0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (2, 2) -> (2, 1) -> (2, 0) -> (3, 0) -> (4, 0) -> (4, 1) -> (5, 1) -> (6, 1) -> (6, 2) -> (6, 3) -> (6, 4) -> (6, 5) -> (6, 6) -> (6, 7) -> (5, 7) -> (4, 7) -> (4, 6) -> (4, 5)
Total steps: 21
true

tallman's People

Watchers

James Cloos avatar PS avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.