Note that there are some explanatory texts on larger screens.

plurals
  1. POMemoization:Rememo
    primarykey
    data
    text
    <p>The array <code>memo[][]</code> is returning the cloth that is that is in <code>memo[X][Y]</code>. I have tested this class and it seems like it only stores the first cloth that fits in <code>memo[X][Y]</code>, but I want it to return the most valuable cloth that fits in <code>memo[X][Y]</code>. How can I do this?</p> <pre><code>class ClothCutter { static ArrayList &lt;Pattern&gt; patterns; //array of patterns to try static Cloth maxCloth; //the maximum cloth static Cloth memo[][]; // memo for maximum cloth (X,Y), memo[X][Y] int width; int height; //Constructor public ClothCutter(int w, int h,ArrayList&lt;Pattern&gt; p) { width = w; height = h; patterns = p; maxCloth = new Cloth(w,h); memo = new Cloth [w+1][h+1]; for (int i = 0; i&lt;patterns.size();i++) { Pattern z = patterns.get(i); System.out.println(z.name); Cloth m = new Cloth(z.width,z.height); m.add(z); memo[z.width][z.height]=m; System.out.println(memo[z.width][z.height].value); } } public Cloth optimize() { return optimize(maxCloth); } public Cloth optimize(Cloth c){ Cloth temp1 = new Cloth(); Cloth temp2 = new Cloth(); Cloth max = new Cloth(c.width,c.height);//temporary max if (memo[c.width][c.height]!=null) // return memo if there is one return memo[c.width][c.height]; if (c.width==0||c.height==0) //if (X||Y ==0) memo[c.width][c.height]=max; return max; } for (int i=0;i&lt;patterns.size();i++) { //for each pattern Pattern p = patterns.get(i); if (p.width&lt;=c.width &amp;&amp; p.height&lt;=c.height) { if (p.width&lt;c.width) { //if the pattern's width is less than the cloth's width Cloth a = new Cloth(c.width-p.width,c.height);//cut vertically a.pattern = p; Cloth b = new Cloth(p.width,c.height);//remainder after vertical cut b.pattern = p; temp2=optimize(b);//recurse temp1=optimize(a);//recurse } if (c.width==p.width) { //if the cloth's width is equal to a patterns with start cutting horizontally Cloth a = new Cloth(c.width,c.height-p.height);//horizontal cut a.pattern=p; Cloth b = new Cloth(c.width,p.height);//remainder after horizontal cut b.pattern = p; temp2=optimize(b);//recurse temp1=optimize(a);//recurse } if (temp1.value+temp2.value&gt;max.value) { //if the value of the optimal cloths is greater than the value of max max.add(temp1,temp2);//add the two cloths to max max.pattern = p; if (max.width == maxCloth.width &amp;&amp; max.height == maxCloth.height &amp;&amp; maxCloth.value &lt; max.value) // if the max dimentions is equal to maxCloth dimetions and the value of the maxCloth is less than max maxCloth=max;//set maxCloth to max } } } if (memo[max.width][max.height]==null) //if memo is equal to null memo[max.width][max.height]=max;// create memo return max; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload