content: update another cache method, build

This commit is contained in:
2025-12-31 18:56:59 +08:00
parent df981921bf
commit 148c3b0179
5 changed files with 77 additions and 2 deletions

View File

@@ -176,6 +176,55 @@ If cache hit, it means such **sub question has no solution** and we should **not
>
> In other word, if any sub question is valid in our definition, it must also indicate the original question solution is also being found.
## More About Cache
At the beginning, I'm actually thinking about another way of recording cache. That is, when we **find a valid value for position `[i][j]`,** then if we **use this position as pyramid top to consider the sub-pyramid**, then **all the layer should be able to considered a valid sequence** as a sub question.
![[251231_another_way_of_cache.png]]
Use the image above as an example, if during the searching procedure, we **found that `D` as a valid color for position `[2][1]`,** then we should be able to ensure the **layers of the sub-pyramid are all valid sequences as sub-question**, that is:
```
seq1 = C, D
seq2 = E, F, A
seq3 = ...
```
**All these sequences should be consider valid**, since when any of them is considered as the bottom, we **could found at least a valid pyramid that satisfies the constrain of the question** simply based on the sub-pyramid *(the green and blue part in the image)* we found.
> [!note]
>
> In this case, if we **can not find any valid value for position `[1][2]`,** we **can NOT say** conversely that **any layer for the sub-pyramid is not valid**.
>
> This is easy to understand, actually we could ensure `C, D` is not a valid pyramid bottom, however, that doesn't mean `E, F, A` is not valid, since it may have other available sequence other than `C, D` as the upper layer of `E, F, A`
-----
I tried writing solution code using this thought, triggering cache but it turns out I got `TLE`. The basic code logic I implement is like below:
```python
# if we should trigger cache for current position for now?
def should_trigger_cache(curR, curC) -> bool:
# only trigger cache when a position is being searched
# at the first time in current sub solution space
if currChar != '0':
return False
# first search, but no valid value for current position
# then do not cache
if next_possible_char_for_this_pos() == '1':
return False
return True
# perform caching for all sequences of sub pyramid
def set_cache_for_sub_pyramid(curR, curC):
for string in get_sub_pyramid_layers_as_string():
cache.add(seq=string, value=True)
```
Personally I guess the cost of caching sub-pyramid for every position every time it's first searched in a sub solution space is too high, but I can't figure out a better approach lol
# Code Implementation
```cpp
@@ -550,4 +599,4 @@ public:
return found;
}
};
```
```