Skip to content

Instantly share code, notes, and snippets.

@torazuka
Created December 24, 2012 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torazuka/4368093 to your computer and use it in GitHub Desktop.
Save torazuka/4368093 to your computer and use it in GitHub Desktop.
オフラインリアルタイムどう書く第6回の参考問題(Java解答) ref: http://qiita.com/items/e61a4181d1e033ef2b4d
package sample6;
import static org.junit.Assert.assertEquals;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
/**
* 問題: http://nabetani.sakura.ne.jp/hena/ord6lintersection/
*/
public class Lintersection {
public String solve(String input) {
String[] split = input.split(",");
String[] first = split[0].split("-");
String[] second = split[1].split("-");
Set<String> firstL = getL(first);
Set<String> secondL = getL(second);
int counter = 0;
for (String string : secondL) {
boolean canAdd = firstL.add(string);
if (canAdd == false) {
counter++;
}
}
return String.valueOf(counter);
}
protected Set<String> getL(String[] points) {
int x0 = Integer.valueOf(String.valueOf(points[0].charAt(0)));
int y0 = Integer.valueOf(String.valueOf(points[0].charAt(1)));
int x1 = Integer.valueOf(String.valueOf(points[1].charAt(0)));
int y1 = Integer.valueOf(String.valueOf(points[1].charAt(1)));
Set<String> tmp = new HashSet<>();
Set<String> firstBlock = makeBlock(x0, y0, x1, y1, tmp);
int x2 = Integer.valueOf(String.valueOf(points[2].charAt(0)));
int y2 = Integer.valueOf(String.valueOf(points[2].charAt(1)));
Set<String> result = makeBlock(x2, y2, x0, y0, firstBlock);
return result;
}
protected Set<String> makeBlock(int x0, int y0, int x1, int y1,
Set<String> set) {
Set<String> result = new HashSet<>(set);
int maxX = x0 < x1 ? x1 : x0;
int minX = x0 < x1 ? x0 : x1;
int maxY = y0 < y1 ? y1 : y0;
int minY = y0 < y1 ? y0 : y1;
for (int i = minX; i < maxX + 1; i++) {
for (int k = minY; k < maxY + 1; k++) {
result.add(String.valueOf(i) + String.valueOf(k));
}
}
return result;
}
public static void main(String[] args) {
Lintersection li = new Lintersection();
System.out.println(li.solve("23-94-28,89-06-51"));
}
@Test
public void testSolve() throws Exception {
test("23-94-28,89-06-51", 11);
test("11-84-58,02-73-69", 40);
test("18-41-86,77-04-32", 26);
test("81-88-23,64-58-14", 0);
test("31-29-07,41-87-69", 0);
test("83-13-40,18-10-94", 1);
test("77-80-92,21-72-38", 2);
test("57-70-91,55-19-08", 3);
test("18-22-75,66-80-91", 4);
test("51-93-78,54-49-06", 5);
test("58-70-96,17-43-76", 6);
test("58-07-12,58-82-93", 7);
test("41-29-07,35-95-88", 8);
test("88-26-60,42-29-07", 9);
test("18-40-85,34-40-91", 10);
test("36-60-96,53-96-89", 11);
test("51-39-02,44-98-69", 12);
test("48-06-20,76-04-42", 13);
test("85-29-18,26-50-93", 14);
test("27-50-91,43-29-07", 15);
test("57-06-20,48-60-91", 16);
test("52-98-89,21-76-67", 17);
test("67-12-40,45-80-92", 18);
test("47-03-10,26-30-82", 19);
test("74-28-06,21-86-37", 20);
test("65-01-20,73-39-05", 21);
test("17-72-86,36-50-94", 22);
test("51-29-07,77-15-41", 23);
test("33-98-39,82-16-02", 24);
test("75-05-10,37-81-96", 25);
test("72-58-06,48-80-96", 26);
test("81-67-16,21-91-59", 27);
test("13-96-57,24-96-79", 28);
test("57-04-32,51-18-06", 29);
test("88-03-52,28-41-86", 30);
test("78-04-61,13-86-49", 31);
test("58-12-20,27-50-85", 32);
test("61-19-05,71-68-15", 33);
test("63-29-16,18-31-83", 34);
test("16-50-91,32-98-79", 35);
test("82-17-03,38-40-81", 36);
test("72-48-04,11-98-39", 37);
test("77-05-10,28-50-62", 38);
test("38-50-91,11-86-57", 39);
test("87-05-10,13-97-69", 40);
test("11-86-49,22-98-89", 44);
test("11-97-69,12-86-67", 46);
test("11-95-69,71-49-05", 47);
test("28-31-92,13-98-79", 48);
}
protected void test(String string, int i) {
Lintersection li = new Lintersection();
assertEquals(String.valueOf(i), li.solve(string));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment