@@ -15,6 +15,7 @@ public abstract class usacotools {
15
15
public static ArrayList <Exception > console =new ArrayList <Exception >();
16
16
public static String error ="Error" ;
17
17
public static int debugcode =-1 ;
18
+ public static boolean DEBUG =false ;
18
19
public static boolean isrect (int [][] map ,int x ,int y ) {
19
20
int cachedsize =-1 ;
20
21
int cachey =-1 ;
@@ -135,6 +136,24 @@ public static int binarySearch(int arr[], int l, int r, int x)
135
136
136
137
return -1 ;
137
138
}
139
+ public static int [][] copy2D (int [][] a ){
140
+ int [][] b =new int [a .length ][];
141
+ for (int i =0 ;i <a .length ;i ++) {
142
+ b [i ]=new int [a [i ].length ];
143
+ for (int j =0 ;j <a [i ].length ;j ++) {
144
+ b [i ][j ]=a [i ][j ];
145
+ }
146
+ }
147
+ return b ;
148
+ }
149
+ public static int [] copyarr (int [] a ) {
150
+ int [] b =new int [a .length ];
151
+
152
+ for (int i =0 ;i <a .length ;i ++) {
153
+ b [i ]=a [i ];
154
+ }
155
+ return b ;
156
+ }
138
157
public static int ebs (int arr [], int l , int r , int x ) {
139
158
Arrays .sort (arr );
140
159
return binarySearch (arr , l , r , x );
@@ -256,7 +275,7 @@ public static int[][] rotate90cw(int[][] map) {
256
275
* 90 degree clockwise
257
276
*/
258
277
int N =map .length ;
259
- int [][] n =new int [N ][N ];
278
+ int [][] n =new int [map [ 0 ]. length ][N ];
260
279
for (int i =0 ;i <N ;i ++) {
261
280
for (int j =0 ;j <N ;j ++) {
262
281
n [j ][N -1 -i ]=map [i ][j ];
@@ -327,6 +346,26 @@ public static void show2Darr(int[][] a) {
327
346
public static void showarr (int [] a ) {
328
347
for (int x :a ) {print (x +" " );}
329
348
}
349
+ public static int [][] dpcache ;
350
+ public static int ks (int W ,int [] wt ,int [] val ,int n ) {
351
+ int result ;
352
+ if (dpcache [n ][W ]!=0 ) {return dpcache [n ][W ];}
353
+ if (n ==0 ||W ==0 ) {
354
+ result =0 ;
355
+ }else if (wt [n -1 ]>W ) {
356
+ result =ks (W ,wt ,val ,n -1 );
357
+
358
+
359
+
360
+ }else {
361
+ result =Math .max (val [n -1 ]+ks (W -wt [n -1 ],wt ,val ,n -1 ),ks (W ,wt ,val ,n -1 ));
362
+ }
363
+ dpcache [n ][W ]=result ;
364
+ return result ;
365
+ }
366
+ public static void kssetup (int n ,int W ) {
367
+ dpcache =new int [n +1 ][W +1 ];
368
+ }
330
369
public static void main (String [] args ) throws Exception {
331
370
/*
332
371
* Short demo of stuff
@@ -339,26 +378,46 @@ public static void main(String[] args) throws Exception{
339
378
print ("Welcome to the demo" );
340
379
print (">" ,"" );
341
380
int val ;
381
+ /*
342
382
int[][] testarray= {
343
- {1 ,1 ,1 , 1 , 1 ,1 ,1 ,1 },
344
- {1 ,1 ,1 ,0 ,0 ,1 ,1 ,2 },
345
- {0 ,0 , 0 , 0 , 0 ,0 ,0 ,0 },
346
- {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 },
347
- {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 },
348
- {1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 },
349
- {1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 },
383
+ {1,1,2,7,7 ,1,1,1},
384
+ {1,1,4 ,0,7 ,1,2 ,2},
385
+ {0,3,6,9,1 ,0,0,0},
386
+ {0,3 ,0,1 ,0,0,0,0},
387
+ {0,3 ,0,0,0,0,0,0},
388
+ {1,1,5 ,1,3 ,1,1,1},
389
+ {1,1,1,1,3 ,1,1,1},
350
390
};
351
- print ("Roation of 90 degrees\n Before \n \n \n \n " );
391
+ */
392
+ int [][] testarray = {
393
+ {1 ,2 ,3 ,1 },
394
+ {4 ,5 ,6 ,2 },
395
+ {7 ,8 ,9 ,3 },
396
+ {10 ,69 ,1 ,4 }
397
+
398
+ };
399
+ print ("Roation of 90 degrees\n Before \n " );
352
400
show2Darr (testarray );
353
401
print ("After \n " );
354
402
show2Darr (rotate90cw (testarray ));
403
+ print ("BEFORE:" );
404
+ int [][] ii = {
405
+ {1 ,1 ,2 ,3 },
406
+ {1 ,0 ,2 ,1 },
407
+ {1 ,1 ,1 ,1 },
408
+ {1 ,2 ,3 ,4 }
409
+ };
410
+ show2Darr (ii );
411
+ print ("After H reflect:" );
412
+ show2Darr (reverseh (ii ));
413
+
355
414
try {
356
415
val =sc .nextInt ();
357
416
}catch (Exception e ) {
358
417
print ("Oops that did not go well please rerun and choose a INTEGER" );
359
418
val =-1 ;
360
419
report (e );
361
- print ("How about we test erro reporting" );
420
+ print ("How about we test error reporting" );
362
421
console ();
363
422
}
364
423
if (1 ==val ) {
0 commit comments