搜索
首页后端开发php教程sudoku breaker-php edition

Deducer class:

  1 php
  2 class  Deducer
  3 {
  4      private   $array ;
  5      public   function  __construct( $array )
  6     { 
  7          $this -> array = array ();
  8          for ( $row = 0 ; $row   9         {
 10              for ( $column = 0 ; $column  11             {
 12                  $this -> array [ $row ][ $column ] = $array [ $row ][ $column ];
 13             }
 14         }
 15        } 
 16      private   function  isFinished()
 17     {
 18          for ( $row = 0 ; $row  19         {
 20              for ( $column = 0 ; $column  21             {
 22                  if ( $this -> array [ $row ][ $column ] == 0 )
 23                 {
 24                      return   false ;
 25                 }
 26             }
 27         }
 28          return   true ;
 29     }
 30      public   function  deduceAll()
 31     {
 32          if ( $this -> isFinished())
 33         {
 34              return ;
 35         }
 36          for ( $row = 0 ; $row  37         {
 38              for ( $column = 0 ; $column  39             {
 40                  if ( $this -> reduceFromOneZone( $this -> array , $row , $column ))
 41                 {
 42                      $this -> deduceAll();
 43                      return ;
 44                 }
 45             }
 46         }
 47          for ( $row = 0 ; $row  48         {
 49              if ( $this -> reduceFromOneRow( $this -> array , $row ))
 50             {
 51                  $this -> deduceAll();
 52                  return ;
 53             }
 54         }
 55          for ( $column = 0 ; $column  56         {
 57              if ( $this -> reduceFromOneColumn( $this -> array , $column ))
 58             {
 59                  $this -> deduceAll();
 60                  return ;
 61             }
 62         }
 63          for ( $row = 0 ; $row  64         {
 65              if ( $this -> reduceFromThreeRows( $this -> array , $row , $row + 2 ))
 66             {
 67                  $this -> deduceAll();
 68                  return ;
 69             }
 70         } 
 71          for ( $column = 0 ; $column  72         {
 73              if ( $this -> reduceFromThreeColumns( $this -> array , $column , $column + 2 ))
 74             {
 75                  $this -> deduceAll();
 76                  return ;
 77             }
 78         }
 79     }
 80      public   function  deduceOnce()
 81     {
 82          for ( $row = 0 ; $row  83         {
 84              for ( $column = 0 ; $column  85             {
 86                  if ( $this -> reduceFromOneZone( $this -> array , $row , $column ))
 87                 {
 88                      return ;
 89                 }
 90             }
 91         }
 92          for ( $row = 0 ; $row  93         {
 94              if ( $this -> reduceFromOneRow( $this -> array , $row ))
 95             {
 96                  return ;
 97             }
 98         }
 99          for ( $column = 0 ; $column 100         {
101              if ( $this -> reduceFromOneColumn( $this -> array , $column ))
102             {
103                  return ;
104             }
105         }
106          for ( $row = 0 ; $row 107         {
108              if ( $this -> reduceFromThreeRows( $this -> array , $row , $row + 2 ))
109             {
110                  return ;
111             }
112         } 
113          for ( $column = 0 ; $column 114         {
115              if ( $this -> reduceFromThreeColumns( $this -> array , $column , $column + 2 ))
116             {
117                  return ;
118             }
119         }
120     }
121      private   function  reduceFromOneZone( & $array , $row , $column )
122     {
123          $startRow = ( floor ( $row / 3 )) * 3 ;
124          $startColumn = ( floor ( $column / 3 )) * 3 ;
125          $unknown = array ();
126          for ( $pointer = 0 ; $pointer 127         {
128              $unknown [ $pointer ] = $pointer + 1 ;
129         }
130          for ( $rowPointer = $startRow ; $rowPointer 131         {
132              for ( $columnPointer = $startColumn ; $columnPointer 133             {
134                  if ( $array [ $rowPointer ][ $columnPointer ] != 0 )
135                 {
136                      $unknown [ $array [ $rowPointer ][ $columnPointer ] - 1 ] = 0 ;
137                 }
138             }
139         }
140          for ( $digit = 0 ; $digit 141         {
142              if ( $unknown [ $digit ] != 0 )
143             {
144                  $number = $unknown [ $digit ];
145                  $posibilities = 0 ;
146                  $rowPosition =- 1 ;
147                  $columnPosition =- 1 ;
148                  for ( $rowPointer = $startRow ; $rowPointer 149                 {
150                      for ( $columnPointer = $startColumn ; $columnPointer 151                     {
152                          if ( $array [ $rowPointer ][ $columnPointer ] == 0 )
153                         {
154                              if ( $this -> isPossibleInThatCellCheckByColumn( $array , $number , $rowPointer , $columnPointer ) && $this -> isPossibleInThatCellCheckByRow( $array , $number , $rowPointer , $columnPointer ))
155                             {
156                                  $rowPosition = $rowPointer ;
157                                  $columnPosition = $columnPointer ;
158                                  $posibilities ++ ;
159                             }
160                         }
161                     }
162                 }
163                  if ( $posibilities == 1 )
164                 {
165                      $array [ $rowPosition ][ $columnPosition ] = $number ;
166                      return   true ;
167                 }
168             }
169         }
170          return   false ;
171     }
172      private   function  reduceFromOneRow( & $array , $row )
173     {
174          $unknown = array ();
175          for ( $column = 0 ; $column 176         {
177              $unknown [ $column ] = $column + 1 ;
178         }
179          for ( $column = 0 ; $column 180         {
181              if ( $array [ $row ][ $column ] != 0 )
182             {
183                  $unknown [ $array [ $row ][ $column ] - 1 ] = 0 ;
184             }
185         }
186          for ( $column = 0 ; $column 187         {
188              if ( $unknown [ $column ] != 0 )
189             {
190                  $number = $unknown [ $column ];
191                  $posibilities = 0 ;
192                  $position =- 1 ;
193                  for ( $pointer = 0 ; $pointer 194                 {
195                      if ( $array [ $row ][ $pointer ] == 0 )
196                     {
197                          if ( $this -> isPossibleInThatCellCheckByColumnAndZone( $array , $number , $row , $pointer ))
198                         {
199                              $position = $pointer ;
200                              $posibilities ++ ;
201                         }
202                     }
203                 }
204                  if ( $posibilities == 1 )
205                 {
206                      $array [ $row ][ $position ] = $number ;
207                      return   true ;
208                 }
209             }
210         }
211          return   false ;
212     }
213      private   function  reduceFromOneColumn( & $array , $column )
214     {
215          $unknown = array ();
216          for ( $row = 0 ; $row 217         {
218              $unknown [ $row ] = $row + 1 ;
219         }
220          for ( $row = 0 ; $row 221         {
222              if ( $array [ $row ][ $column ] != 0 )
223             {
224                  $unknown [ $array [ $row ][ $column ] - 1 ] = 0 ;
225             }
226         }
227          for ( $row = 0 ; $row 228         {
229              if ( $unknown [ $row ] != 0 )
230             {
231                  $number = $unknown [ $row ];
232                  $posibilities = 0 ;
233                  $position =- 1 ;
234                  for ( $pointer = 0 ; $pointer 235                 {
236                      if ( $array [ $pointer ][ $column ] == 0 )
237                     {
238                          if ( $this -> isPossibleInThatCellCheckByRowAndZone( $array , $number , $pointer , $column ))
239                         {
240                              $position = $pointer ;
241                              $posibilities ++ ;
242                         }
243                     }
244                 }
245                  if ( $posibilities == 1 )
246                 {
247                      $array [ $position ][ $column ] = $number ;
248                      return   true ;
249                 }
250             }
251         }
252          return   false ;
253     }
254      private   function  isPossibleInThatCellCheckByRowAndZone( $array , $number , $row , $column )
255     {
256          if ( ! $this -> isPossibleInThatCellCheckByRow( $array , $number , $row , $column ))
257         {
258              return   false ;
259         }
260          else   if ( ! $this -> isPossibleInThatCellCheckByZone( $array , $number , $row , $column ))
261         {
262              return   false ;
263         }
264          else   if ( ! $this -> canBeInThatZoneCheckByColumn( $array , $number , $row , $column ))
265         {
266              return   false ;
267         }
268          else
269         {
270              return   true ;
271         }
272     }
273      private   function  isPossibleInThatCellCheckByColumnAndZone( $array , $number , $row , $column )
274     {
275          if ( ! $this -> isPossibleInThatCellCheckByColumn( $array , $number , $row , $column ))
276         {
277              return   false ;
278         }
279          else   if ( ! $this -> isPossibleInThatCellCheckByZone( $array , $number , $row , $column ))
280         {
281              return   false ;
282         }
283          else   if ( ! $this -> canBeInThatZoneCheckByRow( $array , $number , $row , $column ))
284         {
285              return   false ;
286         }
287          else
288         {
289              return   true ;
290         }
291     }
292      private   function  canBeInThatZoneCheckByRow( $array , $number , $row , $column )
293     {    
294          $startRow = ( floor ( $row / 3 )) * 3 ;
295          $startColumn = ( floor ( $column / 3 )) * 3 ;
296          for ( $rowPointer = $startRow ; $rowPointer 297         {
298              if ( $rowPointer != $row )
299             {
300                  if ( ! $this -> isPossibleInThatCellCheckByRow( $array , $number , $rowPointer , $column ))
301                 {
302                      continue ;
303                 }
304                  $canItBe = true ;
305                  for ( $columnPointer = 0 ; $columnPointer 306                 {
307                      if ( $columnPointer $startColumn + 2 )
308                     {
309                          if ( $array [ $rowPointer ][ $columnPointer ] == 0 )
310                         {
311                              if ( $this -> isPossibleInThatCellCheckByColumn( $array , $number , $rowPointer , $columnPointer ) && $this -> isPossibleInThatCellCheckByZone( $array , $number , $rowPointer , $columnPointer ))
312                             {
313                                  $canItBe = false ;
314                             }
315                         }
316                     }
317                 }
318                  if ( $canItBe )
319                 {
320                      return   false ;
321                 }
322             }
323         }
324          return   true ;
325     }
326      private   function  canBeInThatZoneCheckByColumn( $array , $number , $row , $column )
327     {    
328          $startRow = ( floor ( $row / 3 )) * 3 ;
329          $startColumn = ( floor ( $column / 3 )) * 3 ;
330          for ( $columnPointer = $startColumn ; $columnPointer 331         {
332              if ( $columnPointer != $column )
333             {
334                  if ( ! $this -> isPossibleInThatCellCheckByColumn( $array , $number , $row , $columnPointer ))
335                 {
336                      continue ;
337                 }
338                  $canItBe = true ;
339                  for ( $rowPointer = 0 ; $rowPointer 340                 {
341                      if ( $rowPointer $startRow + 2 )
342                     {
343                          if ( $array [ $rowPointer ][ $columnPointer ] == 0 )
344                         {
345                              if ( $this -> isPossibleInThatCellCheckByRow( $array , $number , $rowPointer , $columnPointer ) && $this -> isPossibleInThatCellCheckByZone( $array , $number , $rowPointer , $columnPointer ))
346                             {
347                                  $canItBe = false ;
348                             }
349                         }
350                     }
351                 }
352                  if ( $canItBe )
353                 {
354                      return   false ;
355                 }
356             }
357         }
358          return   true ;
359     }
360      private   function  isPossibleInThatCellCheckByZone( $array , $number , $row , $column )
361     {
362          $startRow = ( floor ( $row / 3 )) * 3 ;
363          $startColumn = ( floor ( $column / 3 )) * 3 ;
364          for ( $rowPointer = $startRow ; $rowPointer 365         {
366              for ( $columnPointer = $startColumn ; $columnPointer 367             {
368                  if ( $array [ $rowPointer ][ $columnPointer ] == $number )
369                 {
370                      return   false ;
371                 }
372             }
373         }
374          return   true ;
375     }
376      private   function  reduceFromThreeColumns( & $array , $firstColumn , $lastColumn )
377     {
378          $numberAndCount = array ();
379          $numberAndPosition = array ();
380          for ( $row = 0 ; $row 381         {
382              $numberAndCount [ $row ][ 0 ] = $row + 1 ;
383              $numberAndCount [ $row ][ 1 ] = 0 ;
384         }
385          for ( $row = 0 ; $row 386         {
387              for ( $column = 0 ; $column 388             {
389                  $numberAndPosition [ $row ][ $column ] = 0 ;
390             }
391         }
392          for ( $column = $firstColumn ; $column 393         {
394              for ( $row = 0 ; $row 395             {
396                  if ( $array [ $row ][ $column ] != 0 )
397                 {
398                      $numberAndCount [ $array [ $row ][ $column ] - 1 ][ 1 ] ++ ;
399                      $numberAndPosition [ 9 * ( $column % 3 ) + $row ][ 0 ] = $array [ $row ][ $column ];
400                      $numberAndPosition [ 9 * ( $column % 3 ) + $row ][ 1 ] = $row ;
401                      $numberAndPosition [ 9 * ( $column % 3 ) + $row ][ 2 ] = $column ;
402                 }
403             }
404         }
405          for ( $row = 0 ; $row 406         {
407              if ( $numberAndCount [ $row ][ 1 ] == 2 )
408             {
409                  $number = $numberAndCount [ $row ][ 0 ];
410                  $pointer = 0 ;
411                  $firstAppearanceRowPosition =- 1 ;
412                  $firstAppearanceColumnPosition =- 1 ;
413                  $secondAppearanceRowPosition =- 1 ;
414                  $secondAppearanceColumnPosition =- 1 ;
415                  while ( $pointer 416                 {
417                      if ( $numberAndPosition [ $pointer ][ 0 ] == $number )
418                     {
419                          $firstAppearanceRowPosition = $numberAndPosition [ $pointer ][ 1 ];
420                          $firstAppearanceColumnPosition = $numberAndPosition [ $pointer ][ 2 ];
421                          $pointer ++ ;
422                          break ;
423                     }
424                      else
425                     {    
426                          $pointer ++ ;
427                     }
428                 }
429                  while ( $pointer 430                 {
431                      if ( $numberAndPosition [ $pointer ][ 0 ] == $number )
432                     {
433                          $secondAppearanceRowPosition = $numberAndPosition [ $pointer ][ 1 ];
434                          $secondAppearanceColumnPosition = $numberAndPosition [ $pointer ][ 2 ];
435                          break ;
436                     }
437                      else
438                     {
439                          $pointer ++ ;
440                     }
441                 }
442                  $thirdAppearanceColumnPosition = 3 * ( floor ( $firstAppearanceColumnPosition / 3 )) + 3 - $firstAppearanceColumnPosition % 3 - $secondAppearanceColumnPosition % 3 ;
443                  $thirdAppearanceRowStartPosition = ( 3 - ( floor ( $firstAppearanceRowPosition / 3 )) - ( floor ( $secondAppearanceRowPosition / 3 ))) * 3 ;
444                  $posibilities = 0 ;
445                  $thirdAppearanceRowPosition =- 1 ;
446                  for ( $indicator = $thirdAppearanceRowStartPosition ; $indicator 447                 {
448                      if ( $array [ $indicator ][ $thirdAppearanceColumnPosition ] == 0 )
449                     {
450                          if ( $this -> isPossibleInThatCellCheckByRow( $array , $number , $indicator , $thirdAppearanceColumnPosition ))
451                         {
452                              $thirdAppearanceRowPosition = $indicator ;
453                              $posibilities ++ ;
454                         }
455                     }
456                 }
457                  if ( $posibilities == 1 )
458                 {    
459                      $array [ $thirdAppearanceRowPosition ][ $thirdAppearanceColumnPosition ] = $number ;
460                      return   true ;
461                 }
462             }
463         }
464          return   false ;
465     }
466      private   function  reduceFromThreeRows( & $array , $firstRow , $lastRow )
467     {
468          $numberAndCount = array ();
469          $numberAndPosition = array ();
470          for ( $column = 0 ; $column 471         {
472              $numberAndCount [ 0 ][ $column ] = $column + 1 ;
473              $numberAndCount [ 1 ][ $column ] = 0 ;
474         }
475          for ( $row = 0 ; $row 476         {
477              for ( $column = 0 ; $column 478             {
479                  $numberAndPosition [ $row ][ $column ] = 0 ;
480             }
481         }
482          for ( $row = $firstRow ; $row 483         {
484              for ( $column = 0 ; $column 485             {
486                  if ( $array [ $row ][ $column ] != 0 )
487                 {
488                      $numberAndCount [ 1 ][ $array [ $row ][ $column ] - 1 ] ++ ;
489                      $numberAndPosition [ 0 ][ 9 * ( $row % 3 ) + $column ] = $array [ $row ][ $column ];
490                      $numberAndPosition [ 1 ][ 9 * ( $row % 3 ) + $column ] = $row ;
491                      $numberAndPosition [ 2 ][ 9 * ( $row % 3 ) + $column ] = $column ;
492                 }
493             }
494         }
495          for ( $column = 0 ; $column 496         {
497              if ( $numberAndCount [ 1 ][ $column ] == 2 )
498             {
499                  $number = $numberAndCount [ 0 ][ $column ];
500                  $pointer = 0 ;
501                  $firstAppearanceRowPosition =- 1 ;
502                  $firstAppearanceColumnPosition =- 1 ;
503                  $secondAppearanceRowPosition =- 1 ;
504                  $secondAppearanceColumnPosition =- 1 ;
505                  while ( $pointer 506                 {
507                      if ( $numberAndPosition [ 0 ][ $pointer ] == $number )
508                     {
509                          $firstAppearanceRowPosition = $numberAndPosition [ 1 ][ $pointer ];
510                          $firstAppearanceColumnPosition = $numberAndPosition [ 2 ][ $pointer ];
511                          $pointer ++ ;
512                          break ;
513                     }
514                      else
515                     {    
516                          $pointer ++ ;
517                     }
518                 }
519                  while ( $pointer 520                 {
521                      if ( $numberAndPosition [ 0 ][ $pointer ] == $number )
522                     {
523                          $secondAppearanceRowPosition = $numberAndPosition [ 1 ][ $pointer ];
524                          $secondAppearanceColumnPosition = $numberAndPosition [ 2 ][ $pointer ];
525                          break ;
526                     }
527                      else
528                     {
529                          $pointer ++ ;
530                     }
531                 }
532                  $thirdAppearanceRowPosition = 3 * ( floor ( $firstAppearanceRowPosition / 3 )) + 3 - $firstAppearanceRowPosition % 3 - $secondAppearanceRowPosition % 3 ;
533                  $thirdAppearanceColumnStartPosition = ( 3 - ( floor ( $firstAppearanceColumnPosition / 3 )) - ( floor ( $secondAppearanceColumnPosition / 3 ))) * 3 ;
534              

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP的目的:构建动态网站PHP的目的:构建动态网站Apr 15, 2025 am 12:18 AM

PHP用于构建动态网站,其核心功能包括:1.生成动态内容,通过与数据库对接实时生成网页;2.处理用户交互和表单提交,验证输入并响应操作;3.管理会话和用户认证,提供个性化体验;4.优化性能和遵循最佳实践,提升网站效率和安全性。

PHP:处理数据库和服务器端逻辑PHP:处理数据库和服务器端逻辑Apr 15, 2025 am 12:15 AM

PHP在数据库操作和服务器端逻辑处理中使用MySQLi和PDO扩展进行数据库交互,并通过会话管理等功能处理服务器端逻辑。1)使用MySQLi或PDO连接数据库,执行SQL查询。2)通过会话管理等功能处理HTTP请求和用户状态。3)使用事务确保数据库操作的原子性。4)防止SQL注入,使用异常处理和关闭连接来调试。5)通过索引和缓存优化性能,编写可读性高的代码并进行错误处理。

您如何防止PHP中的SQL注入? (准备的陈述,PDO)您如何防止PHP中的SQL注入? (准备的陈述,PDO)Apr 15, 2025 am 12:15 AM

在PHP中使用预处理语句和PDO可以有效防范SQL注入攻击。1)使用PDO连接数据库并设置错误模式。2)通过prepare方法创建预处理语句,使用占位符和execute方法传递数据。3)处理查询结果并确保代码的安全性和性能。

PHP和Python:代码示例和比较PHP和Python:代码示例和比较Apr 15, 2025 am 12:07 AM

PHP和Python各有优劣,选择取决于项目需求和个人偏好。1.PHP适合快速开发和维护大型Web应用。2.Python在数据科学和机器学习领域占据主导地位。

PHP行动:现实世界中的示例和应用程序PHP行动:现实世界中的示例和应用程序Apr 14, 2025 am 12:19 AM

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP:轻松创建交互式Web内容PHP:轻松创建交互式Web内容Apr 14, 2025 am 12:15 AM

PHP可以轻松创建互动网页内容。1)通过嵌入HTML动态生成内容,根据用户输入或数据库数据实时展示。2)处理表单提交并生成动态输出,确保使用htmlspecialchars防XSS。3)结合MySQL创建用户注册系统,使用password_hash和预处理语句增强安全性。掌握这些技巧将提升Web开发效率。

PHP和Python:比较两种流行的编程语言PHP和Python:比较两种流行的编程语言Apr 14, 2025 am 12:13 AM

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP的持久相关性:它还活着吗?PHP的持久相关性:它还活着吗?Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在现代编程领域中依然占据重要地位。1)PHP的简单易学和强大社区支持使其在Web开发中广泛应用;2)其灵活性和稳定性使其在处理Web表单、数据库操作和文件处理等方面表现出色;3)PHP不断进化和优化,适用于初学者和经验丰富的开发者。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中