6
3
The Scenario
You are given a matrix of size m x n (width x height) with m*n spots where there are a few obstacles. Spots with obstacles are marked as 1, and those without are marked as 0. You can move vertically or horizontally, but can visit each spot only once. The goal is to cover as much area/spots as possible, avoiding obstacles.
Input
A 2D Matrix consisting of 0s and 1s and the coordinates of starting point.
Example Matrix1:
0(0,0) 1(0,1) 0(0,2)
0(1,0) 0(1,1) 0(1,2)
1(2,0) 0(2,1) 0(2,2)
Example Matrix2:
0(0,0) 0(0,1) 1(0,2) 0(0,3) 0(0,4)
0(1,0) 1(1,1) 0(1,2) 0(1,3) 0(1,4)
0(2,0) 0(2,1) 0(2,2) 1(2,3) 0(2,4)
0(3,0) 1(3,1) 0(3,2) 0(3,3) 0(3,4)
0(4,0) 0(4,1) 1(4,2) 0(4,3) 0(4,4)
The coordinates shown above, next to matrix elements is for explanation only. They are nothing but array indices. Actual input would just be the matrix. for example1 the input is just,
0 1 0
0 0 0
1 0 0
Input for example2,
0 0 1 0 0
0 1 0 0 0
0 0 0 1 0
0 1 0 0 0
0 0 1 0 0
Starting Point (0,0).
Output
Output should be a line containing comma separated moves. A move is a space separated direction (n,s,e,w) and number of units of movement in the direction (range 1-1000).
Example output for the matrix in example1 would be,
s 1,e 1,s 1,e 1,n 2
(0,0) --> (1,0) --> (1,1) --> (2,1) --> (2,2) --> (1,2) --> (0,2)
and for the matrix in example2 would be,
s 2,e 2,n 1,e 1,n 1,e 1,s 4,w 1,n 1,w 1
(0,0) --> (1,0) --> (2,0) --> (2,1) --> (2,2) --> (1,2) --> (1,3) --> (0,3) --> (0,4) --> (1,4) --> (2,4) --> (3,4) --> (4,4) --> (4,3) --> (3,3) --> (3,2)
which is the longest path, visiting each coordinate only once and avoiding those coordinates with obstacles.
Objective Winning Criteria
Should compute the distance efficiently(efficient: performance wise, fast program basically) for huge matrices (Of order say, 500x500). One which computes the fastest wins (All submissions run on my machine, for fairness). No restrictions on number of bytes!
Here is a 20x20 matrix which can be used to get some idea of the performace, for tuning purposes.
0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0
0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0
0 1 0 0 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0
2
Welcome to Programming Puzzles & Code Golf. Every challenge here needs an objective winning criterion, so its possible to determine a winner. In your case you should go with code-golf, so the solution with the fewest bytes of code wins. For future posts please check out the sandbox to get feedback first, before you post your challenge.
– Denker – 2016-03-12T14:26:41.850Edited. Thanks. Sorry! This was my first time here. – GeT_RiGhT – 2016-03-12T14:46:51.067
3If you want this to be reopened, you need to improve the spec a bit as well. Firstly you should add more testcases, one is not enough. Also you shold format them in a way that makes it easy to copy-paste them (the matrix could just be a 2D array. Furthermore you need to specify the expected output more. I guess `s,e,w,n' are used to indicate north, west, ... but you need to make it clear. Also it is generally discouraged to require a specific I/O format. Just let everyone choose the most convenient input format that fits the used language best. – Denker – 2016-03-12T14:56:29.723
1@xnor Really? I think limiting it to only orthogonal moves (and not allowing moves from any point to any other) should make this one a bit different? – Martin Ender – 2016-03-12T17:01:07.187
@MartinBüttner Oh, I totally misunderstood that challenge, my bad. – xnor – 2016-03-12T23:49:55.280
1I think this would be more interesting if output format was more flexible, for example
sesenn
. As it stands, a significant part of the efffort is formatting the output – Luis Mendo – 2016-03-13T16:14:47.903@DonMuesli Yes, you are right. Output can be as you said. It can even just be coordinates/array indices of points to be visited. – GeT_RiGhT – 2016-03-13T16:59:33.530
1If the winning criterion is running speed, you should remove the "code golf" tag. And you should define precisely how speed will be measured. For example, some answer may be the fastest for small matrices, and some other for large matrices. Which one wins then? – Luis Mendo – 2016-03-13T17:47:57.777
1Also posted on CS.SE. Please do not post the same question on multiple sites. Each community should have an honest shot at answering without anybody's time being wasted. Also, where did you encounter this problem? Please make sure to attribute your sources. Thank you! – D.W. – 2016-03-13T21:25:26.523
@DonMuesli I have already mentioned this in the winning criteria, but going to reiterate the one which computes the path for large matrices (largest being of the order 1000x1000) wins! – GeT_RiGhT – 2016-03-14T05:13:29.267
@D.W. My intention of posting it here was just to share this problem since I felt it is interesting and fun to solve! Wanted this problem to reach out to as many people as possible. The intention of posting it on CS.SE was purely for learning purposes. I wanted some advice on best algorithms to use for solving this. The problem might be the same but the intent with which it was posted was entirely different. Thats why different SE sites exist? Each with a different purpose? – GeT_RiGhT – 2016-03-14T05:20:04.783
1@xor I don't see how fastest for large matrix is fair - whoever has the biggest mainframe wins... what if I can't afford a mainframe? Shouldn't it be fastest on some specific computer (maybe yours)? That way the fastest algorithm wins instead of the most expensive computer. – Jerry Jeremiah – 2016-03-14T11:48:19.147
Are the outputs for the given test cases canonical? i.e. where (in case 2) there is an equivalent option, if the algorithm happens to choose a different but equally-covering route (that differs from the output in the question) is this valid? Or must the algorithm always be bias a certain way under equivalency choices? (and obviously, if so, how does this bias work/which way/etc?) – Tersosauros – 2016-03-14T18:53:18.590
Also, just out of interest... Is:
s 2,e 1,w 2,s 3,n 2,e 1,s 2,s 1,s 3,s 1,s 3,n 2,n 3,s 2,n 7,e 1,n 5,e 1,s 4,s 3,n 1,e 1,s 3,s 1,e 1,w 2,s 1,s 1,s 2,e 3,w 2,e 3,n 1,s 2,s 1,w 1,e 2,e 2,w 1,n 2,n 1,n 2,w 1,w 3,e 2,w 1,s 1,n 1,n 1,n 1,n 1,n 1,n 1,n 3,w 1,n 5,e 1,e 2,e 5,w 3,w 2,s 2,s 1,s 1,e 1,n 2,s 1,s 1,s 1,s 1,e 1,e 2,e 1,w 1,e 2,s 1,w 1,e 4,s 1,e 1,n 2,e 1,n 5,e 2,w 2,n 3,e 2,w 2,e 2,e 1,w 1,s 1,s 3,w 1,w 1,e 1,w 2,s 2,s 2,n 1,s 1,s 3,w 4,n 1
the correct path for the 20x20 matrix? ;P – Tersosauros – 2016-03-14T18:55:29.570@Tersosauros. Please, correct me if I'm wrong. I've summed up those values and it gave me 193. I did also a very simple algorithm (without any strategy for selecting directions) and the highest value was 219 check here. I think the output for the
– removed – 2016-03-15T01:10:13.90720x20 matriz
in the example should be some path with steps around 300... :/@Washington Guedes Your
– Tersosauros – 2016-03-25T00:26:14.603219
move solution skips22
accessible cells/nodes - see this diagram of your solution's path. "some path with steps around 300..." It is true that there are301
*unblocked* nodes. However, 43 of these are inaccessible (i.e. any [near-]optimum solution cannot traverse them). The largest path I've been able to find in the 20x20 matrix is258
. Obviously, the193
node path I suggest above is not optimum, as you have pointed out.