I'm creating a minesweeper solver, but it's unable to solve this puzzle and nor can I figure out how to do it by hand.
Is this puzzle possible without guessing?
1 1 1 0
1 x 2 1
1 2 ? ?
0 1 ? ?
1 2 2 ?
x 2 ? ?
1 2 ? ?
There are 3 mines remaining.
Here are two possibilities I can up with:
1 1 1 0
1 x 2 1
1 2 x 1
0 1 1 1
1 2 2 2
x 2 x x
1 2 2 2
1 1 1 0
1 x 2 1
1 2 3 x
0 1 x 2
1 2 2 2
x 2 x 1
1 2 1 1
It is not possible without guessing. Here are two (of several) solved grids that match your example.
1 1 1 0
1 x 2 1
1 2 x 1
0 1 2 2
1 2 2 x
x 2 x 2
1 2 1 1
1 1 1 0
1 x 2 1
1 2 2 x
0 1 x 2
1 2 2 2
x 2 2 x
1 2 x 2
Related
I need the efficient algorithm for this problem (time comlexity less than O(n^2)), please help me:
a[i..j] is called a[i..j] < b[i..j] if a[i]<b[i], a[i+1]<b[i+1], ..., a[j]<b[j] after sorting these 2 arrays.
Given array A[1..n], (n<= 10^5, a[i]<= 1000). Find the maximum of k that A[1..k] < A[k+1..2k]
For example, n=10: 2 2 1 4 3 2 5 4 2 3
the answer is 4
Easily to see that k <= n/2. So we can use brute-forces (k from n/2 to 1), but not binary search.
And I don't know what to do with a[i] <= 1000. Maybe using map???
Use a Fenwick tree with range updates. Each index in the tree represents the count of how many numbers in window A are smaller than it. For the windows to be valid, each element in B (the window on the right) must have a partner in A (the window on the left). When we shift a number x into A, we add 1 to the range, [x+1, 1000] in the tree. For the element shifted from B to A, add 1 in its tree index. For each new element in B, add -1 to its index in the tree. If an index drops below zero, the window is invalid.
For the example, we have:
2 2 1 4 3 2 5 4 2 3
2 2
|
Tree:
add 1 to [3, 1000]
add -1 to 2
idx 1 2 3 4 5
val 0 -1 1 1 1 (invalid)
2 2 1 4 3 2 5 4 2 3
2 2 1 4
|
Tree:
add 1 to [3, 1000]
add 1 to 2 (remove 2 from B)
add -1 to 1
add -1 to 4
idx 1 2 3 4 5
val -1 0 2 1 2 (invalid)
2 2 1 4 3 2 5 4 2 3
2 2 1 4 3 2
|
Tree:
add 1 to [2, 1000]
add 1 to 1 (remove 1 from B)
add -1 to 3
add -1 to 2
idx 1 2 3 4 5
val 0 0 2 2 3 (valid)
2 2 1 4 3 2 5 4 2 3
2 2 1 4 3 2 5 4
|
Tree:
add 1 to [5, 1000]
add 1 to 4 (remove 4 from B)
add -1 to 5
add -1 to 4
idx 1 2 3 4 5
val 0 0 2 2 3 (valid)
2 2 1 4 3 2 5 4 2 3
2 2 1 4 3 2 5 4 2 3
|
Tree:
add 1 to [4, 1000]
add 1 to 3 (remove 3 from B)
add -1 to 2
add -1 to 3
idx 1 2 3 4 5
val 0 -1 2 3 4 (invalid)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
given G graph ' and a matrix of nodes , how can i find number of paths between 2 given nodes of a certain length ?
i've thought of multiping the matrix k times and then find the Ak[i,j] but i don't know to build the algorithm , or is it the best solution when it comes to complexity ?
If you want to find all the paths between two nodes of length k, just multiply the adjacency matrix by itself k times.
The reason for this is simple:
If there is an edge ij and an edge js, then there will be a path is through j. The entries ii are the degrees of the nodes i.
Here is an adjacency matrix for a graph:
0 1 1 0 0 0 0 0 0 0
0 1 1 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 0 0
1 0 0 1 1 0 0 0 0 0
0 0 0 0 1 1 1 0 1 0
1 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 1 0 1
0 0 0 0 0 0 0 1 0 1
0 0 0 1 0 0 0 0 0 1
Let's say we want to find the number of length 3 paths between Nodes 2 and 5. For this we need to find A_3[2, 5].
There are plenty of algorithms for matrix multiplication, and certain languages have these built in.
So if our adjancency matrix is called A, we want A * A * A.
This gives us:
2 1 1 2 3 2 1 1 1 0
2 2 2 2 3 2 1 2 1 1
2 1 1 1 3 2 3 3 3 1
2 2 2 2 4 3 3 3 3 1
1 1 1 1 1 1 3 8 3 6
0 1 1 2 1 1 0 1 0 2
0 0 0 2 0 0 1 5 1 6
1 0 0 3 1 0 0 1 0 3
1 0 0 3 1 0 0 1 0 3
2 1 1 3 3 1 1 0 1 1
When we find A_3[2, 5] we are given 2, which is the number of length 3 paths between the 2 nodes.
I am trying to implement LDA using Spark MLlib.
But I am having difficulty understanding input format. I was able to run its sample implementation to take input from a file which contains only number's as shown :
1 2 6 0 2 3 1 1 0 0 3
1 3 0 1 3 0 0 2 0 0 1
1 4 1 0 0 4 9 0 1 2 0
2 1 0 3 0 0 5 0 2 3 9
3 1 1 9 3 0 2 0 0 1 3
4 2 0 3 4 5 1 1 1 4 0
2 1 0 3 0 0 5 0 2 2 9
1 1 1 9 2 1 2 0 0 1 3
4 4 0 3 4 2 1 3 0 0 0
2 8 2 0 3 0 2 0 2 7 2
1 1 1 9 0 2 2 0 0 3 3
4 1 0 0 4 5 1 3 0 1 0
I followed
http://spark.apache.org/docs/latest/mllib-clustering.html#latent-dirichlet-allocation-lda
I understand the output format of this as explained here.
My use case is very simple, I have one data file with some sentences.
I want to convert this file into corpus so that to pass it to org.apache.spark.mllib.clustering.LDA.run().
My doubt is about what those numbers in input represent which is then zipWithIndex and passed to LDA? Is it like number 1 appearing everywhere represent same word or it is some kind of count?
First you need to convert your sentences into vectors.
val documents: RDD[Seq[String]] = sc.textFile("yourfile").map(_.split(" ").toSeq)
val hashingTF = new HashingTF()
val tf: RDD[Vector] = hashingTF.transform(documents)
val idf = new IDF().fit(tf)
val tfidf: RDD[Vector] = idf.transform(tf)
val corpus = tfidf.zipWithIndex.map(_.swap).cache()
// Cluster the documents into three topics using LDA
val ldaModel = new LDA().setK(3).run(corpus)
Read more about TF_IDF vectorization here
I need something like Position function for Mathematica (http://reference.wolfram.com/mathematica/ref/Position.html) but in Q. My solution for rectangular matrix is following:
q) colrow:{sz:count x; $[(count first x) = 1; enlist y; (floor y % sz; y mod sz)]}
q) position:{flip colrow[x;(where raze x = y)]}
It works straightforward for rectangular matrices and lists:
q) t:(1 -1 1; / matrix test
-1 3 4;
1 -1 1);
q) pos1:position[t;-1] / try to find all positions of -1
q) pos1
0 1
1 0
2 1
q) t ./: pos1 / here get items
-1 -1 -1
q) l:1 0 3 0 2 3 4 1 0 / list test
q) pos2:position[l;0] / try to find all positions of 0
q) pos2
1
3
8
q) l ./: pos2 / get items
0 0 0
This works but it'd be good to have more general solution for arbitrary lists and not only rectangular matrices. For instance code above won't work correctly for arguments like:
position[(1 2 3; 1 2; 1 2 1 4); 1]
May be someone has generic solution for that ?
How's this look? I think it should work for all two-dimensional lists, ragged or rectangular, and also for vectors. (I haven't worked out a version for arbitrary dimensions yet.)
q)position:{{$[type x;enlist each where x;raze flip each flip(til count x;raze each .z.s each x)]}x=y}
q)t
1 -1 1
-1 3 4
1 -1 1
q)l
1 0 3 0 2 3 4 1 0
q)r
1 2 3
1 2
1 2 1 4
q)pos1:position[t;-1]
q)pos2:position[l;0]
q)pos3:position[r;1]
q)pos1
0 1
1 0
2 1
q)pos2
1
3
8
q)pos3
0 0
1 0
2 0
2 2
q)t ./:pos1
-1 -1 -1
q)l ./:pos2
0 0 0
q)r ./:pos3
1 1 1 1
q)
EDIT:
Here's a version that works for all dimensions except 1 (and 0 of course):
q)position2:{{$[type x;where x;raze each raze flip each flip(til count x;.z.s each x)]}x=y}
q)r2:(r;r)
q)0N!r2;
((1 2 3;1 2;1 2 1 4);(1 2 3;1 2;1 2 1 4))
q)pos4:position2[r2;1]
q)0N!pos4;
(0 0 0;0 1 0;0 2 0;0 2 2;1 0 0;1 1 0;1 2 0;1 2 2)
q)r2 ./:pos4
1 1 1 1 1 1 1 1
q)r ./:position2[r;1]
1 1 1 1
q)t ./:position2[t;-1]
-1 -1 -1
q)
On vectors, though, it returns an address vector, not an address matrix, so it has to be used with #, not .:
q)0N!position2[l;0];
1 3 8
q)l ./:position2[l;0]
'type
q)l position2[l;0]
0 0 0
q)
If you really need it to work the same way on vectors as on higher-dimensional structures, the simplest solution is probably just to special-case them directly:
q)position3:{$[type x;enlist each where#;{$[type x;where x;raze each raze flip each flip(til count x;.z.s each x)]}]x=y}
q)position3[l;0]
1
3
8
q)l ./:position3[l;0]
0 0 0
q)r2 ./:position3[r2;1]
1 1 1 1 1 1 1 1
q)r ./:position3[r;1]
1 1 1 1
q)t ./:position3[t;-1]
-1 -1 -1
q)
Below should also work.
Not the exact solution but workable.
pos:{$[type x;where x=y;where each x=y]}
val:{raze ($[0h=type x;x#';x#])pos[x;y]}
q)t:(1 -1 1;-1 3 4;1 -1 1)
q)pos[t;-1]
1
0
1
q)val[t;-1]
-1
-1
-1
q)l:1 0 3 0 2 3 4 1 0
q)pos[l;0]
1 3 8
q)val[l;0]
0 0 0
q)r:(1 2 3; 1 2; 1 2 1 4)
q)pos[r;1]
,0
,0
0 2
q)val[r;1]
1 1 1 1
I am interested in how can I add rows and columns of zeros in a matrix so that it looks like this:
1 0 2 0 3
1 2 3 0 0 0 0 0
2 3 4 => 2 0 3 0 4
5 4 3 0 0 0 0 0
5 0 4 0 3
Actually I am interested in how can I do this efficiently, because walking the matrix and adding zeros takes a lot of time if you work with a big matrix.
Update:
Thank you very much.
Now I'm trying to replace the zeroes with the sum of their neighbors:
1 0 2 0 3 1 3 2 5 3
1 2 3 0 0 0 0 0 3 8 5 12... and so on
2 3 4 => 2 0 3 0 4 =>
5 4 3 0 0 0 0 0
5 0 4 0 3
as you can see i'm considering all the 8 neighbors of an element, but again using for and walking the matrix slows me down quite a bit, is there a faster way ?
Let your little matrix be called m1. Then:
m2 = zeros(5)
m2(1:2:end,1:2:end) = m1(:,:)
Obviously this is hard-wired to your example, I'll leave it to you to generalise.
Here are two ways to do part 2 of the question. The first does the shifts explicitly, and the second uses conv2. The second way should be faster.
M=[1 2 3; 2 3 4 ; 5 4 3];
% this matrix (M expanded) has zeros inserted, but also an extra row and column of zeros
Mex = kron(M,[1 0 ; 0 0 ]);
% The sum matrix is built from shifts of the original matrix
Msum = Mex + circshift(Mex,[1 0]) + ...
circshift(Mex,[-1 0]) +...
circshift(Mex,[0 -1]) + ...
circshift(Mex,[0 1]) + ...
circshift(Mex,[1 1]) + ...
circshift(Mex,[-1 1]) + ...
circshift(Mex,[1 -1]) + ...
circshift(Mex,[-1 -1]);
% trim the extra line
Msum = Msum(1:end-1,1:end-1)
% another version, a bit more fancy:
MexTrimmed = Mex(1:end-1,1:end-1);
MsumV2 = conv2(MexTrimmed,ones(3),'same')
Output:
Msum =
1 3 2 5 3
3 8 5 12 7
2 5 3 7 4
7 14 7 14 7
5 9 4 7 3
MsumV2 =
1 3 2 5 3
3 8 5 12 7
2 5 3 7 4
7 14 7 14 7
5 9 4 7 3