Need a hint where to start with combining - c#

I,m new to this forum and hope to find a hint or answer to the next hypothetical problem:
There are 9 boxes. Three rows of three. I pick one as a start. From that start i have to combine it with three more boxes. The start box included so a total of 4. The combination has to be that the boxes are all connect to each other with at least one side. So a max combination/permutation search won't work i'll guess? Then i get combinations that are not allowed.
Just need a hint where/how to start.

Define the boxes and their spatial relationshiop as
0 1 2
3 4 5
6 7 8
With only 9 boxes, you'll have 9! / (5! * 4!) == 126 possible combinations.
You could enumerate them all and filter out the invalid ones.
given a combination (4 integers, each a box number)
it's invalid if it doesn't contain the box you picked randomly
it's invalid if any member isn't +/- 1 or +/3 of any other member

Related

C# : is there an appropriate collection for fast range-related search?

I have data like that:
Time(seconds from start)
Value
15
2
16
4
19
2
25
9
There are a lot of entries (10000+), and I need a way to find fast enough sum of any time range, like sum of range 16-25 seconds (which would be 4+2+9=15). This data will be dynamically changed many times (always adding new entries at the bottom of list).
I am thinking about using sorted list + binary search to determinate positions and just make sum of values, but is can took too much time to calculate it. Is there are any more appropriate way to do so? Nuget packets or algorithm references would be appreciated.
Just calculate cumulative sum:
Time Value CumulativeSum
15 2 2
16 4 6
19 2 8
25 9 17
Then for range [16,25] it will be task to binary search left border of 16 and 25 exact, which turns into 17 - 2 = 15
Complexity: O(log(n)), where n - size of the list.
Binary search implementation for lower/upper bound can be found in my repo - https://github.com/eocron/Algorithm/blob/master/Algorithm/Sorted/BinarySearchExtensions.cs

Algorithm to divide a list of numbers and summed up >= X million

I got 9 numbers which I want to divide in two lists, and both lists need to reach a certain amount when summed up. For example I got a list of ints:
List<int> test = new List<int>
{
1963000, 1963000, 393000, 86000,
393000, 393000, 176000, 420000,
3193000
};
And I want to have 2 lists of numbers that when you sum them up, they both reach over 4 million.
It doesn't matter if the 2 lists don't have the same amount of numbers. If it only takes 2 numbers to reach 4 million in 1 list, and 7 numbers together reaching 7 million, is fine.
As long as both lists summed up are equal to 4 million or higher.
Is this certain sum low enough to be reached easily?
If yes, then your algorithm may be as simple as: iterate i from 1 to number of items. sum up the first i numbers. if the sum is higher than your certain sum (eg 4 million), then you are finished, else increment i.
BUT: if your certain sums are high and it is not such trivial to find the partition, then you have the famous Partition Probem (https://en.wikipedia.org/wiki/Partition_problem), this is not that simple but there are some algorithms. Read this wikipedia artikle or try to google "Partition problem solution" or similar.

How to identify 5 numbers within 5% of each other out of 6 consecutive numbers

Using C# I have to:
Take an user entered number and decide if 5 out of 6 of these numbers are within 5% of each other. Also identify when it's impossible reach success before going to all 6 numbers. An example of this would be in the first 3 numbers... if 2 were not within 5% of each other, no use continuing.
I've tried taking in the first 2 numbers, into an array, and setting a Min and Max assuming they are within 5%. By the time I get to the 3rd and 4th numbers, which also can reset my Min and Max, it's turned into a bowl of spaghetti :)
Please help!

Using masked text box to validate input

Hi I am using the following mask string to ensure user enters a valid phone no
(99)-00000000
which should work fine for mobile as well as regional land line numbers in Australia
However I have encountered a problem that User can get away without entring all the digits
I understand that 9 represents an optional digit between 0 and 9 and 0 represents a required digit between 0 and 9
So how come if I dont enter all last eight mandatory digits the program still display results like
(03)-6474
(03)- 63799
(02)-1 38 390
Because it's simply filling in the blanks from left to right as you type. Even though the first two digits are optional, it fills them in like you've entered them with the first two numbers that you type.
If you dislike this behavior, you can easily subclass the MaskedTextBox control and customize how it handles keyboard input. Here are a couple of sample controls that cause the numbers to push in from right to left, as expected:
http://www.codeproject.com/KB/edit/Cool_Phone_Number_Box.aspx
http://www.dotnetheaven.com/Uploadfile/mgold/MaskedCurrencyTextBox02252006005553AM/MaskedCurrencyTextBox.aspx
(I think the second example does a better job of explaining exactly what changes are being made to the base control, plus it allows you to see most of the relevant code without downloading the sample project.)

How to manage AI actions based on percentages

I am looking now for some time about how can a programmer simulate a AI decision based on percentages of actions for the final fantasy tactic-like games (strategy game).
Say for example that the AI character has the following actions:
Attack 1: 10%
Attack 2: 9%
Magic : 4%
Move : 1%
All of this is far from equaling 100%
Now at first I though about having an array with 100 empty slots, attack would have 10 slots, attack 2 9 slots on the array. Combining random I could get the action to do then. My problem here is it is not really efficient, or doesn't seem to be. Also important thing, what do I do if I get on an empty slot. Do I have to calculate for each character all actions based on 100% or define maybe a "default" action for everyone ?
Or maybe there is a more efficient way to see all of this ? I think that percentage is the easiest way to implement an AI.
The best answer I can come up with is to make a list of all the possible moves you want the character to have, give each a relative value, then scale all of them to total 100%.
EDIT:
For example, here are three moves I have. I want attack and magic to be equally likely, and fleeing to be half as likely as attacking or using magic:
attack = 20
magic = 20
flee = 10
This adds up to 50, so dividing each by this total gives me a fractional value (multiply by 100 for percentage):
attack = 0.4
magic = 0.4
flee = 0.2
Then, I would make from this a list of cumulative values (i.e. each entry is a sum of that entry and all that came before it):
attack = 0.4
magic = 0.8
flee = 1
Now, generate a random number between 0 and 1 and find the first entry in the list that is greater than or equal to that number. That is the move you make.
No, you just create threshholds. One simple way is:
0 - 9 -> Attack1
10 - 18 -> Attack 2
19 - 22 -> Magic
23 -> Move
Something else -> 24-99 (you need to add up to 100)
Now create a random number and mod it by 100 (so num = randomNumber % 100) to define your action. The better the random number to close to a proper distribution you will get. So you take the result and see which category it falls into. You can actually make this even more efficient but it is a good start.
Well if they don't all add up to 100 they aren't really percentages. This doesnt matter though. you just need to figure out the relative probability of each action. To do this use the following formula...
prob = value_of_action / total_value_of_all_actions
This gives you a number between 0 and 1. if you really want a percentage rather than a fraction, multiply it by 100.
here is an example:
prob_attack = 10 / (10 + 9 + 4 + 1)
= 10 / 24
= 0.4167
This equates to attack being chosen 41.67% of the time.
you can then generate thresholds as is mentioned in other answers. And use a random number between 0 and 1 to choose your action.

Categories