Git Product home page Git Product logo

8-puzzle's Introduction

puzzle

რვიანი-ს ამონახსნის პოვნა ძებნის ალგორითმის საშუალებით

A ალგორითმის რეალიზაცია

function A(state) {
  var closed = []; // განხილული კვანძების სია
  var opened = [ // შესაძლო კვანძთა გზები
    [state] // საწყისი მდგომარეობა
  ];

  // ------------------------------------------------------------

  function g(n) {
    return n.length; // გავლილი გზის წონა საწყისი კვანძიდან
  }

  function h(n, m = goal) {
    var d = 0; // ევრისტიკა - გზის ფასი მოცემული კვანძიდან
    for (var i = 0; i < n.length; i++) // მიზნის კვანძამდე
      if (n[i] != m[i])
        d++; // სხვის ადგილზე მყოფი უჯრედების რაოდენობა
    return d;
  }

  function f(n) {
    var node = n[n.length - 1];
    return g(n) + h(node); // ოპტიმალური ამონახსნი
  }

  function enqueue() {
    opened.sort(function(a, b) {
      return f(a) - f(b); // კვანძების მოწესრიგება
    });
  }

  // ------------------------------------------------------------

  while (opened.length > 0) {
    var node_list = opened.shift(); // შესაძლო "საუკეთესო" გზის
    var node = node_list[node_list.length - 1]; // ბოლო კვანძი
    if (includes(node))
      continue; // კვანძი აქამდე განხილულია
    if (h(node) == 0)
      return node_list; // ამონახსნი - "საუკეთესო" გზა
    closed.push(node); // კვანძი განხილულია
    var nodes = successors(node); // შესაძლო გადასვლები
    for (var i = 0; i < nodes.length; i++) {
      var n = node_list.slice();
      n.push(nodes[i]); // გზაში გადასვლის დამატება
      opened.push(n); // სიაში გზის დამატება
    }
    enqueue(); // გზების მოწესრიგება - "საუკეთესო" გზა თავშია
  }

  // ------------------------------------------------------------

  return null; // ამონახსნი ვერ მოიძებნა
}

ლიცენზია

© 2021 Tsotne Zarandia

8-puzzle's People

Contributors

tsotnezarandia avatar

Watchers

James Cloos avatar  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.