Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 94559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:31:14+00:00 2026-05-10T23:31:14+00:00

I’m writing an algorithm in PHP to solve a given Sudoku puzzle. I’ve set

  • 0

I’m writing an algorithm in PHP to solve a given Sudoku puzzle. I’ve set up a somewhat object-oriented implementation with two classes: a Square class for each individual tile on the 9×9 board, and a Sudoku class, which has a matrix of Squares to represent the board.

The implementation of the algorithm I’m using is a sort of triple-tier approach. The first step, which will solve only the most basic puzzles (but is the most efficient), is to fill in any squares which can only take a single value based on the board’s initial setup, and to adjust the constraints accordingly on the rest of the unsolved squares.

Usually, this process of ‘constant propagation’ doesn’t solve the board entirely, but it does solve a sizable chunk. The second tier will then kick in. This parses each unit (or 9 squares which must all have unique number assignments, e.g. a row or column) for the ‘possible’ values of each unsolved square. This list of possible values is represented as a string in the Square class:

class Square {  private $name;                // 00, 01, 02, ... , 86, 87, 88 private $peers;               // All squares in same row, col, and box private $number;              // Assigned value (0 if not assigned) private $possibles;           // String of possible numbers (1-9)  public function __construct($name, $p = 0) {   $this->name = $name;   $this->setNumber($p);   if ($p == 0) {     $this->possibles = '123456789';   } }  // ... other functions 

Given a whole array of unsolved squares in a unit (as described in the second tier above), the second tier will concatenate all the strings of ‘possibles’ into a single string. It will then search through that single string for any unique character values – values which do not repeat themselves. This will indicate that, within the unit of squares, there is only one square that can take on that particular value.

My question is: for implementing this second tier, how can I parse this string of all the possible values in a unit and easily detect the unique value(s)? I know I could create an array where each index is represented by the numbers 1-9, and I could increment the value at the corresponding index by 1 for each possible-value of that number that I find, then scan the array again for any values of 1, but this seems extremely inefficient, requiring two linear scans of an array for each unit, and in a Sudoku puzzle there are 27 units.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:31:15+00:00Added an answer on May 10, 2026 at 11:31 pm

    This is somewhat like what you have already ruled out as ‘extremely inefficient’, but with builtin functions so it might be quite efficient:

    $all_possibilities = '1234567891234'; $unique = array(); foreach (count_chars($all_possibilities, 1) as $c => $occurrences) {   if ($occurrences == 1)     $unique[] = chr($c); } print join('', $unique) . '\n'; 

    Prints: ‘56789’

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this: $conn = get_db_conn(); # should reuse a connection… May 12, 2026 at 12:47 am
  • Editorial Team
    Editorial Team added an answer After more extensive googling, this http://www.rogueamoeba.com/utm/2007/09/29/ seems to address the… May 12, 2026 at 12:47 am
  • Editorial Team
    Editorial Team added an answer Some browsers have issues with anchors that are display: inline… May 12, 2026 at 12:47 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.