00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef GO_REGION_H
00016 #define GO_REGION_H
00017
00018 #include <bitset>
00019 #include <iosfwd>
00020 #include "GoBoard.h"
00021 #include "GoBoardUtil.h"
00022 #include "GoEyeCount.h"
00023 #include "SgVector.h"
00024 #include "SgIncrementalStack.h"
00025 #include "SgMiaiStrategy.h"
00026 #include "SgPointArray.h"
00027
00028 class GoBlock;
00029 class GoChain;
00030 class GoRegionBoard;
00031
00032
00033
00034
00035
00036
00037
00038 enum GoRegionFlag
00039 {
00040 GO_REGION_SMALL,
00041 GO_REGION_CORRIDOR,
00042 GO_REGION_STATIC_1VC,
00043 GO_REGION_1VC,
00044 GO_REGION_STATIC_2V,
00045 GO_REGION_2V,
00046 GO_REGION_SINGLE_BLOCK_BOUNDARY,
00047 GO_REGION_OPP_CAN_LIVE_INSIDE,
00048 GO_REGION_AT_LEAST_SEKI,
00049 GO_REGION_SAFE,
00050 GO_REGION_PROTECTED_CUTS,
00051 GO_REGION_STATIC_1VITAL,
00052 GO_REGION_1VITAL,
00053 GO_REGION_USED_FOR_MERGE,
00054 GO_REGION_VALID,
00055 GO_REGION_COMPUTED_BLOCKS,
00056 GO_REGION_COMPUTED_CHAINS,
00057 GO_REGION_COMPUTED_NAKADE,
00058 _GO_REGION_FLAG_COUNT
00059 };
00060
00061
00062 typedef std::bitset<_GO_REGION_FLAG_COUNT> GoRegionFlags;
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 class GoRegion
00087 {
00088 public:
00089
00090 GoRegion(const GoBoard& board, const SgPointSet& points,
00091 SgBlackWhite color);
00092
00093
00094 ~GoRegion()
00095 {
00096 #ifndef NDEBUG
00097 ++s_free;
00098 #endif
00099 }
00100
00101
00102 void ReInitialize();
00103
00104
00105 void CheckConsistency() const;
00106
00107
00108
00109 const SgPointSet& Points() const
00110 {
00111 return m_points;
00112 }
00113
00114
00115 SgPointSet Dep() const
00116 {
00117 return m_points.Border(m_bd.Size());
00118 }
00119
00120
00121 SgPointSet AllInsideLibs() const;
00122
00123
00124 const SgVectorOf<GoChain>& Chains() const
00125 {
00126 return m_chains;
00127 }
00128
00129
00130 const SgVectorOf<GoBlock>& Blocks() const
00131 {
00132 return m_blocks;
00133 }
00134
00135
00136
00137
00138 SgVectorOf<GoBlock>& BlocksNonConst()
00139 {
00140 return m_blocks;
00141 }
00142
00143
00144 SgVectorOf<GoBlock> InteriorBlocks() const;
00145
00146
00147 bool IsInteriorBlock(const GoBlock* block) const;
00148
00149
00150 bool IsBoundaryBlock(const GoBlock* block) const
00151 {
00152 return ! IsInteriorBlock(block);
00153 }
00154
00155
00156 SgPointSet BlocksPoints() const;
00157
00158
00159 SgPointSet PointsPlusInteriorBlocks() const;
00160
00161
00162 SgBlackWhite Color() const
00163 {
00164 return m_color;
00165 }
00166
00167
00168 int MinEyes() const {return m_eyes.MinEyes();}
00169
00170
00171 int MaxEyes() const {return m_eyes.MaxEyes();}
00172
00173
00174 int MinPotEyes() const {return m_eyes.MinPotEyes();}
00175
00176
00177 int MaxPotEyes() const {return m_eyes.MaxPotEyes();}
00178
00179
00180 void Write(std::ostream& out) const;
00181
00182
00183 void WriteID(std::ostream& out) const;
00184
00185
00186
00187 bool GetFlag(GoRegionFlag flag) const;
00188
00189
00190 bool ComputeAndGetFlag(GoRegionFlag flag);
00191
00192
00193 bool ComputedFlag(GoRegionFlag flag) const;
00194
00195
00196 void ComputeFlag(GoRegionFlag flag);
00197
00198
00199
00200
00201 void ResetNonBlockFlags()
00202 { m_computedFlags.reset();
00203 m_computedFlags.set(GO_REGION_COMPUTED_BLOCKS);
00204 m_eyes.Clear();
00205 Invalidate();
00206 }
00207
00208
00209 bool IsValid() const {return m_flags.test(GO_REGION_VALID);}
00210
00211
00212 void Invalidate() {m_flags.reset(GO_REGION_VALID);}
00213
00214
00215
00216
00217
00218 void ComputeBasicFlags();
00219
00220
00221 void DoComputeFlag(GoRegionFlag flag);
00222
00223
00224 void SetComputedFlag(GoRegionFlag flag) {m_computedFlags.set(flag);}
00225
00226
00227 void SetFlag(GoRegionFlag flag, bool value)
00228 { m_computedFlags.set(flag);
00229 m_flags.set(flag, value);
00230 }
00231
00232
00233 void Set1VCDepth(int depth) {m_1vcDepth = depth;}
00234
00235
00236
00237 bool Has2ConnForChains(const GoChain* c1, const GoChain* c2) const;
00238
00239
00240 bool Has2Conn() const;
00241
00242
00243 bool HealthyForSomeBlock(const SgVectorOf<GoBlock>& blocks) const;
00244
00245
00246 bool IsSurrounded(const SgVectorOf<GoBlock>& blocks) const;
00247
00248
00249 bool ComputedVitalForDepth(int depth) const;
00250
00251
00252 bool SomeBlockIsSafe() const;
00253
00254
00255 bool AllBlockIsSafe() const;
00256
00257
00258 bool AdjacentToBlock(SgPoint anchor) const;
00259
00260
00261 bool AdjacentToSomeBlock(const SgVector<SgPoint>& anchors) const;
00262
00263
00264 bool Safe2Cuts(const GoBoard& board) const;
00265
00266
00267
00268
00269 bool AllEmptyAreLibs() const;
00270
00271
00272
00273
00274
00275
00276
00277
00278 bool Has2SureLibs(SgMiaiStrategy* miaiStrategy) const;
00279
00280
00281 void InsideLibs(const GoBlock* b, SgVector<SgPoint>* libs) const;
00282
00283
00284 bool HasBlockLibs(const GoBlock* b) const;
00285
00286
00287 bool HasLibsForBlock(const GoBlock* b, int n) const;
00288
00289
00290 bool HasLibForAllBlocks() const;
00291
00292
00293 bool HasLibsForAllBlocks(int n) const;
00294
00295
00296
00297
00298
00299 bool Find2ConnForAll() const;
00300
00301
00302
00303
00304 bool Find2ConnForAllInterior(SgMiaiStrategy* miaiStrategy,
00305 SgVector<SgPoint>& usedLibs) const;
00306
00307
00308
00309
00310
00311 bool Has2IPs(const SgVector<SgPoint>& interiorEmpty, SgMiaiPair* ips)
00312 const;
00313
00314
00315
00316
00317 bool Has2IntersectionPoints(const SgVector<SgPoint>& usedLibs) const;
00318
00319
00320 void GetIPs(SgVector<SgPoint>* ips) const;
00321
00322
00323
00324
00325
00326
00327 void GetDivideMiaiPairs(SgVector<SgMiaiPair>& pairs) const;
00328
00329
00330
00331
00332 void JointLibs(SgVector<SgPoint>* libs) const;
00333
00334
00335 bool IsCorridor() const;
00336
00337
00338 bool ReplaceChain(const GoChain* old, const GoChain* newChain);
00339
00340
00341 bool Find2Mergable(GoChain** c1, GoChain** c2) const;
00342
00343
00344
00345
00346
00347 void Find2FreeLibs(const GoChain* c1, const GoChain* c2,
00348 SgPoint* lib1, SgPoint* lib2) const;
00349
00350
00351 void FindBlocks(const GoRegionBoard& ra);
00352
00353
00354 void SetBlocks(const SgVectorOf<GoBlock>& blocks);
00355
00356
00357
00358
00359 void FindChains(const GoRegionBoard& ra);
00360
00361
00362 void SetToSafe() {SetFlag(GO_REGION_SAFE, true);}
00363
00364
00365
00366 void RemoveBlock(const GoBlock* b);
00367
00368
00369 void OnAddStone(SgPoint p);
00370
00371
00372 void OnRemoveStone(SgPoint p);
00373
00374
00375 static void Fini();
00376
00377 private:
00378
00379
00380 const GoBoard& m_bd;
00381
00382
00383 GoRegionFlags m_flags;
00384
00385
00386 GoRegionFlags m_computedFlags;
00387
00388
00389 SgPointSet m_points;
00390
00391
00392 SgBlackWhite m_color;
00393
00394
00395 SgVectorOf<GoBlock> m_blocks;
00396
00397
00398 SgVectorOf<GoChain> m_chains;
00399
00400
00401 GoEyeCount m_eyes;
00402
00403
00404 SgPoint m_vitalPoint;
00405
00406
00407 int m_1vcDepth;
00408
00409
00410 SgMiaiStrategy m_miaiStrategy;
00411
00412
00413
00414
00415
00416
00417
00418
00419 bool ProtectedCuts(const GoBoard& board) const;
00420
00421
00422
00423
00424 bool ComputeIs1Vital() const;
00425
00426
00427 bool StaticIs1VitalAndConnected() const;
00428
00429
00430 void ComputeNakade();
00431
00432
00433 void ComputeSingleBlockEyeSpace();
00434
00435
00436 void ComputeMultipleBlockEyeSpace();
00437
00438
00439 void ComputeEyeSpace();
00440
00441
00442
00443
00444 void InteriorEmpty(SgVector<SgPoint>* interiorEmpty, int maxNu) const;
00445
00446 #ifndef NDEBUG
00447
00448 static int s_alloc, s_free;
00449 #endif
00450 };
00451
00452 std::ostream& operator<<(std::ostream& stream, const GoRegion& r);
00453
00454
00455
00456 #endif // GO_REGION_H
00457