Skip to content

Instantly share code, notes, and snippets.

@darkowlzz
Created August 25, 2012 09:23
Show Gist options
  • Save darkowlzz/3462877 to your computer and use it in GitHub Desktop.
Save darkowlzz/3462877 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pebblemerchant;
/**
*
* @author sunny
*/
public class PebbleMerchant {
public static int[ ] findCost(char[ ] direction,int[ ] length) {
int x=0, y=0;
int [][] l = new int[30][30];
l[0][0] = 0;
l[0][1] = 0;
int [] result = new int[2];
int face = 1;
for (int i=0; i<direction.length; i++) {
if (face == 1) {
if (direction[i]=='L') {
x -= length[i];
face = 4;
}
else if (direction[i]=='R') {
x += length[i];
face = 2;
}
}
else if (face == 2) {
if (direction[i]=='L') {
y += length[i];
face = 1;
}
else if (direction[i]=='R') {
y -= length[i];
face = 3;
}
}
else if (face == 3) {
if (direction[i]=='L') {
x += length[i];
face = 2;
}
else if (direction[i]=='R') {
x -= length[i];
face = 4;
}
}
else if (face == 4) {
if (direction[i]=='L') {
y -= length[i];
face = 3;
}
else if (direction[i]=='R') {
y += length[i];
face = 1;
}
}
l[i][0] = x;
l[i][1] = y;
}
int area = 0;
int x1, x2, y1, y2;
int cross, cost;
for (int i=0; i+1<direction.length; i++) {
x1 = l[i][0] - l[0][0];
y1 = l[i][1] - l[0][1];
x2 = l[i+1][0] - l[0][0];
y2 = l[i+1][1] - l[0][1];
cross = (x1*y2) - (x2*y1);
area += cross;
i += 1;
}
area = Math.abs(area);
cost = area*3*5;
result[0] = area;
result[1] = cost;
System.out.println("area: " + area);
System.out.println("cost: " + cost);
return result;
}
/**
* @param args the command line arguments
*/
/** public static void main(String[] args) {
//char[] direction = {'L', 'R', 'R', 'L', 'L', 'R', 'R', 'R', 'R', 'L', 'Z'};
//int[] length = {2,2,1,1,1,2,5,2,2,3,1};
char[] direction = {'L','L','R','R','L','R','R','L','R','L','R','R','L','R','L','R','R','L','Z'};
int[] length = {1,1,1,1,2,2,2,2,1,2,1,2,2,1,1,1,1,2,2};
int[] r = findCost(direction, length);
//System.out.println(r);
// TODO code application logic here
}
* */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment