f1
stringlengths
6
6
f2
stringlengths
6
6
content_f1
stringlengths
149
20.2k
content_f2
stringlengths
149
20.2k
flag
int64
0
1
__index_level_0__
int64
0
3.83k
C20084
C20056
package google.codejam2012.qualification; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import static java.lang.Math.*; final class BinaryMatrix { private int[] data; public BinaryMatrix(int height, int width) { data = new int[height]; } public void set(int row, int col) { data[row] |= 1 << col; } public boolean isEmpty(int row, int col) { return (data[row] & (1 << col)) == 0; } } public strictfp class HallOfMirrors { static private double eps = 1e-8; static private int gcd(int a, int b) { while (a > 0 & b > 0) { if (b > 0) a %= b; if (a > 0) b %= a; } return a + b; } // - simple reflections checker static private boolean isSimpleReflectionVisible(BinaryMatrix hall, int row, int col, int dr, int dc, int dist) { dist -= 1; row += dr; col += dc; while(dist >= 0 & hall.isEmpty(row, col)) { row += dr; col += dc; dist -= 2; } return dist >= 0; } static private double euclideanDistance(double dx, double dy) { return sqrt(dx * dx + dy * dy); } static private boolean isReflectionVisible(BinaryMatrix hall, int row, int col, double distance, double ax, double ay) { int r = row; int c = col; double distPassed = 0.0; double x = c + 0.5; double y = r + 0.5; int dr = ay > 0 ? 1 : -1; int dc = ax > 0 ? 1 : -1; while (distPassed < distance + eps) { double nx = ax > 0 ? (int) (x + eps) + 1.0 : (int) (x - eps); double ny = ay > 0 ? (int) (y + eps) + 1.0 : (int) (y - eps); double scenario = abs((x - nx) / ax) - abs((y - ny) / ay); // - corner if (abs(scenario) < eps) { r += dr; c += dc; distPassed += euclideanDistance(x - nx, y - ny); x = nx; y = ny; if (!hall.isEmpty(r, c)) { if (!hall.isEmpty(r - dr, c) & !hall.isEmpty(r, c - dc)) { distPassed += distPassed; break; } // - x else if (!hall.isEmpty(r - dr, c)) { ax = -ax; dc = -dc; c += dc; } // - y else if (!hall.isEmpty(r, c - dc)) { ay = -ay; dr = -dr; r += dr; } else { distPassed += distance + eps; break; } } } // - x else if (scenario < 0) { c += dc; ny = y + abs((x - nx) * ay / ax) * dr; distPassed += euclideanDistance(x - nx, y - ny); x = nx; y = ny; // - reflect if (!hall.isEmpty(r, c)) { ax = -ax; dc = -dc; c += dc; } } // - y else { r += dr; nx = x + abs((y - ny) * ax / ay) * dc; distPassed += euclideanDistance(x - nx, y - ny); x = nx; y = ny; if (!hall.isEmpty(r, c)) { ay = -ay; dr = -dr; r += dr; } } // - check termination condition if (r == row & c == col) { double cx = c + 0.5; double cy = r + 0.5; if (abs((x - cx) / ax - (y - cy) / ay) < eps) { distPassed += euclideanDistance(cx - x, cy - y); break; } } } return distPassed < distance + eps; } static private int getVisibleReflectionsCount(BinaryMatrix hall, int row, int col, int distance) { int result = 0; // - check simple (up / down / left / right) reflections // -- up if (isSimpleReflectionVisible(hall, row, col, -1, 0, distance)) result++; // -- down if (isSimpleReflectionVisible(hall, row, col, 1, 0, distance)) result++; // -- left if (isSimpleReflectionVisible(hall, row, col, 0, -1, distance)) result++; // -- right if (isSimpleReflectionVisible(hall, row, col, 0, 1, distance)) result++; // - check other kinds of reflections using ray tracing method for (int ax = 1; ax <= 250; ax++) for (int ay = 1; ay <= 250; ay++) if (gcd(ax, ay) == 1) { if (isReflectionVisible(hall, row, col, distance, ax, ay)) result++; if (isReflectionVisible(hall, row, col, distance, -ax, ay)) result++; if (isReflectionVisible(hall, row, col, distance, ax, -ay)) result++; if (isReflectionVisible(hall, row, col, distance, -ax, -ay)) result++; } return result; } static public void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 64 << 10); int testsNumber = Integer.parseInt(br.readLine().trim()); for (int test = 1; test <= testsNumber; test++) { // - read test data StringTokenizer tokenizer = new StringTokenizer(br.readLine()); int height = Integer.parseInt(tokenizer.nextToken()); int width = Integer.parseInt(tokenizer.nextToken()); int distance = Integer.parseInt(tokenizer.nextToken()); BinaryMatrix hall = new BinaryMatrix(height, width); int initialRow = 0; int initialCol = 0; for (int i = 0; i < height; i++) { String s = br.readLine(); for (int j = 0; j < width; j++) { if (s.charAt(j) == '#') { hall.set(i, j); } else if (s.charAt(j) == 'X'){ initialRow = i; initialCol = j; } } } int result = getVisibleReflectionsCount(hall, initialRow, initialCol, distance); System.out.println("Case #" + test + ": " + result); } } catch (Exception e) { System.err.println("Error:" + e.getMessage()); } } }
package jp.funnything.competition.util; import java.math.BigDecimal; /** * Utility for BigDeciaml */ public class BD { public static BigDecimal ZERO = BigDecimal.ZERO; public static BigDecimal ONE = BigDecimal.ONE; public static BigDecimal add( final BigDecimal x , final BigDecimal y ) { return x.add( y ); } public static BigDecimal add( final BigDecimal x , final double y ) { return add( x , v( y ) ); } public static BigDecimal add( final double x , final BigDecimal y ) { return add( v( x ) , y ); } public static int cmp( final BigDecimal x , final BigDecimal y ) { return x.compareTo( y ); } public static int cmp( final BigDecimal x , final double y ) { return cmp( x , v( y ) ); } public static int cmp( final double x , final BigDecimal y ) { return cmp( v( x ) , y ); } public static BigDecimal div( final BigDecimal x , final BigDecimal y ) { return x.divide( y ); } public static BigDecimal div( final BigDecimal x , final double y ) { return div( x , v( y ) ); } public static BigDecimal div( final double x , final BigDecimal y ) { return div( v( x ) , y ); } public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) { return x.multiply( y ); } public static BigDecimal mul( final BigDecimal x , final double y ) { return mul( x , v( y ) ); } public static BigDecimal mul( final double x , final BigDecimal y ) { return mul( v( x ) , y ); } public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) { return x.subtract( y ); } public static BigDecimal sub( final BigDecimal x , final double y ) { return sub( x , v( y ) ); } public static BigDecimal sub( final double x , final BigDecimal y ) { return sub( v( x ) , y ); } public static BigDecimal v( final double value ) { return BigDecimal.valueOf( value ); } }
0
3,500
C20084
C20018
package google.codejam2012.qualification; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import static java.lang.Math.*; final class BinaryMatrix { private int[] data; public BinaryMatrix(int height, int width) { data = new int[height]; } public void set(int row, int col) { data[row] |= 1 << col; } public boolean isEmpty(int row, int col) { return (data[row] & (1 << col)) == 0; } } public strictfp class HallOfMirrors { static private double eps = 1e-8; static private int gcd(int a, int b) { while (a > 0 & b > 0) { if (b > 0) a %= b; if (a > 0) b %= a; } return a + b; } // - simple reflections checker static private boolean isSimpleReflectionVisible(BinaryMatrix hall, int row, int col, int dr, int dc, int dist) { dist -= 1; row += dr; col += dc; while(dist >= 0 & hall.isEmpty(row, col)) { row += dr; col += dc; dist -= 2; } return dist >= 0; } static private double euclideanDistance(double dx, double dy) { return sqrt(dx * dx + dy * dy); } static private boolean isReflectionVisible(BinaryMatrix hall, int row, int col, double distance, double ax, double ay) { int r = row; int c = col; double distPassed = 0.0; double x = c + 0.5; double y = r + 0.5; int dr = ay > 0 ? 1 : -1; int dc = ax > 0 ? 1 : -1; while (distPassed < distance + eps) { double nx = ax > 0 ? (int) (x + eps) + 1.0 : (int) (x - eps); double ny = ay > 0 ? (int) (y + eps) + 1.0 : (int) (y - eps); double scenario = abs((x - nx) / ax) - abs((y - ny) / ay); // - corner if (abs(scenario) < eps) { r += dr; c += dc; distPassed += euclideanDistance(x - nx, y - ny); x = nx; y = ny; if (!hall.isEmpty(r, c)) { if (!hall.isEmpty(r - dr, c) & !hall.isEmpty(r, c - dc)) { distPassed += distPassed; break; } // - x else if (!hall.isEmpty(r - dr, c)) { ax = -ax; dc = -dc; c += dc; } // - y else if (!hall.isEmpty(r, c - dc)) { ay = -ay; dr = -dr; r += dr; } else { distPassed += distance + eps; break; } } } // - x else if (scenario < 0) { c += dc; ny = y + abs((x - nx) * ay / ax) * dr; distPassed += euclideanDistance(x - nx, y - ny); x = nx; y = ny; // - reflect if (!hall.isEmpty(r, c)) { ax = -ax; dc = -dc; c += dc; } } // - y else { r += dr; nx = x + abs((y - ny) * ax / ay) * dc; distPassed += euclideanDistance(x - nx, y - ny); x = nx; y = ny; if (!hall.isEmpty(r, c)) { ay = -ay; dr = -dr; r += dr; } } // - check termination condition if (r == row & c == col) { double cx = c + 0.5; double cy = r + 0.5; if (abs((x - cx) / ax - (y - cy) / ay) < eps) { distPassed += euclideanDistance(cx - x, cy - y); break; } } } return distPassed < distance + eps; } static private int getVisibleReflectionsCount(BinaryMatrix hall, int row, int col, int distance) { int result = 0; // - check simple (up / down / left / right) reflections // -- up if (isSimpleReflectionVisible(hall, row, col, -1, 0, distance)) result++; // -- down if (isSimpleReflectionVisible(hall, row, col, 1, 0, distance)) result++; // -- left if (isSimpleReflectionVisible(hall, row, col, 0, -1, distance)) result++; // -- right if (isSimpleReflectionVisible(hall, row, col, 0, 1, distance)) result++; // - check other kinds of reflections using ray tracing method for (int ax = 1; ax <= 250; ax++) for (int ay = 1; ay <= 250; ay++) if (gcd(ax, ay) == 1) { if (isReflectionVisible(hall, row, col, distance, ax, ay)) result++; if (isReflectionVisible(hall, row, col, distance, -ax, ay)) result++; if (isReflectionVisible(hall, row, col, distance, ax, -ay)) result++; if (isReflectionVisible(hall, row, col, distance, -ax, -ay)) result++; } return result; } static public void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 64 << 10); int testsNumber = Integer.parseInt(br.readLine().trim()); for (int test = 1; test <= testsNumber; test++) { // - read test data StringTokenizer tokenizer = new StringTokenizer(br.readLine()); int height = Integer.parseInt(tokenizer.nextToken()); int width = Integer.parseInt(tokenizer.nextToken()); int distance = Integer.parseInt(tokenizer.nextToken()); BinaryMatrix hall = new BinaryMatrix(height, width); int initialRow = 0; int initialCol = 0; for (int i = 0; i < height; i++) { String s = br.readLine(); for (int j = 0; j < width; j++) { if (s.charAt(j) == '#') { hall.set(i, j); } else if (s.charAt(j) == 'X'){ initialRow = i; initialCol = j; } } } int result = getVisibleReflectionsCount(hall, initialRow, initialCol, distance); System.out.println("Case #" + test + ": " + result); } } catch (Exception e) { System.err.println("Error:" + e.getMessage()); } } }
package template; import java.util.Objects; public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{ private T1 o1; private T2 o2; public Pair(T1 o1, T2 o2) { this.o1 = o1; this.o2 = o2; } @Override public boolean equals(Object other) { if (!(other instanceof Pair)) {return false;} return (compareTo((Pair<T1, T2>)other) == 0); } @Override public int compareTo(Pair<T1, T2> other) { int c1 = ((Comparable<T1>) o1).compareTo(other.getO1()); if (c1 != 0) {return c1;} int c2 = ((Comparable<T2>) o2).compareTo(other.getO2()); return c2; } @Override public int hashCode() { int hash = 5; hash = 83 * hash + Objects.hashCode(this.o1); hash = 83 * hash + Objects.hashCode(this.o2); return hash; } @Override public String toString() { return "[" + o1 + ", " + o2 + "]"; } public T1 getO1() { return o1; } public void setO1(T1 o1) { this.o1 = o1; } public T2 getO2() { return o2; } public void setO2(T2 o2) { this.o2 = o2; } }
0
3,501
C20084
C20051
package google.codejam2012.qualification; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import static java.lang.Math.*; final class BinaryMatrix { private int[] data; public BinaryMatrix(int height, int width) { data = new int[height]; } public void set(int row, int col) { data[row] |= 1 << col; } public boolean isEmpty(int row, int col) { return (data[row] & (1 << col)) == 0; } } public strictfp class HallOfMirrors { static private double eps = 1e-8; static private int gcd(int a, int b) { while (a > 0 & b > 0) { if (b > 0) a %= b; if (a > 0) b %= a; } return a + b; } // - simple reflections checker static private boolean isSimpleReflectionVisible(BinaryMatrix hall, int row, int col, int dr, int dc, int dist) { dist -= 1; row += dr; col += dc; while(dist >= 0 & hall.isEmpty(row, col)) { row += dr; col += dc; dist -= 2; } return dist >= 0; } static private double euclideanDistance(double dx, double dy) { return sqrt(dx * dx + dy * dy); } static private boolean isReflectionVisible(BinaryMatrix hall, int row, int col, double distance, double ax, double ay) { int r = row; int c = col; double distPassed = 0.0; double x = c + 0.5; double y = r + 0.5; int dr = ay > 0 ? 1 : -1; int dc = ax > 0 ? 1 : -1; while (distPassed < distance + eps) { double nx = ax > 0 ? (int) (x + eps) + 1.0 : (int) (x - eps); double ny = ay > 0 ? (int) (y + eps) + 1.0 : (int) (y - eps); double scenario = abs((x - nx) / ax) - abs((y - ny) / ay); // - corner if (abs(scenario) < eps) { r += dr; c += dc; distPassed += euclideanDistance(x - nx, y - ny); x = nx; y = ny; if (!hall.isEmpty(r, c)) { if (!hall.isEmpty(r - dr, c) & !hall.isEmpty(r, c - dc)) { distPassed += distPassed; break; } // - x else if (!hall.isEmpty(r - dr, c)) { ax = -ax; dc = -dc; c += dc; } // - y else if (!hall.isEmpty(r, c - dc)) { ay = -ay; dr = -dr; r += dr; } else { distPassed += distance + eps; break; } } } // - x else if (scenario < 0) { c += dc; ny = y + abs((x - nx) * ay / ax) * dr; distPassed += euclideanDistance(x - nx, y - ny); x = nx; y = ny; // - reflect if (!hall.isEmpty(r, c)) { ax = -ax; dc = -dc; c += dc; } } // - y else { r += dr; nx = x + abs((y - ny) * ax / ay) * dc; distPassed += euclideanDistance(x - nx, y - ny); x = nx; y = ny; if (!hall.isEmpty(r, c)) { ay = -ay; dr = -dr; r += dr; } } // - check termination condition if (r == row & c == col) { double cx = c + 0.5; double cy = r + 0.5; if (abs((x - cx) / ax - (y - cy) / ay) < eps) { distPassed += euclideanDistance(cx - x, cy - y); break; } } } return distPassed < distance + eps; } static private int getVisibleReflectionsCount(BinaryMatrix hall, int row, int col, int distance) { int result = 0; // - check simple (up / down / left / right) reflections // -- up if (isSimpleReflectionVisible(hall, row, col, -1, 0, distance)) result++; // -- down if (isSimpleReflectionVisible(hall, row, col, 1, 0, distance)) result++; // -- left if (isSimpleReflectionVisible(hall, row, col, 0, -1, distance)) result++; // -- right if (isSimpleReflectionVisible(hall, row, col, 0, 1, distance)) result++; // - check other kinds of reflections using ray tracing method for (int ax = 1; ax <= 250; ax++) for (int ay = 1; ay <= 250; ay++) if (gcd(ax, ay) == 1) { if (isReflectionVisible(hall, row, col, distance, ax, ay)) result++; if (isReflectionVisible(hall, row, col, distance, -ax, ay)) result++; if (isReflectionVisible(hall, row, col, distance, ax, -ay)) result++; if (isReflectionVisible(hall, row, col, distance, -ax, -ay)) result++; } return result; } static public void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 64 << 10); int testsNumber = Integer.parseInt(br.readLine().trim()); for (int test = 1; test <= testsNumber; test++) { // - read test data StringTokenizer tokenizer = new StringTokenizer(br.readLine()); int height = Integer.parseInt(tokenizer.nextToken()); int width = Integer.parseInt(tokenizer.nextToken()); int distance = Integer.parseInt(tokenizer.nextToken()); BinaryMatrix hall = new BinaryMatrix(height, width); int initialRow = 0; int initialCol = 0; for (int i = 0; i < height; i++) { String s = br.readLine(); for (int j = 0; j < width; j++) { if (s.charAt(j) == '#') { hall.set(i, j); } else if (s.charAt(j) == 'X'){ initialRow = i; initialCol = j; } } } int result = getVisibleReflectionsCount(hall, initialRow, initialCol, distance); System.out.println("Case #" + test + ": " + result); } } catch (Exception e) { System.err.println("Error:" + e.getMessage()); } } }
package jp.funnything.competition.util; import java.util.List; import com.google.common.collect.Lists; public class Lists2 { public static < T > List< T > newArrayListAsArray( final int length ) { final List< T > list = Lists.newArrayListWithCapacity( length ); for ( int index = 0 ; index < length ; index++ ) { list.add( null ); } return list; } }
0
3,502
C20027
C20009
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
0
3,503
C20027
C20060
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
0
3,504
C20027
C20004
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
0
3,505
C20027
C20005
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package gcj; import com.sun.org.apache.xerces.internal.impl.xs.opti.DefaultXMLDocumentHandler; import java.util.*; import java.io.*; public class HallOfMirrors { final static String PROBLEM_NAME = "mirrors"; final static String WORK_DIR = "D:\\Gcj\\" + PROBLEM_NAME + "\\"; int H, W, D; double stX, stY; int res = 0; String[] map; int gcd(int a, int b) { while (a>0 && b>0) if (a>b) a %= b; else b %= a; return a + b; } final double EPS = 1e-10; double getT(double cur, int V) { cur *= 2; V *= 2; if (V == 0) return 1e100; if (V > 0) { double want = Math.ceil(cur + EPS); return (want - cur) / V; } else { double want = Math.floor(cur - EPS); return (want - cur) / V; } } boolean isInteger(double x) { return Math.abs(x - Math.floor(x)) <= EPS || Math.abs(x - Math.ceil(x)) <= EPS; } boolean inMirror(double x, double y) { int xx = (int)Math.floor(x); int yy = (int)Math.floor(y); return map[xx].charAt(yy) == '#'; } void process(int dx, int dy) { double curX = stX, curY = stY; double dist = 0.0; while (true) { double t1 = getT(curX, dx); double t2 = getT(curY, dy); double t = Math.min(t1, t2); double nextX = curX + t * dx; double nextY = curY + t * dy; double piece = Math.sqrt((nextX - curX) * (nextX - curX) + (nextY - curY) * (nextY - curY)); dist += piece; if (dist > D + EPS) return; if (Math.abs(nextX - stX) <= EPS && Math.abs(nextY - stY) <= EPS) { res++; return; } curX = nextX; curY = nextY; boolean fx = isInteger(nextX); boolean fy = isInteger(nextY); if (fx && fy) { // corner // A B // C D boolean A = inMirror(nextX - EPS, nextY - EPS); boolean B = inMirror(nextX - EPS, nextY + EPS); boolean C = inMirror(nextX + EPS, nextY - EPS); boolean D = inMirror(nextX + EPS, nextY + EPS); int cnt = (A ? 1 : 0) + (B ? 1 : 0) + (C ? 1 : 0) + (D ? 1 : 0); if (cnt == 3) { dx = -dx; dy = -dy; } else if ((A && B && !C && !D) || (!A && !B && C & D)) { dx = -dx; } else if ((A && C && !B && !D) || (!A && !C && B && D)) { dy = -dy; } else if (inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY + (dy > 0 ? EPS : -EPS))) { return; } else { // just continue; } } else if (fx && !fy) { // horizontal if (dx != 0 && inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY)) dx = -dx; } else if (!fx && fy) { // vertical if (dy != 0 && inMirror(nextX, nextY + (dy > 0 ? EPS : -EPS))) dy = -dy; } else { // nothing } } } void solve(Scanner sc, PrintWriter pw) { H = sc.nextInt(); W = sc.nextInt(); D = sc.nextInt(); map = new String[H]; for (int i=0; i<H; i++) map[i] = sc.next(); for (int i=0; i<H; i++) for (int j=0; j<W; j++) if (map[i].charAt(j) == 'X') { stX = i + 0.5; stY = j + 0.5; } for (int dx=-D; dx<=D; dx++) for (int dy=-D; dy<=D; dy++) if (dx*dx + dy*dy > 0 && dx*dx + dy*dy <= D * D && gcd(Math.abs(dx), Math.abs(dy)) == 1) { process(dx, dy); } pw.println(res); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new FileReader(WORK_DIR + "input.txt")); PrintWriter pw = new PrintWriter(new FileWriter(WORK_DIR + "output.txt")); int caseCnt = sc.nextInt(); for (int caseNum=0; caseNum<caseCnt; caseNum++) { System.out.println("Processing test case " + (caseNum + 1)); pw.print("Case #" + (caseNum+1) + ": "); new HallOfMirrors().solve(sc, pw); } pw.flush(); pw.close(); sc.close(); } }
0
3,506
C20027
C20065
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package jp.funnything.prototype; import static java.lang.Math.abs; import java.io.File; import java.io.IOException; import jp.funnything.competition.util.CompetitionIO; import jp.funnything.competition.util.Packer; import org.apache.commons.io.FileUtils; import org.apache.commons.math.fraction.Fraction; import org.apache.commons.math.util.MathUtils; public class Runner { public static void main( final String[] args ) throws Exception { new Runner().run(); } boolean isValid( final int d , final boolean[][] map , final int ox , final int oy , int dx , int dy ) { final Fraction fox = new Fraction( ox * 2 + 1 ); final Fraction foy = new Fraction( oy * 2 + 1 ); Fraction x = new Fraction( ox * 2 + 1 ); Fraction y = new Fraction( oy * 2 + 1 ); Fraction sumDiffX = new Fraction( 0 ); Fraction sumDiffY = new Fraction( 0 ); for ( ; ; ) { final Fraction diffX = new Fraction( dx > 0 ? ( int ) Math.floor( x.doubleValue() + 1 ) : ( int ) Math.ceil( x.doubleValue() - 1 ) ).subtract( x ); final Fraction diffY = new Fraction( dy > 0 ? ( int ) Math.floor( y.doubleValue() + 1 ) : ( int ) Math.ceil( y.doubleValue() - 1 ) ).subtract( y ); if ( abs( diffX.doubleValue() * dy ) < abs( diffY.doubleValue() * dx ) ) { x = x.add( diffX ); y = y.add( diffX.multiply( dy ).divide( dx ) ); sumDiffX = sumDiffX.add( diffX.abs() ); sumDiffY = sumDiffY.add( diffX.multiply( dy ).divide( dx ).abs() ); } else { y = y.add( diffY ); x = x.add( diffY.multiply( dx ).divide( dy ) ); sumDiffY = sumDiffY.add( diffY.abs() ); sumDiffX = sumDiffX.add( diffY.multiply( dx ).divide( dy ).abs() ); } if ( sumDiffX.multiply( sumDiffX ).add( sumDiffY.multiply( sumDiffY ) ).compareTo( new Fraction( d * d * 2 * 2 ) ) > 0 ) { return false; } if ( x.equals( fox ) && y.equals( foy ) ) { return true; } final int nx = x.intValue() / 2 + ( dx > 0 ? 0 : -1 ); final int ny = y.intValue() / 2 + ( dy > 0 ? 0 : -1 ); if ( x.getDenominator() == 1 && x.getNumerator() % 2 == 0 && y.getDenominator() == 1 && y.getNumerator() % 2 == 0 ) { if ( map[ ny ][ nx ] ) { final int px = x.intValue() / 2 + ( dx > 0 ? -1 : 0 ); final int py = y.intValue() / 2 + ( dy > 0 ? -1 : 0 ); if ( map[ py ][ nx ] ) { if ( map[ ny ][ px ] ) { dx = -dx; dy = -dy; } else { dx = -dx; } } else { if ( map[ ny ][ px ] ) { dy = -dy; } else { return false; } } } } else { if ( x.getDenominator() == 1 && x.getNumerator() % 2 == 0 ) { if ( map[ y.intValue() / 2 ][ nx ] ) { dx = -dx; } } else if ( y.getDenominator() == 1 && y.getNumerator() % 2 == 0 ) { if ( map[ ny ][ x.intValue() / 2 ] ) { dy = -dy; } } } } } void pack() { try { final File dist = new File( "dist" ); if ( dist.exists() ) { FileUtils.deleteQuietly( dist ); } final File workspace = new File( dist , "workspace" ); FileUtils.copyDirectory( new File( "src/main/java" ) , workspace ); FileUtils.copyDirectory( new File( "../../../../CompetitionUtil/Lib/src/main/java" ) , workspace ); Packer.pack( workspace , new File( dist , "sources.zip" ) ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } void run() throws Exception { final CompetitionIO io = new CompetitionIO(); final int t = io.readInt(); for ( int index = 0 ; index < t ; index++ ) { final int[] values = io.readInts(); final int h = values[ 0 ]; final int w = values[ 1 ]; final int d = values[ 2 ]; final char[][] map = new char[ h ][]; for ( int y = 0 ; y < h ; y++ ) { final char[] l = io.read().toCharArray(); if ( l.length != w ) { throw new RuntimeException( "assert" ); } map[ y ] = l; } io.write( index + 1 , solve( d , map ) ); } io.close(); pack(); } int solve( final int d , final char[][] map ) { int count = 0; int ox = -1; int oy = -1; final boolean[][] parsed = new boolean[ map.length ][]; for ( int y = 0 ; y < map.length ; y++ ) { parsed[ y ] = new boolean[ map[ y ].length ]; for ( int x = 0 ; x < map[ y ].length ; x++ ) { final char c = map[ y ][ x ]; if ( c == '#' ) { parsed[ y ][ x ] = true; } if ( c == 'X' ) { ox = x; oy = y; } } } for ( int dy = -d ; dy <= d ; dy++ ) { for ( int dx = -d ; dx <= d ; dx++ ) { if ( dx == 0 && dy == 0 ) { continue; } if ( MathUtils.gcd( dx , dy ) != 1 ) { continue; } if ( dx * dx + dy * dy > d * d ) { continue; } if ( isValid( d , parsed , ox , oy , dx , dy ) ) { count++; } } } return count; } }
0
3,507
C20027
C20006
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package problemD; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ProblemD { static Rational posX = null; static Rational posY = null; static Rational tarX = null; static Rational tarY = null; static boolean[][] array = null; public static void main(String[] args) throws FileNotFoundException { // Scanner sc = new Scanner(new File("D-practice.in")); // Scanner sc = new Scanner(new File("D-small.in")); Scanner sc = new Scanner(new File("D-large.in")); int cases = sc.nextInt(); for (int i = 1; i <= cases; i++) { // do case things here int H = sc.nextInt(); int W = sc.nextInt(); int D = sc.nextInt(); D *= 2; array = new boolean[H][W]; for (int j = 0; j < H; j++) { String s = sc.next(); for (int k = 0; k < W; k++) { array[j][k] = (s.charAt(k) == '#'); if (s.charAt(k) == 'X') { posX = new Rational(2 * k + 1, 1); posY = new Rational(2 * j + 1, 1); tarX = posX; tarY = posY; } } } int count = 0; boolean checked[][] = new boolean[2 * D + 1][2 * D + 1]; for (int j = 2; j <= D; j += 2) { for (int x = -j; x <= j; x += 2) { if (D + j >= 0 && D + j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + j][D + x]) { if (followRay(j, x, D)) { count++; } int k = 1; while (D + (k * j) >= 0 && D + (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * j)][D + (k * x)] = true; k++; } } } if (D + j >= 0 && D + j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + x][D + j]) { if (followRay(x, j, D)) { count++; } int k = 1; while (D + (k * j) >= 0 && D + (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * x)][D + (k * j)] = true; k++; } } } if (D - j >= 0 && D - j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D - j][D + x]) { if (followRay(-j, x, D)) { count++; } int k = 1; while (D - (k * j) >= 0 && D - (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D - (k * j)][D + (k * x)] = true; k++; } } } if (D - j >= 0 && D - j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + x][D - j]) { if (followRay(x, -j, D)) { count++; } int k = 1; while (D - (k * j) >= 0 && D - (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * x)][D - (k * j)] = true; k++; } } } } } // System.out.println(count); System.out.format("Case #%d: %d\n", i, count); } } private static boolean followRay(int dirX, int dirY, int max) { double distance = 0; posX = tarX; posY = tarY; distance += step(dirX, dirY); while (distance <= max) { if (tarX.equals(posX) && tarY.equals(posY)) { return true; } // check mirror, adjust direction if (dirX == 0) { if (posY.n == 1 && posY.z % 2 == 0) { int y = posY.z / 2; if (y >= array.length || y == 0) { return false; } int x = posX.z / posX.n / 2; if (dirY > 0 && array[y][x] || dirY < 0 && array[y - 1][x]) { dirY = -1 * dirY; } } } else if (dirY == 0) { if (posX.n == 1 && posX.z % 2 == 0) { int x = posX.z / 2; if (x >= array[0].length || x == 0) { return false; } int y = posY.z / posY.n / 2; if (dirX > 0 && array[y][x] || dirX < 0 && array[y][x - 1]) { dirX = -1 * dirX; } } } if (posX.n == 1 && posX.z % 2 == 0) { if (posY.n == 1 && posY.z % 2 == 0) { // corner int x = posX.z / 2; int y = posY.z / 2; boolean mirrored = false; if (dirX > 0) { if (array[y][x] && array[y - 1][x]) { dirX = -1 * dirX; mirrored = true; } } else { if (array[y][x - 1] && array[y - 1][x - 1]) { dirX = -1 * dirX; mirrored = true; } } if (dirY > 0) { if (array[y][x] && array[y][x - 1]) { dirY = -1 * dirY; mirrored = true; } } else { if (array[y - 1][x] && array[y - 1][x - 1]) { dirY = -1 * dirY; mirrored = true; } } if (!mirrored) { if (dirX > 0) { if (dirY > 0) { if (array[y][x]) { return false; } } else { if (array[y - 1][x]) { return false; } } } else { if (dirY > 0) { if (array[y][x - 1]) { return false; } } else { if (array[y - 1][x - 1]) { return false; } } } } } else { int x = posX.z / 2; if (x >= array[0].length || x == 0) { return false; } int y = posY.z / posY.n / 2; if (dirX > 0 && array[y][x] || dirX < 0 && array[y][x - 1]) { dirX = -1 * dirX; } } } else if (posY.n == 1 && posY.z % 2 == 0) { int y = posY.z / 2; if (y >= array.length || y == 0) { return false; } int x = posX.z / posX.n / 2; if (dirY > 0 && array[y][x] || dirY < 0 && array[y - 1][x]) { dirY = -1 * dirY; } } distance += step(dirX, dirY); } return false; } // steps until the next coord becomes integer private static double step(int dirX, int dirY) { if (dirY == 0) { if (dirX > 0) { posX = posX.plus(Rational.one); } else { posX = posX.minus(Rational.one); } return 1; } if (dirX == 0) { if (dirY > 0) { posY = posY.plus(Rational.one); } else { posY = posY.minus(Rational.one); } return 1; } if (posX.n == 1) { Rational distY = posY.fractal(); Rational speed = new Rational(Math.abs(dirY), Math.abs(dirX)); if (dirY > 0) { distY = Rational.one.minus(distY); } if (distY.equals(Rational.zero)) { distY = Rational.one; } if (distY.minus(speed).positive()) { if (dirX > 0) { posX = posX.plus(Rational.one); } else { posX = posX.minus(Rational.one); } if (dirY > 0) { posY = posY.plus(speed); } else { posY = posY.minus(speed); } return Math.sqrt(1 + speed.times(speed).value()); } else { if (dirY > 0) { posY = posY.plus(distY); } else { posY = posY.minus(distY); } Rational distX = distY.divides(speed); if (dirX > 0) { posX = posX.plus(distX); } else { posX = posX.minus(distX); } return Math.sqrt(distY.times(distY).value() + distX.times(distX).value()); } } else { Rational distX = posX.fractal(); Rational speed = new Rational(Math.abs(dirX), Math.abs(dirY)); if (dirX > 0) { distX = Rational.one.minus(distX); } if (distX.minus(speed).positive()) { if (dirY > 0) { posY = posY.plus(Rational.one); } else { posY = posY.minus(Rational.one); } if (dirX > 0) { posX = posX.plus(speed); } else { posX = posX.minus(speed); } return Math.sqrt(1 + speed.times(speed).value()); } else { if (dirX > 0) { posX = posX.plus(distX); } else { posX = posX.minus(distX); } Rational distY = distX.divides(speed); if (dirY > 0) { posY = posY.plus(distY); } else { posY = posY.minus(distY); } return Math.sqrt(distY.times(distY).value() + distX.times(distX).value()); } } } // private static void out(boolean[] array) { // System.out.println(Arrays.toString(array)); // } // // private static void out(boolean[][] array) { // int count = 0; // for (boolean[] a : array) { // System.out.print(count++ + ":"); // out(a); // } // } private static class Rational { static final Rational one = new Rational(1, 1); static final Rational zero = new Rational(0, 1); public int z; public int n; // create and initialize a new Rational object public Rational(int z, int n) { if (n == 0) { throw new RuntimeException("Denominator is zero"); } int g = gcd(z, n); this.z = z / g; this.n = n / g; } // return string representation of (this) public String toString() { if (n == 1) { return z + ""; } else { return z + "/" + n; } } // return (this * b) public Rational times(Rational b) { return new Rational(this.z * b.z, this.n * b.n); } // return (this + b) public Rational plus(Rational b) { int z = (this.z * b.n) + (this.n * b.z); int n = this.n * b.n; return new Rational(z, n); } // return (this - b) public Rational minus(Rational b) { int z = (this.z * b.n) - (this.n * b.z); int n = this.n * b.n; return new Rational(z, n); } // return fractal amount public Rational fractal() { return new Rational(z % n, n); } // return (1 / this) public Rational reciprocal() { return new Rational(n, z); } // return (this / b) public Rational divides(Rational b) { return this.times(b.reciprocal()); } public boolean positive() { return z * n >= 0; } public boolean equals(Rational r) { return r.z == this.z && r.n == this.n; } public double value() { return 1.0 * z / n; } private int gcd(int m, int n) { if (0 == n) return m; else return gcd(n, m % n); } } }
0
3,508
C20027
C20067
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
import java.io.*; import java.util.*; public class Solution { private StringTokenizer st; private BufferedReader in; private PrintWriter out; final String file = "D-large"; public void solve() throws IOException { int tests = nextInt(); for (int test = 0; test < tests; ++test) { int n = nextInt(); int m = nextInt(); int d = nextInt(); char[][] f = new char[n][m]; int x0 = -1, y0 = -1; for (int i = 0; i < n; ++i) { f[i] = next().toCharArray(); for (int j = 0; j < m; ++j) { if (f[i][j] == 'X') { x0 = i; y0 = j; } } } int ans = 0; for (int dx = -d; dx <= d; ++dx) { for (int dy = -d; dy <= d; ++dy) { if ((dx != 0 || dy != 0) && dx * dx + dy * dy <= d * d && raytrace(x0, y0, x0 + dx, y0 + dy, f, 0.)) { // System.err.println(dx + " " + dy); ans++; } } } System.err.printf("Case #%d: %d%n", test + 1, ans); out.printf("Case #%d: %d%n", test + 1, ans); } } final double EPS = 1e-8; private boolean raytrace(int x0, int y0, int x1, int y1, char[][] f, double t) { double firstt = Double.POSITIVE_INFINITY; int dx = x1 - x0; int dy = y1 - y0; double tx = Double.POSITIVE_INFINITY; for (int i = Math.max(0, Math.min(x0, x1)); i < f.length && i <= Math.max(x0, x1); ++i) { for (int j = Math.max(0, Math.min(y0, y1)); j < f[0].length && j <= Math.max(y0, y1); ++j) { if (f[i][j] == 'X') { double tt = dx != 0 ? (double)(i - x0) / dx : (double)(j - y0) / dy; if (Math.abs(x0 + dx * tt - i) < EPS && Math.abs(y0 + dy * tt - j) < EPS) { tx = tt; } } if (f[i][j] != '#') { continue; } double minx = dx == 0 ? Double.NEGATIVE_INFINITY : Math.min((i - 0.5 - x0) / dx, (i + 0.5 - x0) / dx); double maxx = dx == 0 ? Double.POSITIVE_INFINITY : Math.max((i - 0.5 - x0) / dx, (i + 0.5 - x0) / dx); double miny = dy == 0 ? Double.NEGATIVE_INFINITY : Math.min((j - 0.5 - y0) / dy, (j + 0.5 - y0) / dy); double maxy = dy == 0 ? Double.POSITIVE_INFINITY : Math.max((j - 0.5 - y0) / dy, (j + 0.5 - y0) / dy); if (maxx < miny - EPS || maxy < minx - EPS) { continue; } double tt = Math.max(minx, miny); if (tt > t + EPS) { firstt = Math.min(firstt, tt); } } } if (firstt > 1.) { return x1 >= 0 && x1 < f.length && y1 >= 0 && y1 < f[0].length && f[x1][y1] == 'X'; } if (tx > t + EPS && tx < firstt) { return false; } int sx = Integer.signum(dx); int sy = Integer.signum(dy); double x = x0 + dx * firstt - 0.5; double y = y0 + dy * firstt - 0.5; int rx = (int)Math.round(x); int ry = (int)Math.round(y); if (Math.abs(rx - x) < EPS && Math.abs(ry - y) < EPS) { if (dx == 0 || dy == 0) { throw new AssertionError(); } boolean f11 = get(f, rx + (1 + sx) / 2, ry + (1 + sy) / 2); boolean f01 = get(f, rx + (1 - sx) / 2, ry + (1 + sy) / 2); boolean f10 = get(f, rx + (1 + sx) / 2, ry + (1 - sy) / 2); if (get(f, rx + (1 - sx) / 2, ry + (1 - sy) / 2) || !f11 && !f01 && !f10) { throw new AssertionError(); } if (!f11) { return raytrace(x0, y0, x1, y1, f, firstt); } if (f01 && f10 && f11) { return raytrace(2 * rx + 1 - x0, 2 * ry + 1 - y0, 2 * rx + 1 - x1, 2 * ry + 1 - y1, f, firstt); } if (f10 && f11) { return raytrace(2 * rx + 1 - x0, y0, 2 * rx + 1 - x1, y1, f, firstt); } if (f01 && f11) { return raytrace(x0, 2 * ry + 1 - y0, x1, 2 * ry + 1 - y1, f, firstt); } return false; } if (Math.abs(rx - x) < EPS) { return raytrace(2 * rx + 1 - x0, y0, 2 * rx + 1 - x1, y1, f, firstt); } if (Math.abs(ry - y) < EPS) { return raytrace(x0, 2 * ry + 1 - y0, x1, 2 * ry + 1 - y1, f, firstt); } throw new AssertionError(); } private boolean get(char[][] f, int i, int j) { return i >= 0 && i < f.length && j >= 0 && j < f[0].length && f[i][j] == '#'; } public void run() throws IOException { in = new BufferedReader(new FileReader(file + ".in")); out = new PrintWriter(file + ".out"); eat(""); solve(); out.close(); } void eat(String s) { st = new StringTokenizer(s); } String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } eat(line); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); new Solution().run(); } }
0
3,509
C20027
C20008
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; public class SimpleMirrors { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader(args[0])); PrintWriter out = new PrintWriter(args[1]); // number of testcases String sCount = in.readLine(); int count = Integer.parseInt(sCount); for(int idx=1; idx<=count; idx++) { String[] parts = in.readLine().split(" "); int h = Integer.parseInt(parts[0]); int w = Integer.parseInt(parts[1]); int d = Integer.parseInt(parts[2]); int x = -1, y = -1; // small dataset => just find the "X" for(int i=0; i<h; i++) { String l = in.readLine(); int p = l.indexOf('X'); if(p > -1) { y = 10*i - 5; x = 10*p - 5; } } out.println("Case #" + idx + ": " + process(10*(h-2), 10*(w-2), 10*d, x, y)); } out.close(); } private static int process(int h, int w, int d, int ox, int oy) { System.out.println("h=" + h + ", w=" + w + ", d=" + d + ", ox=" + ox + ", oy=" + oy); MirrorSet ms = new MirrorSet( Arrays.asList(new Mirror[] { new Mirror(Facing.POS, Orient.HORIZ, 0, 0, w), new Mirror(Facing.NEG, Orient.HORIZ, h, 0, w), new Mirror(Facing.POS, Orient.VERT, 0, 0, h), new Mirror(Facing.NEG, Orient.VERT, w, 0, h), }), ox, oy, d, w, h ); Set<Point> pts = ms.transitiveImages(); Set<Double> angles = ms.angles(pts); System.out.println(" " + pts.size() + ": " + pts); System.out.println(" " + angles.size() + ": " + angles); return angles.size(); } private static class MirrorSet { private final Collection<Mirror> mirrors; private final int ox, oy, limit, w, h; public MirrorSet(Collection<Mirror> mirrors, int ox, int oy, int limit, int w, int h) { this.mirrors = mirrors; this.ox = ox; this.oy = oy; this.limit = limit; this.w = w; this.h = h; } public Set<Point> images(Set<Point> in) { HashSet<Point> out = new HashSet<Point>(); for(Point i : in) { for(Mirror m : mirrors) { Point o = m.image(i); if(o != null && ! in.contains(o) && Math.sqrt((ox-o.x)*(ox-o.x) + (oy-o.y)*(oy-o.y)) <= limit) out.add(o); } } return out; } public Set<Point> transitiveImages() { Set<Point> im = new HashSet<Point>(); im.add(new Point(ox, oy)); Set<Point> newer; do { newer = images(im); im.addAll(newer); } while(newer.size() > 0); return im; } public Set<Double> angles(Set<Point> in) { Set<Double> out = new HashSet<Double>(); for(Point i : in) { if(i.x != ox || i.y != oy) out.add(Math.atan2(i.y-oy, i.x-ox)); } return out; } } private static enum Facing { POS, NEG } private static enum Orient { VERT, HORIZ } private static class Mirror { private final Facing facing; private final Orient orient; private final int pos, start, stop; public Mirror(Facing facing, Orient orient, int pos, int start, int stop) { this.facing = facing; this.orient = orient; this.pos = pos; this.start = start; this.stop = stop; } public Point image(Point p) { switch(orient) { case VERT: if(facing == Facing.POS && p.x >= pos || facing == Facing.NEG && p.x <= pos) { return new Point(2 * pos - p.x, p.y); } else return null; case HORIZ: if(facing == Facing.POS && p.y >= pos || facing == Facing.NEG && p.y <= pos) { return new Point(p.x, 2 * pos - p.y); } else return null; } return null; } } private static class Point { public final int x, y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { return obj instanceof Point && ((Point)obj).x == x && ((Point)obj).y == y; } @Override public int hashCode() { return (x << 16) ^ y; } @Override public String toString() { return "(" + x + ", " + y + ")"; } } }
0
3,510
C20027
C20063
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
0
3,511
C20027
C20015
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
0
3,512
C20027
C20050
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,513
C20027
C20085
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
0
3,514
C20027
C20020
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
0
3,515
C20027
C20062
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,516
C20027
C20059
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
0
3,517
C20027
C20064
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
0
3,518
C20027
C20076
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
0
3,519
C20027
C20026
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
import java.io.*; import java.util.*; import java.math.*; class D { private static final boolean DEBUG_ON = true; private static final boolean ECHO_ON = true; private static BufferedReader input; private static BufferedWriter output; private static final int INF = Integer.MAX_VALUE / 2; private static final int MOD = 10007; private static int H, W, D, row, col; public static int gcd(int n, int m) {return (0 == m) ? (n) : gcd(m, n % m);} public static int sqrt(int X) { int answer = 1, interval = 1; for (int i = String.valueOf(X).length() / 2; i > 0; i--) {interval *= 10;} while (interval >= 1) { while ((answer * answer) <= X) {answer += interval;} answer -= interval; interval /= 10; } return answer; } public static void main(String[] args) { try { input = new BufferedReader(new FileReader(args[0] + ".in")); output = new BufferedWriter(new FileWriter(args[0] + ".out")); String line = input.readLine(); int testcases = getInt(line, 0); for (int testcase = 1; testcase <= testcases; testcase++) { char[][] real = getCharMatrix(input); HashSet<Integer> valid = new HashSet<Integer>(); for (int i = row - D; i <= row + D; i++) { int range = sqrt((D * D) - ((i - row) * (i - row))); for (int j = col - range; j <= col + range; j++) { int diffX = i - row; int diffY = j - col; if (0 == diffX && 0 == diffY) {continue;} int gcd = gcd(Math.abs(diffX), Math.abs(diffY)); int direction = (((diffX/gcd) + D) << 16) + ((diffY/gcd) + D); if (valid.contains(direction)) {continue;} int x = 100 * row + 50; int y = 100 * col + 50; for (int k = 0; k < 100; k++) { int nextX = x + diffX; int nextY = y + diffY; int xCell = x / 100; int yCell = y / 100; int nextXCell = nextX / 100; if (0 == nextX % 100) {nextXCell = (nextX + diffX) / 100;} int nextYCell = nextY / 100; if (0 == nextY % 100) {nextYCell = (nextY + diffY) / 100;} if (xCell == nextXCell && yCell == nextYCell) {x = nextX; y = nextY;} else if (xCell != nextXCell && yCell == nextYCell) { if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;} else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if (xCell == nextXCell && yCell != nextYCell) { if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;} else { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else { int cornerX = -1, cornerY = -1; if (nextXCell < xCell && nextYCell < yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * (nextYCell + 1);} else if (nextXCell > xCell && nextYCell < yCell) {cornerX = 100 * nextXCell; cornerY = 100 * (nextYCell + 1);} else if (nextXCell < xCell && nextYCell > yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * nextYCell;} else if (nextXCell > xCell && nextYCell > yCell) {cornerX = 100 * nextXCell; cornerY = 100 * nextYCell;} if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { diffX = -diffX; diffY = -diffY; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {x = nextX; y = nextY;} // passing corner else { diffX = -diffX; diffY = -diffY; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {break;} // hitting corner else if (Math.abs((cornerX - x) * (nextY - y)) < Math.abs((nextX - x) * (cornerY - y))) // hitting Y { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { if (Math.abs((cornerX - x) * (nextY - y)) <= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;} else { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { if (Math.abs((cornerX - x) * (nextY - y)) >= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;} else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) {x = nextX; y = nextY;} } if ((100 * row + 50) == x && (100 * col + 50) == y) {valid.add(direction); break;} } } } String result = "Case #" + testcase + ": " + valid.size(); output(result); } input.close(); output.close(); } catch (Exception e) { e.printStackTrace(); } } public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));} public static long getLong(String line, int index) {return Long.parseLong(getString(line, index));} public static double getDouble(String line, int index) {return Double.parseDouble(getString(line, index));} public static String getString(String line, int index) { line = line.trim(); while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;} if ((-1) == line.indexOf(' ')) {return line;} else {return line.substring(0, line.indexOf(' '));} } public static int[] getIntArray(String line) { String[] strings = getStringArray(line); int[] numbers = new int[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Integer.parseInt(strings[i]);} return numbers; } public static long[] getLongArray(String line) { String[] strings = getStringArray(line); long[] numbers = new long[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Long.parseLong(strings[i]);} return numbers; } public static double[] getDoubleArray(String line) { String[] strings = getStringArray(line); double[] numbers = new double[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Double.parseDouble(strings[i]);} return numbers; } public static String[] getStringArray(String line) {return line.trim().split("(\\s)+", 0);} public static int[] getIntArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); int[] numbers = new int[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Integer.parseInt(strings[i - begin]);} return numbers; } public static long[] getLongArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); long[] numbers = new long[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Long.parseLong(strings[i - begin]);} return numbers; } public static double[] getDoubleArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); double[] numbers = new double[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Double.parseDouble(strings[i - begin]);} return numbers; } public static String[] getStringArray(String line, int begin, int end) { String[] lines = line.trim().split("(\\s)+", 0); String[] results = new String[end - begin]; for (int i = begin; i < end; i++) {results[i - begin] = lines[i];} return results; } public static char[][] getCharMatrix(BufferedReader input) throws Exception { String line = input.readLine(); H = getInt(line, 0); W = getInt(line, 1); D = getInt(line, 2); char[][] matrix = new char[H][W]; for (int i = 0; i < H; i++) { line = input.readLine(); for (int j = 0; j < W; j++) { char c = matrix[i][j] = line.charAt(j); if ('X' == c) {row = i; col = j;} } } return matrix; } public static int[][] getIntMatrix(BufferedReader input) throws Exception { String line = input.readLine(); int R = getInt(line, 0); int C = getInt(line, 1); int[][] matrix = new int[R][C]; for (int i = 0; i < R; i++) { line = input.readLine(); for (int j = 0; j < C; j++) {matrix[i][j] = getInt(line, j);} } return matrix; } public static boolean[][] newBooleanMatrix(int R, int C, boolean value) { boolean[][] matrix = new boolean[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static char[][] newCharMatrix(int R, int C, char value) { char[][] matrix = new char[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static int[][] newIntMatrix(int R, int C, int value) { int[][] matrix = new int[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static long[][] newLongMatrix(int R, int C, long value) { long[][] matrix = new long[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static double[][] newDoubleMatrix(int R, int C, double value) { double[][] matrix = new double[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static void output(String s) throws Exception { if (ECHO_ON) {System.out.println(s);} output.write(s); output.newLine(); } public static String toKey(boolean[] array) { StringBuffer buffer = new StringBuffer(array.length + ","); for (int i = 0; i < array.length / 16; i++) { char c = 0; for (int j = 0; j < 16; j++) { c <<= 1; if (array[i * 16 + j]) {c += 1;} } buffer.append(c + ""); } char c = 0; for (int j = 0; j < (array.length % 16); j++) { c <<= 1; if (array[(array.length / 16) * 16 + j]) {c += 1;} } buffer.append(c + ""); return buffer.toString(); } public static String toKey(int[] array, int bit) { StringBuffer buffer = new StringBuffer(array.length + ","); if (bit > 16) { for (int i = 0; i < array.length; i++) { char c1 = (char)(array[i] >> 16); char c2 = (char)(array[i] & 0xFFFF); buffer.append("" + c1 + c2); } } else { int n = 16 / bit; for (int i = 0; i < array.length / n; i++) { char c = 0; for (int j = 0; j < n; j++) { c <<= bit; c += array[i * n + j]; } buffer.append(c + ""); } char c = 0; for (int j = 0; j < (array.length % n); j++) { c <<= bit; c += array[(array.length / n) * n + j]; } buffer.append(c + ""); } return buffer.toString(); } public static void debug(String s) {if (DEBUG_ON) {System.out.println(s);}} public static void debug(String s0, double l0) {if (DEBUG_ON) {System.out.println(s0+" = "+l0);}} public static void debug(String s0, double l0, String s1, double l1) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2) {if (DEBUG_ON) { System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3, String s4, double l4) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3+"; "+s4+" = "+l4);}} public static void debug(boolean[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(boolean[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append((array[i] == true ? "1" : "0") + separator);} buffer.append((array[array.length - 1] == true) ? "1" : "0"); System.out.println(buffer.toString()); } } public static void debug(boolean[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(boolean[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} public static void debug(char[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(char[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);} buffer.append(array[array.length - 1]); System.out.println(buffer.toString()); } } public static void debug(char[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(char[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} public static void debug(int[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(int[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);} buffer.append(array[array.length - 1]); System.out.println(buffer.toString()); } } public static void debug(int[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(int[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} }
0
3,520
C20027
C20078
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
import java.awt.Point; public class FracVec { public final Frac row, col; public FracVec(Frac row, Frac col) { this.row = row; this.col = col; } public FracVec(Point pos) { row = new Frac(pos.y); col = new Frac(pos.x); } public FracVec flipHoriz() { return new FracVec(row, col.neg()); } public FracVec flipVert() { return new FracVec(row.neg(), col); } public boolean isCorner() { return row.denom == 1 && col.denom == 1; } public FracVec add(FracVec other) { return new FracVec(row.add(other.row), col.add(other.col)); } public FracVec sub(FracVec other) { return new FracVec(row.sub(other.row), col.sub(other.col)); } public FracVec neg() { return new FracVec(row.neg(), col.neg()); } public boolean equals(FracVec other) { return row.equals(other.row) && col.equals(other.col); } public boolean equals(Point p) { return row.equals(p.y) && col.equals(p.x); } @Override public boolean equals(Object other) { return other instanceof FracVec && equals((FracVec) other); } @Override public int hashCode() { return (31 + row.hashCode()) * 31 + col.hashCode(); } public String toString() { return "( " + row + ", " + col + " )"; } public FracVec multiply(Frac scalar) { return new FracVec(row.mult(scalar), col.mult(scalar)); } public int compareLengthTo(int d) { return row.mult(row).add(col.mult(col)).compareTo(new Frac(d * d)); } public Point getNextCell(FracVec dm) { int resRow, resCol; if (dm.row.sgn() < 0) { resRow = row.roundDownAddSmallDown(); } else { resRow = row.roundDownAddSmallUp(); } if (dm.col.sgn() < 0) resCol = col.roundDownAddSmallDown(); else resCol = col.roundDownAddSmallUp(); return new Point(resCol, resRow); } }
0
3,521
C20027
C20057
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package jp.funnything.competition.util; import java.util.Arrays; import java.util.List; import com.google.common.collect.Lists; import com.google.common.primitives.Ints; import com.google.common.primitives.Longs; public class Prime { public static class PrimeData { public int[] list; public boolean[] map; private PrimeData( final int[] values , final boolean[] map ) { list = values; this.map = map; } } public static long[] factorize( long n , final int[] primes ) { final List< Long > factor = Lists.newArrayList(); for ( final int p : primes ) { if ( n < p * p ) { break; } while ( n % p == 0 ) { factor.add( ( long ) p ); n /= p; } } if ( n > 1 ) { factor.add( n ); } return Longs.toArray( factor ); } public static PrimeData prepare( final int n ) { final List< Integer > primes = Lists.newArrayList(); final boolean[] map = new boolean[ n ]; Arrays.fill( map , true ); map[ 0 ] = map[ 1 ] = false; primes.add( 2 ); for ( int composite = 2 * 2 ; composite < n ; composite += 2 ) { map[ composite ] = false; } for ( int value = 3 ; value < n ; value += 2 ) { if ( map[ value ] ) { primes.add( value ); for ( int composite = value * 2 ; composite < n ; composite += value ) { map[ composite ] = false; } } } return new PrimeData( Ints.toArray( primes ) , map ); } }
0
3,522
C20027
C20030
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; /** * * @author jim */ public class RayVector { public final int x; public final int y; public RayVector(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { if (obj instanceof RayVector) { RayVector v = (RayVector)obj; if (v.x * y == v.y * x) return true; } return false; } @Override public int hashCode() { int hash = 7; hash = 41 * hash + this.x; hash = 41 * hash + this.y; return hash; } public Fraction getScalarGardient() { return new Fraction(Math.abs(y), Math.abs(x)); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("("); sb.append(x); sb.append(","); sb.append(y); sb.append(")"); return sb.toString(); } }
0
3,523
C20027
C20002
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
import java.util.*; import java.io.*; import java.math.*; import java.awt.*; import static java.lang.Math.*; import static java.lang.Integer.parseInt; import static java.lang.Double.parseDouble; import static java.lang.Long.parseLong; import static java.lang.System.*; import static java.util.Arrays.*; import static java.util.Collection.*; public class D { static int gcd(int a, int b) { return b == 0 ? a : a == 0 ? b : gcd(b, a%b); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(in)); int T = parseInt(br.readLine()); for(int t = 0; t++ < T; ) { String[] line = br.readLine().split(" "); int H = parseInt(line[0]), W = parseInt(line[1]), D = parseInt(line[2]); char[][] G = new char[H][]; for(int h = 0; h < H; h++) G[h] = br.readLine().toCharArray(); int X = 0, Y = 0; outer:for(Y = 0; Y < H; Y++) for(X = 0; X < W; X++) if(G[Y][X] == 'X') break outer; int count = 0; for(int i = -D; i <= D; i++) { for(int j = -D; j <= D; j++) { int dx = i, dy = j, scale = 2 * Math.abs((dx == 0 ? 1 : dx) * (dy == 0 ? 1 : dy)), x0, y0, x, y; int steps = (int)Math.floor(scale * D / Math.sqrt(dx * dx + dy * dy)); if(gcd(Math.abs(dx), Math.abs(dy)) != 1) continue; x0 = x = X * scale + scale / 2; y0 = y = Y * scale + scale / 2; do { steps -= 1; if(x % scale == 0 && y % scale == 0) { // at a corner int dxi = dx > 0 ? 1 : -1, dyi = dy > 0 ? 1 : -1; int xi = (x / scale) - (dxi + 1) / 2, yi = (y / scale) - (dyi + 1) / 2; if(G[yi+dyi][xi+dxi] == '#') { if(G[yi+dyi][xi] != '#' && G[yi][xi+dxi] != '#') steps = -1; // kill the light if(G[yi+dyi][xi] == '#') dy *= -1; if(G[yi][xi+dxi] == '#') dx *= -1; } else ; // otherwise step as normal } else if(x % scale == 0) { int xi = x / scale, yi = y / scale; if(G[yi][xi] == '#' || G[yi][xi-1] == '#') dx *= -1; } else if(y % scale == 0) { int xi = x / scale, yi = y / scale; if(G[yi][xi] == '#' || G[yi-1][xi] == '#') dy *= -1; } else ; // smooth sailing x += dx; y += dy; } while(steps >= 0 && !(x == x0 && y == y0)); if(steps >= 0) ++count; } } out.println("Case #" + t +": " + count) ; } } }
0
3,524
C20027
C20056
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package jp.funnything.competition.util; import java.math.BigDecimal; /** * Utility for BigDeciaml */ public class BD { public static BigDecimal ZERO = BigDecimal.ZERO; public static BigDecimal ONE = BigDecimal.ONE; public static BigDecimal add( final BigDecimal x , final BigDecimal y ) { return x.add( y ); } public static BigDecimal add( final BigDecimal x , final double y ) { return add( x , v( y ) ); } public static BigDecimal add( final double x , final BigDecimal y ) { return add( v( x ) , y ); } public static int cmp( final BigDecimal x , final BigDecimal y ) { return x.compareTo( y ); } public static int cmp( final BigDecimal x , final double y ) { return cmp( x , v( y ) ); } public static int cmp( final double x , final BigDecimal y ) { return cmp( v( x ) , y ); } public static BigDecimal div( final BigDecimal x , final BigDecimal y ) { return x.divide( y ); } public static BigDecimal div( final BigDecimal x , final double y ) { return div( x , v( y ) ); } public static BigDecimal div( final double x , final BigDecimal y ) { return div( v( x ) , y ); } public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) { return x.multiply( y ); } public static BigDecimal mul( final BigDecimal x , final double y ) { return mul( x , v( y ) ); } public static BigDecimal mul( final double x , final BigDecimal y ) { return mul( v( x ) , y ); } public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) { return x.subtract( y ); } public static BigDecimal sub( final BigDecimal x , final double y ) { return sub( x , v( y ) ); } public static BigDecimal sub( final double x , final BigDecimal y ) { return sub( v( x ) , y ); } public static BigDecimal v( final double value ) { return BigDecimal.valueOf( value ); } }
0
3,525
C20027
C20018
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package template; import java.util.Objects; public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{ private T1 o1; private T2 o2; public Pair(T1 o1, T2 o2) { this.o1 = o1; this.o2 = o2; } @Override public boolean equals(Object other) { if (!(other instanceof Pair)) {return false;} return (compareTo((Pair<T1, T2>)other) == 0); } @Override public int compareTo(Pair<T1, T2> other) { int c1 = ((Comparable<T1>) o1).compareTo(other.getO1()); if (c1 != 0) {return c1;} int c2 = ((Comparable<T2>) o2).compareTo(other.getO2()); return c2; } @Override public int hashCode() { int hash = 5; hash = 83 * hash + Objects.hashCode(this.o1); hash = 83 * hash + Objects.hashCode(this.o2); return hash; } @Override public String toString() { return "[" + o1 + ", " + o2 + "]"; } public T1 getO1() { return o1; } public void setO1(T1 o1) { this.o1 = o1; } public T2 getO2() { return o2; } public void setO2(T2 o2) { this.o2 = o2; } }
0
3,526
C20027
C20051
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; import java.awt.Point; import java.util.ArrayList; import uk.co.epii.codejam.common.DatumConverter; /** * * @author jim */ public class HallFactory implements DatumConverter<Hall> { @Override public Hall getNext(ArrayList<String> list) { String[] specification = list.remove(0).split(" "); int H = Integer.parseInt(specification[0]); int W = Integer.parseInt(specification[1]); int D = Integer.parseInt(specification[2]); FractionPoint meLocation = null; Square[][] floor = new Square[H][]; for (int y = H - 1; y >= 0; y--) { floor[y] = new Square[W]; String line = list.remove(0); for (int x = 0; x < W; x++) { Square s = Square.parse(line.charAt(x)); if (s == Square.ME) meLocation = new FractionPoint( new Fraction(x, 1, 2), new Fraction(y, 1 , 2)); floor[y][x] = s; } } return new Hall(H, W, D, meLocation, floor); } }
package jp.funnything.competition.util; import java.util.List; import com.google.common.collect.Lists; public class Lists2 { public static < T > List< T > newArrayListAsArray( final int length ) { final List< T > list = Lists.newArrayListWithCapacity( length ); for ( int index = 0 ; index < length ; index++ ) { list.add( null ); } return list; } }
0
3,527
C20009
C20060
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
0
3,528
C20009
C20004
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
0
3,529
C20009
C20005
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package gcj; import com.sun.org.apache.xerces.internal.impl.xs.opti.DefaultXMLDocumentHandler; import java.util.*; import java.io.*; public class HallOfMirrors { final static String PROBLEM_NAME = "mirrors"; final static String WORK_DIR = "D:\\Gcj\\" + PROBLEM_NAME + "\\"; int H, W, D; double stX, stY; int res = 0; String[] map; int gcd(int a, int b) { while (a>0 && b>0) if (a>b) a %= b; else b %= a; return a + b; } final double EPS = 1e-10; double getT(double cur, int V) { cur *= 2; V *= 2; if (V == 0) return 1e100; if (V > 0) { double want = Math.ceil(cur + EPS); return (want - cur) / V; } else { double want = Math.floor(cur - EPS); return (want - cur) / V; } } boolean isInteger(double x) { return Math.abs(x - Math.floor(x)) <= EPS || Math.abs(x - Math.ceil(x)) <= EPS; } boolean inMirror(double x, double y) { int xx = (int)Math.floor(x); int yy = (int)Math.floor(y); return map[xx].charAt(yy) == '#'; } void process(int dx, int dy) { double curX = stX, curY = stY; double dist = 0.0; while (true) { double t1 = getT(curX, dx); double t2 = getT(curY, dy); double t = Math.min(t1, t2); double nextX = curX + t * dx; double nextY = curY + t * dy; double piece = Math.sqrt((nextX - curX) * (nextX - curX) + (nextY - curY) * (nextY - curY)); dist += piece; if (dist > D + EPS) return; if (Math.abs(nextX - stX) <= EPS && Math.abs(nextY - stY) <= EPS) { res++; return; } curX = nextX; curY = nextY; boolean fx = isInteger(nextX); boolean fy = isInteger(nextY); if (fx && fy) { // corner // A B // C D boolean A = inMirror(nextX - EPS, nextY - EPS); boolean B = inMirror(nextX - EPS, nextY + EPS); boolean C = inMirror(nextX + EPS, nextY - EPS); boolean D = inMirror(nextX + EPS, nextY + EPS); int cnt = (A ? 1 : 0) + (B ? 1 : 0) + (C ? 1 : 0) + (D ? 1 : 0); if (cnt == 3) { dx = -dx; dy = -dy; } else if ((A && B && !C && !D) || (!A && !B && C & D)) { dx = -dx; } else if ((A && C && !B && !D) || (!A && !C && B && D)) { dy = -dy; } else if (inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY + (dy > 0 ? EPS : -EPS))) { return; } else { // just continue; } } else if (fx && !fy) { // horizontal if (dx != 0 && inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY)) dx = -dx; } else if (!fx && fy) { // vertical if (dy != 0 && inMirror(nextX, nextY + (dy > 0 ? EPS : -EPS))) dy = -dy; } else { // nothing } } } void solve(Scanner sc, PrintWriter pw) { H = sc.nextInt(); W = sc.nextInt(); D = sc.nextInt(); map = new String[H]; for (int i=0; i<H; i++) map[i] = sc.next(); for (int i=0; i<H; i++) for (int j=0; j<W; j++) if (map[i].charAt(j) == 'X') { stX = i + 0.5; stY = j + 0.5; } for (int dx=-D; dx<=D; dx++) for (int dy=-D; dy<=D; dy++) if (dx*dx + dy*dy > 0 && dx*dx + dy*dy <= D * D && gcd(Math.abs(dx), Math.abs(dy)) == 1) { process(dx, dy); } pw.println(res); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new FileReader(WORK_DIR + "input.txt")); PrintWriter pw = new PrintWriter(new FileWriter(WORK_DIR + "output.txt")); int caseCnt = sc.nextInt(); for (int caseNum=0; caseNum<caseCnt; caseNum++) { System.out.println("Processing test case " + (caseNum + 1)); pw.print("Case #" + (caseNum+1) + ": "); new HallOfMirrors().solve(sc, pw); } pw.flush(); pw.close(); sc.close(); } }
0
3,530
C20009
C20065
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package jp.funnything.prototype; import static java.lang.Math.abs; import java.io.File; import java.io.IOException; import jp.funnything.competition.util.CompetitionIO; import jp.funnything.competition.util.Packer; import org.apache.commons.io.FileUtils; import org.apache.commons.math.fraction.Fraction; import org.apache.commons.math.util.MathUtils; public class Runner { public static void main( final String[] args ) throws Exception { new Runner().run(); } boolean isValid( final int d , final boolean[][] map , final int ox , final int oy , int dx , int dy ) { final Fraction fox = new Fraction( ox * 2 + 1 ); final Fraction foy = new Fraction( oy * 2 + 1 ); Fraction x = new Fraction( ox * 2 + 1 ); Fraction y = new Fraction( oy * 2 + 1 ); Fraction sumDiffX = new Fraction( 0 ); Fraction sumDiffY = new Fraction( 0 ); for ( ; ; ) { final Fraction diffX = new Fraction( dx > 0 ? ( int ) Math.floor( x.doubleValue() + 1 ) : ( int ) Math.ceil( x.doubleValue() - 1 ) ).subtract( x ); final Fraction diffY = new Fraction( dy > 0 ? ( int ) Math.floor( y.doubleValue() + 1 ) : ( int ) Math.ceil( y.doubleValue() - 1 ) ).subtract( y ); if ( abs( diffX.doubleValue() * dy ) < abs( diffY.doubleValue() * dx ) ) { x = x.add( diffX ); y = y.add( diffX.multiply( dy ).divide( dx ) ); sumDiffX = sumDiffX.add( diffX.abs() ); sumDiffY = sumDiffY.add( diffX.multiply( dy ).divide( dx ).abs() ); } else { y = y.add( diffY ); x = x.add( diffY.multiply( dx ).divide( dy ) ); sumDiffY = sumDiffY.add( diffY.abs() ); sumDiffX = sumDiffX.add( diffY.multiply( dx ).divide( dy ).abs() ); } if ( sumDiffX.multiply( sumDiffX ).add( sumDiffY.multiply( sumDiffY ) ).compareTo( new Fraction( d * d * 2 * 2 ) ) > 0 ) { return false; } if ( x.equals( fox ) && y.equals( foy ) ) { return true; } final int nx = x.intValue() / 2 + ( dx > 0 ? 0 : -1 ); final int ny = y.intValue() / 2 + ( dy > 0 ? 0 : -1 ); if ( x.getDenominator() == 1 && x.getNumerator() % 2 == 0 && y.getDenominator() == 1 && y.getNumerator() % 2 == 0 ) { if ( map[ ny ][ nx ] ) { final int px = x.intValue() / 2 + ( dx > 0 ? -1 : 0 ); final int py = y.intValue() / 2 + ( dy > 0 ? -1 : 0 ); if ( map[ py ][ nx ] ) { if ( map[ ny ][ px ] ) { dx = -dx; dy = -dy; } else { dx = -dx; } } else { if ( map[ ny ][ px ] ) { dy = -dy; } else { return false; } } } } else { if ( x.getDenominator() == 1 && x.getNumerator() % 2 == 0 ) { if ( map[ y.intValue() / 2 ][ nx ] ) { dx = -dx; } } else if ( y.getDenominator() == 1 && y.getNumerator() % 2 == 0 ) { if ( map[ ny ][ x.intValue() / 2 ] ) { dy = -dy; } } } } } void pack() { try { final File dist = new File( "dist" ); if ( dist.exists() ) { FileUtils.deleteQuietly( dist ); } final File workspace = new File( dist , "workspace" ); FileUtils.copyDirectory( new File( "src/main/java" ) , workspace ); FileUtils.copyDirectory( new File( "../../../../CompetitionUtil/Lib/src/main/java" ) , workspace ); Packer.pack( workspace , new File( dist , "sources.zip" ) ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } void run() throws Exception { final CompetitionIO io = new CompetitionIO(); final int t = io.readInt(); for ( int index = 0 ; index < t ; index++ ) { final int[] values = io.readInts(); final int h = values[ 0 ]; final int w = values[ 1 ]; final int d = values[ 2 ]; final char[][] map = new char[ h ][]; for ( int y = 0 ; y < h ; y++ ) { final char[] l = io.read().toCharArray(); if ( l.length != w ) { throw new RuntimeException( "assert" ); } map[ y ] = l; } io.write( index + 1 , solve( d , map ) ); } io.close(); pack(); } int solve( final int d , final char[][] map ) { int count = 0; int ox = -1; int oy = -1; final boolean[][] parsed = new boolean[ map.length ][]; for ( int y = 0 ; y < map.length ; y++ ) { parsed[ y ] = new boolean[ map[ y ].length ]; for ( int x = 0 ; x < map[ y ].length ; x++ ) { final char c = map[ y ][ x ]; if ( c == '#' ) { parsed[ y ][ x ] = true; } if ( c == 'X' ) { ox = x; oy = y; } } } for ( int dy = -d ; dy <= d ; dy++ ) { for ( int dx = -d ; dx <= d ; dx++ ) { if ( dx == 0 && dy == 0 ) { continue; } if ( MathUtils.gcd( dx , dy ) != 1 ) { continue; } if ( dx * dx + dy * dy > d * d ) { continue; } if ( isValid( d , parsed , ox , oy , dx , dy ) ) { count++; } } } return count; } }
0
3,531
C20009
C20006
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package problemD; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ProblemD { static Rational posX = null; static Rational posY = null; static Rational tarX = null; static Rational tarY = null; static boolean[][] array = null; public static void main(String[] args) throws FileNotFoundException { // Scanner sc = new Scanner(new File("D-practice.in")); // Scanner sc = new Scanner(new File("D-small.in")); Scanner sc = new Scanner(new File("D-large.in")); int cases = sc.nextInt(); for (int i = 1; i <= cases; i++) { // do case things here int H = sc.nextInt(); int W = sc.nextInt(); int D = sc.nextInt(); D *= 2; array = new boolean[H][W]; for (int j = 0; j < H; j++) { String s = sc.next(); for (int k = 0; k < W; k++) { array[j][k] = (s.charAt(k) == '#'); if (s.charAt(k) == 'X') { posX = new Rational(2 * k + 1, 1); posY = new Rational(2 * j + 1, 1); tarX = posX; tarY = posY; } } } int count = 0; boolean checked[][] = new boolean[2 * D + 1][2 * D + 1]; for (int j = 2; j <= D; j += 2) { for (int x = -j; x <= j; x += 2) { if (D + j >= 0 && D + j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + j][D + x]) { if (followRay(j, x, D)) { count++; } int k = 1; while (D + (k * j) >= 0 && D + (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * j)][D + (k * x)] = true; k++; } } } if (D + j >= 0 && D + j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + x][D + j]) { if (followRay(x, j, D)) { count++; } int k = 1; while (D + (k * j) >= 0 && D + (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * x)][D + (k * j)] = true; k++; } } } if (D - j >= 0 && D - j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D - j][D + x]) { if (followRay(-j, x, D)) { count++; } int k = 1; while (D - (k * j) >= 0 && D - (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D - (k * j)][D + (k * x)] = true; k++; } } } if (D - j >= 0 && D - j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + x][D - j]) { if (followRay(x, -j, D)) { count++; } int k = 1; while (D - (k * j) >= 0 && D - (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * x)][D - (k * j)] = true; k++; } } } } } // System.out.println(count); System.out.format("Case #%d: %d\n", i, count); } } private static boolean followRay(int dirX, int dirY, int max) { double distance = 0; posX = tarX; posY = tarY; distance += step(dirX, dirY); while (distance <= max) { if (tarX.equals(posX) && tarY.equals(posY)) { return true; } // check mirror, adjust direction if (dirX == 0) { if (posY.n == 1 && posY.z % 2 == 0) { int y = posY.z / 2; if (y >= array.length || y == 0) { return false; } int x = posX.z / posX.n / 2; if (dirY > 0 && array[y][x] || dirY < 0 && array[y - 1][x]) { dirY = -1 * dirY; } } } else if (dirY == 0) { if (posX.n == 1 && posX.z % 2 == 0) { int x = posX.z / 2; if (x >= array[0].length || x == 0) { return false; } int y = posY.z / posY.n / 2; if (dirX > 0 && array[y][x] || dirX < 0 && array[y][x - 1]) { dirX = -1 * dirX; } } } if (posX.n == 1 && posX.z % 2 == 0) { if (posY.n == 1 && posY.z % 2 == 0) { // corner int x = posX.z / 2; int y = posY.z / 2; boolean mirrored = false; if (dirX > 0) { if (array[y][x] && array[y - 1][x]) { dirX = -1 * dirX; mirrored = true; } } else { if (array[y][x - 1] && array[y - 1][x - 1]) { dirX = -1 * dirX; mirrored = true; } } if (dirY > 0) { if (array[y][x] && array[y][x - 1]) { dirY = -1 * dirY; mirrored = true; } } else { if (array[y - 1][x] && array[y - 1][x - 1]) { dirY = -1 * dirY; mirrored = true; } } if (!mirrored) { if (dirX > 0) { if (dirY > 0) { if (array[y][x]) { return false; } } else { if (array[y - 1][x]) { return false; } } } else { if (dirY > 0) { if (array[y][x - 1]) { return false; } } else { if (array[y - 1][x - 1]) { return false; } } } } } else { int x = posX.z / 2; if (x >= array[0].length || x == 0) { return false; } int y = posY.z / posY.n / 2; if (dirX > 0 && array[y][x] || dirX < 0 && array[y][x - 1]) { dirX = -1 * dirX; } } } else if (posY.n == 1 && posY.z % 2 == 0) { int y = posY.z / 2; if (y >= array.length || y == 0) { return false; } int x = posX.z / posX.n / 2; if (dirY > 0 && array[y][x] || dirY < 0 && array[y - 1][x]) { dirY = -1 * dirY; } } distance += step(dirX, dirY); } return false; } // steps until the next coord becomes integer private static double step(int dirX, int dirY) { if (dirY == 0) { if (dirX > 0) { posX = posX.plus(Rational.one); } else { posX = posX.minus(Rational.one); } return 1; } if (dirX == 0) { if (dirY > 0) { posY = posY.plus(Rational.one); } else { posY = posY.minus(Rational.one); } return 1; } if (posX.n == 1) { Rational distY = posY.fractal(); Rational speed = new Rational(Math.abs(dirY), Math.abs(dirX)); if (dirY > 0) { distY = Rational.one.minus(distY); } if (distY.equals(Rational.zero)) { distY = Rational.one; } if (distY.minus(speed).positive()) { if (dirX > 0) { posX = posX.plus(Rational.one); } else { posX = posX.minus(Rational.one); } if (dirY > 0) { posY = posY.plus(speed); } else { posY = posY.minus(speed); } return Math.sqrt(1 + speed.times(speed).value()); } else { if (dirY > 0) { posY = posY.plus(distY); } else { posY = posY.minus(distY); } Rational distX = distY.divides(speed); if (dirX > 0) { posX = posX.plus(distX); } else { posX = posX.minus(distX); } return Math.sqrt(distY.times(distY).value() + distX.times(distX).value()); } } else { Rational distX = posX.fractal(); Rational speed = new Rational(Math.abs(dirX), Math.abs(dirY)); if (dirX > 0) { distX = Rational.one.minus(distX); } if (distX.minus(speed).positive()) { if (dirY > 0) { posY = posY.plus(Rational.one); } else { posY = posY.minus(Rational.one); } if (dirX > 0) { posX = posX.plus(speed); } else { posX = posX.minus(speed); } return Math.sqrt(1 + speed.times(speed).value()); } else { if (dirX > 0) { posX = posX.plus(distX); } else { posX = posX.minus(distX); } Rational distY = distX.divides(speed); if (dirY > 0) { posY = posY.plus(distY); } else { posY = posY.minus(distY); } return Math.sqrt(distY.times(distY).value() + distX.times(distX).value()); } } } // private static void out(boolean[] array) { // System.out.println(Arrays.toString(array)); // } // // private static void out(boolean[][] array) { // int count = 0; // for (boolean[] a : array) { // System.out.print(count++ + ":"); // out(a); // } // } private static class Rational { static final Rational one = new Rational(1, 1); static final Rational zero = new Rational(0, 1); public int z; public int n; // create and initialize a new Rational object public Rational(int z, int n) { if (n == 0) { throw new RuntimeException("Denominator is zero"); } int g = gcd(z, n); this.z = z / g; this.n = n / g; } // return string representation of (this) public String toString() { if (n == 1) { return z + ""; } else { return z + "/" + n; } } // return (this * b) public Rational times(Rational b) { return new Rational(this.z * b.z, this.n * b.n); } // return (this + b) public Rational plus(Rational b) { int z = (this.z * b.n) + (this.n * b.z); int n = this.n * b.n; return new Rational(z, n); } // return (this - b) public Rational minus(Rational b) { int z = (this.z * b.n) - (this.n * b.z); int n = this.n * b.n; return new Rational(z, n); } // return fractal amount public Rational fractal() { return new Rational(z % n, n); } // return (1 / this) public Rational reciprocal() { return new Rational(n, z); } // return (this / b) public Rational divides(Rational b) { return this.times(b.reciprocal()); } public boolean positive() { return z * n >= 0; } public boolean equals(Rational r) { return r.z == this.z && r.n == this.n; } public double value() { return 1.0 * z / n; } private int gcd(int m, int n) { if (0 == n) return m; else return gcd(n, m % n); } } }
0
3,532
C20009
C20067
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
import java.io.*; import java.util.*; public class Solution { private StringTokenizer st; private BufferedReader in; private PrintWriter out; final String file = "D-large"; public void solve() throws IOException { int tests = nextInt(); for (int test = 0; test < tests; ++test) { int n = nextInt(); int m = nextInt(); int d = nextInt(); char[][] f = new char[n][m]; int x0 = -1, y0 = -1; for (int i = 0; i < n; ++i) { f[i] = next().toCharArray(); for (int j = 0; j < m; ++j) { if (f[i][j] == 'X') { x0 = i; y0 = j; } } } int ans = 0; for (int dx = -d; dx <= d; ++dx) { for (int dy = -d; dy <= d; ++dy) { if ((dx != 0 || dy != 0) && dx * dx + dy * dy <= d * d && raytrace(x0, y0, x0 + dx, y0 + dy, f, 0.)) { // System.err.println(dx + " " + dy); ans++; } } } System.err.printf("Case #%d: %d%n", test + 1, ans); out.printf("Case #%d: %d%n", test + 1, ans); } } final double EPS = 1e-8; private boolean raytrace(int x0, int y0, int x1, int y1, char[][] f, double t) { double firstt = Double.POSITIVE_INFINITY; int dx = x1 - x0; int dy = y1 - y0; double tx = Double.POSITIVE_INFINITY; for (int i = Math.max(0, Math.min(x0, x1)); i < f.length && i <= Math.max(x0, x1); ++i) { for (int j = Math.max(0, Math.min(y0, y1)); j < f[0].length && j <= Math.max(y0, y1); ++j) { if (f[i][j] == 'X') { double tt = dx != 0 ? (double)(i - x0) / dx : (double)(j - y0) / dy; if (Math.abs(x0 + dx * tt - i) < EPS && Math.abs(y0 + dy * tt - j) < EPS) { tx = tt; } } if (f[i][j] != '#') { continue; } double minx = dx == 0 ? Double.NEGATIVE_INFINITY : Math.min((i - 0.5 - x0) / dx, (i + 0.5 - x0) / dx); double maxx = dx == 0 ? Double.POSITIVE_INFINITY : Math.max((i - 0.5 - x0) / dx, (i + 0.5 - x0) / dx); double miny = dy == 0 ? Double.NEGATIVE_INFINITY : Math.min((j - 0.5 - y0) / dy, (j + 0.5 - y0) / dy); double maxy = dy == 0 ? Double.POSITIVE_INFINITY : Math.max((j - 0.5 - y0) / dy, (j + 0.5 - y0) / dy); if (maxx < miny - EPS || maxy < minx - EPS) { continue; } double tt = Math.max(minx, miny); if (tt > t + EPS) { firstt = Math.min(firstt, tt); } } } if (firstt > 1.) { return x1 >= 0 && x1 < f.length && y1 >= 0 && y1 < f[0].length && f[x1][y1] == 'X'; } if (tx > t + EPS && tx < firstt) { return false; } int sx = Integer.signum(dx); int sy = Integer.signum(dy); double x = x0 + dx * firstt - 0.5; double y = y0 + dy * firstt - 0.5; int rx = (int)Math.round(x); int ry = (int)Math.round(y); if (Math.abs(rx - x) < EPS && Math.abs(ry - y) < EPS) { if (dx == 0 || dy == 0) { throw new AssertionError(); } boolean f11 = get(f, rx + (1 + sx) / 2, ry + (1 + sy) / 2); boolean f01 = get(f, rx + (1 - sx) / 2, ry + (1 + sy) / 2); boolean f10 = get(f, rx + (1 + sx) / 2, ry + (1 - sy) / 2); if (get(f, rx + (1 - sx) / 2, ry + (1 - sy) / 2) || !f11 && !f01 && !f10) { throw new AssertionError(); } if (!f11) { return raytrace(x0, y0, x1, y1, f, firstt); } if (f01 && f10 && f11) { return raytrace(2 * rx + 1 - x0, 2 * ry + 1 - y0, 2 * rx + 1 - x1, 2 * ry + 1 - y1, f, firstt); } if (f10 && f11) { return raytrace(2 * rx + 1 - x0, y0, 2 * rx + 1 - x1, y1, f, firstt); } if (f01 && f11) { return raytrace(x0, 2 * ry + 1 - y0, x1, 2 * ry + 1 - y1, f, firstt); } return false; } if (Math.abs(rx - x) < EPS) { return raytrace(2 * rx + 1 - x0, y0, 2 * rx + 1 - x1, y1, f, firstt); } if (Math.abs(ry - y) < EPS) { return raytrace(x0, 2 * ry + 1 - y0, x1, 2 * ry + 1 - y1, f, firstt); } throw new AssertionError(); } private boolean get(char[][] f, int i, int j) { return i >= 0 && i < f.length && j >= 0 && j < f[0].length && f[i][j] == '#'; } public void run() throws IOException { in = new BufferedReader(new FileReader(file + ".in")); out = new PrintWriter(file + ".out"); eat(""); solve(); out.close(); } void eat(String s) { st = new StringTokenizer(s); } String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } eat(line); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); new Solution().run(); } }
0
3,533
C20009
C20008
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; public class SimpleMirrors { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader(args[0])); PrintWriter out = new PrintWriter(args[1]); // number of testcases String sCount = in.readLine(); int count = Integer.parseInt(sCount); for(int idx=1; idx<=count; idx++) { String[] parts = in.readLine().split(" "); int h = Integer.parseInt(parts[0]); int w = Integer.parseInt(parts[1]); int d = Integer.parseInt(parts[2]); int x = -1, y = -1; // small dataset => just find the "X" for(int i=0; i<h; i++) { String l = in.readLine(); int p = l.indexOf('X'); if(p > -1) { y = 10*i - 5; x = 10*p - 5; } } out.println("Case #" + idx + ": " + process(10*(h-2), 10*(w-2), 10*d, x, y)); } out.close(); } private static int process(int h, int w, int d, int ox, int oy) { System.out.println("h=" + h + ", w=" + w + ", d=" + d + ", ox=" + ox + ", oy=" + oy); MirrorSet ms = new MirrorSet( Arrays.asList(new Mirror[] { new Mirror(Facing.POS, Orient.HORIZ, 0, 0, w), new Mirror(Facing.NEG, Orient.HORIZ, h, 0, w), new Mirror(Facing.POS, Orient.VERT, 0, 0, h), new Mirror(Facing.NEG, Orient.VERT, w, 0, h), }), ox, oy, d, w, h ); Set<Point> pts = ms.transitiveImages(); Set<Double> angles = ms.angles(pts); System.out.println(" " + pts.size() + ": " + pts); System.out.println(" " + angles.size() + ": " + angles); return angles.size(); } private static class MirrorSet { private final Collection<Mirror> mirrors; private final int ox, oy, limit, w, h; public MirrorSet(Collection<Mirror> mirrors, int ox, int oy, int limit, int w, int h) { this.mirrors = mirrors; this.ox = ox; this.oy = oy; this.limit = limit; this.w = w; this.h = h; } public Set<Point> images(Set<Point> in) { HashSet<Point> out = new HashSet<Point>(); for(Point i : in) { for(Mirror m : mirrors) { Point o = m.image(i); if(o != null && ! in.contains(o) && Math.sqrt((ox-o.x)*(ox-o.x) + (oy-o.y)*(oy-o.y)) <= limit) out.add(o); } } return out; } public Set<Point> transitiveImages() { Set<Point> im = new HashSet<Point>(); im.add(new Point(ox, oy)); Set<Point> newer; do { newer = images(im); im.addAll(newer); } while(newer.size() > 0); return im; } public Set<Double> angles(Set<Point> in) { Set<Double> out = new HashSet<Double>(); for(Point i : in) { if(i.x != ox || i.y != oy) out.add(Math.atan2(i.y-oy, i.x-ox)); } return out; } } private static enum Facing { POS, NEG } private static enum Orient { VERT, HORIZ } private static class Mirror { private final Facing facing; private final Orient orient; private final int pos, start, stop; public Mirror(Facing facing, Orient orient, int pos, int start, int stop) { this.facing = facing; this.orient = orient; this.pos = pos; this.start = start; this.stop = stop; } public Point image(Point p) { switch(orient) { case VERT: if(facing == Facing.POS && p.x >= pos || facing == Facing.NEG && p.x <= pos) { return new Point(2 * pos - p.x, p.y); } else return null; case HORIZ: if(facing == Facing.POS && p.y >= pos || facing == Facing.NEG && p.y <= pos) { return new Point(p.x, 2 * pos - p.y); } else return null; } return null; } } private static class Point { public final int x, y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { return obj instanceof Point && ((Point)obj).x == x && ((Point)obj).y == y; } @Override public int hashCode() { return (x << 16) ^ y; } @Override public String toString() { return "(" + x + ", " + y + ")"; } } }
0
3,534
C20009
C20063
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
0
3,535
C20009
C20015
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
0
3,536
C20009
C20050
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,537
C20009
C20085
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
0
3,538
C20009
C20020
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
0
3,539
C20009
C20062
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,540
C20009
C20059
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
0
3,541
C20009
C20064
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
0
3,542
C20009
C20076
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
0
3,543
C20009
C20026
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
import java.io.*; import java.util.*; import java.math.*; class D { private static final boolean DEBUG_ON = true; private static final boolean ECHO_ON = true; private static BufferedReader input; private static BufferedWriter output; private static final int INF = Integer.MAX_VALUE / 2; private static final int MOD = 10007; private static int H, W, D, row, col; public static int gcd(int n, int m) {return (0 == m) ? (n) : gcd(m, n % m);} public static int sqrt(int X) { int answer = 1, interval = 1; for (int i = String.valueOf(X).length() / 2; i > 0; i--) {interval *= 10;} while (interval >= 1) { while ((answer * answer) <= X) {answer += interval;} answer -= interval; interval /= 10; } return answer; } public static void main(String[] args) { try { input = new BufferedReader(new FileReader(args[0] + ".in")); output = new BufferedWriter(new FileWriter(args[0] + ".out")); String line = input.readLine(); int testcases = getInt(line, 0); for (int testcase = 1; testcase <= testcases; testcase++) { char[][] real = getCharMatrix(input); HashSet<Integer> valid = new HashSet<Integer>(); for (int i = row - D; i <= row + D; i++) { int range = sqrt((D * D) - ((i - row) * (i - row))); for (int j = col - range; j <= col + range; j++) { int diffX = i - row; int diffY = j - col; if (0 == diffX && 0 == diffY) {continue;} int gcd = gcd(Math.abs(diffX), Math.abs(diffY)); int direction = (((diffX/gcd) + D) << 16) + ((diffY/gcd) + D); if (valid.contains(direction)) {continue;} int x = 100 * row + 50; int y = 100 * col + 50; for (int k = 0; k < 100; k++) { int nextX = x + diffX; int nextY = y + diffY; int xCell = x / 100; int yCell = y / 100; int nextXCell = nextX / 100; if (0 == nextX % 100) {nextXCell = (nextX + diffX) / 100;} int nextYCell = nextY / 100; if (0 == nextY % 100) {nextYCell = (nextY + diffY) / 100;} if (xCell == nextXCell && yCell == nextYCell) {x = nextX; y = nextY;} else if (xCell != nextXCell && yCell == nextYCell) { if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;} else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if (xCell == nextXCell && yCell != nextYCell) { if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;} else { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else { int cornerX = -1, cornerY = -1; if (nextXCell < xCell && nextYCell < yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * (nextYCell + 1);} else if (nextXCell > xCell && nextYCell < yCell) {cornerX = 100 * nextXCell; cornerY = 100 * (nextYCell + 1);} else if (nextXCell < xCell && nextYCell > yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * nextYCell;} else if (nextXCell > xCell && nextYCell > yCell) {cornerX = 100 * nextXCell; cornerY = 100 * nextYCell;} if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { diffX = -diffX; diffY = -diffY; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {x = nextX; y = nextY;} // passing corner else { diffX = -diffX; diffY = -diffY; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {break;} // hitting corner else if (Math.abs((cornerX - x) * (nextY - y)) < Math.abs((nextX - x) * (cornerY - y))) // hitting Y { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { if (Math.abs((cornerX - x) * (nextY - y)) <= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;} else { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { if (Math.abs((cornerX - x) * (nextY - y)) >= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;} else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) {x = nextX; y = nextY;} } if ((100 * row + 50) == x && (100 * col + 50) == y) {valid.add(direction); break;} } } } String result = "Case #" + testcase + ": " + valid.size(); output(result); } input.close(); output.close(); } catch (Exception e) { e.printStackTrace(); } } public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));} public static long getLong(String line, int index) {return Long.parseLong(getString(line, index));} public static double getDouble(String line, int index) {return Double.parseDouble(getString(line, index));} public static String getString(String line, int index) { line = line.trim(); while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;} if ((-1) == line.indexOf(' ')) {return line;} else {return line.substring(0, line.indexOf(' '));} } public static int[] getIntArray(String line) { String[] strings = getStringArray(line); int[] numbers = new int[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Integer.parseInt(strings[i]);} return numbers; } public static long[] getLongArray(String line) { String[] strings = getStringArray(line); long[] numbers = new long[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Long.parseLong(strings[i]);} return numbers; } public static double[] getDoubleArray(String line) { String[] strings = getStringArray(line); double[] numbers = new double[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Double.parseDouble(strings[i]);} return numbers; } public static String[] getStringArray(String line) {return line.trim().split("(\\s)+", 0);} public static int[] getIntArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); int[] numbers = new int[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Integer.parseInt(strings[i - begin]);} return numbers; } public static long[] getLongArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); long[] numbers = new long[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Long.parseLong(strings[i - begin]);} return numbers; } public static double[] getDoubleArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); double[] numbers = new double[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Double.parseDouble(strings[i - begin]);} return numbers; } public static String[] getStringArray(String line, int begin, int end) { String[] lines = line.trim().split("(\\s)+", 0); String[] results = new String[end - begin]; for (int i = begin; i < end; i++) {results[i - begin] = lines[i];} return results; } public static char[][] getCharMatrix(BufferedReader input) throws Exception { String line = input.readLine(); H = getInt(line, 0); W = getInt(line, 1); D = getInt(line, 2); char[][] matrix = new char[H][W]; for (int i = 0; i < H; i++) { line = input.readLine(); for (int j = 0; j < W; j++) { char c = matrix[i][j] = line.charAt(j); if ('X' == c) {row = i; col = j;} } } return matrix; } public static int[][] getIntMatrix(BufferedReader input) throws Exception { String line = input.readLine(); int R = getInt(line, 0); int C = getInt(line, 1); int[][] matrix = new int[R][C]; for (int i = 0; i < R; i++) { line = input.readLine(); for (int j = 0; j < C; j++) {matrix[i][j] = getInt(line, j);} } return matrix; } public static boolean[][] newBooleanMatrix(int R, int C, boolean value) { boolean[][] matrix = new boolean[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static char[][] newCharMatrix(int R, int C, char value) { char[][] matrix = new char[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static int[][] newIntMatrix(int R, int C, int value) { int[][] matrix = new int[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static long[][] newLongMatrix(int R, int C, long value) { long[][] matrix = new long[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static double[][] newDoubleMatrix(int R, int C, double value) { double[][] matrix = new double[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static void output(String s) throws Exception { if (ECHO_ON) {System.out.println(s);} output.write(s); output.newLine(); } public static String toKey(boolean[] array) { StringBuffer buffer = new StringBuffer(array.length + ","); for (int i = 0; i < array.length / 16; i++) { char c = 0; for (int j = 0; j < 16; j++) { c <<= 1; if (array[i * 16 + j]) {c += 1;} } buffer.append(c + ""); } char c = 0; for (int j = 0; j < (array.length % 16); j++) { c <<= 1; if (array[(array.length / 16) * 16 + j]) {c += 1;} } buffer.append(c + ""); return buffer.toString(); } public static String toKey(int[] array, int bit) { StringBuffer buffer = new StringBuffer(array.length + ","); if (bit > 16) { for (int i = 0; i < array.length; i++) { char c1 = (char)(array[i] >> 16); char c2 = (char)(array[i] & 0xFFFF); buffer.append("" + c1 + c2); } } else { int n = 16 / bit; for (int i = 0; i < array.length / n; i++) { char c = 0; for (int j = 0; j < n; j++) { c <<= bit; c += array[i * n + j]; } buffer.append(c + ""); } char c = 0; for (int j = 0; j < (array.length % n); j++) { c <<= bit; c += array[(array.length / n) * n + j]; } buffer.append(c + ""); } return buffer.toString(); } public static void debug(String s) {if (DEBUG_ON) {System.out.println(s);}} public static void debug(String s0, double l0) {if (DEBUG_ON) {System.out.println(s0+" = "+l0);}} public static void debug(String s0, double l0, String s1, double l1) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2) {if (DEBUG_ON) { System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3, String s4, double l4) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3+"; "+s4+" = "+l4);}} public static void debug(boolean[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(boolean[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append((array[i] == true ? "1" : "0") + separator);} buffer.append((array[array.length - 1] == true) ? "1" : "0"); System.out.println(buffer.toString()); } } public static void debug(boolean[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(boolean[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} public static void debug(char[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(char[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);} buffer.append(array[array.length - 1]); System.out.println(buffer.toString()); } } public static void debug(char[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(char[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} public static void debug(int[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(int[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);} buffer.append(array[array.length - 1]); System.out.println(buffer.toString()); } } public static void debug(int[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(int[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} }
0
3,544
C20009
C20078
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
import java.awt.Point; public class FracVec { public final Frac row, col; public FracVec(Frac row, Frac col) { this.row = row; this.col = col; } public FracVec(Point pos) { row = new Frac(pos.y); col = new Frac(pos.x); } public FracVec flipHoriz() { return new FracVec(row, col.neg()); } public FracVec flipVert() { return new FracVec(row.neg(), col); } public boolean isCorner() { return row.denom == 1 && col.denom == 1; } public FracVec add(FracVec other) { return new FracVec(row.add(other.row), col.add(other.col)); } public FracVec sub(FracVec other) { return new FracVec(row.sub(other.row), col.sub(other.col)); } public FracVec neg() { return new FracVec(row.neg(), col.neg()); } public boolean equals(FracVec other) { return row.equals(other.row) && col.equals(other.col); } public boolean equals(Point p) { return row.equals(p.y) && col.equals(p.x); } @Override public boolean equals(Object other) { return other instanceof FracVec && equals((FracVec) other); } @Override public int hashCode() { return (31 + row.hashCode()) * 31 + col.hashCode(); } public String toString() { return "( " + row + ", " + col + " )"; } public FracVec multiply(Frac scalar) { return new FracVec(row.mult(scalar), col.mult(scalar)); } public int compareLengthTo(int d) { return row.mult(row).add(col.mult(col)).compareTo(new Frac(d * d)); } public Point getNextCell(FracVec dm) { int resRow, resCol; if (dm.row.sgn() < 0) { resRow = row.roundDownAddSmallDown(); } else { resRow = row.roundDownAddSmallUp(); } if (dm.col.sgn() < 0) resCol = col.roundDownAddSmallDown(); else resCol = col.roundDownAddSmallUp(); return new Point(resCol, resRow); } }
0
3,545
C20009
C20057
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package jp.funnything.competition.util; import java.util.Arrays; import java.util.List; import com.google.common.collect.Lists; import com.google.common.primitives.Ints; import com.google.common.primitives.Longs; public class Prime { public static class PrimeData { public int[] list; public boolean[] map; private PrimeData( final int[] values , final boolean[] map ) { list = values; this.map = map; } } public static long[] factorize( long n , final int[] primes ) { final List< Long > factor = Lists.newArrayList(); for ( final int p : primes ) { if ( n < p * p ) { break; } while ( n % p == 0 ) { factor.add( ( long ) p ); n /= p; } } if ( n > 1 ) { factor.add( n ); } return Longs.toArray( factor ); } public static PrimeData prepare( final int n ) { final List< Integer > primes = Lists.newArrayList(); final boolean[] map = new boolean[ n ]; Arrays.fill( map , true ); map[ 0 ] = map[ 1 ] = false; primes.add( 2 ); for ( int composite = 2 * 2 ; composite < n ; composite += 2 ) { map[ composite ] = false; } for ( int value = 3 ; value < n ; value += 2 ) { if ( map[ value ] ) { primes.add( value ); for ( int composite = value * 2 ; composite < n ; composite += value ) { map[ composite ] = false; } } } return new PrimeData( Ints.toArray( primes ) , map ); } }
0
3,546
C20009
C20030
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; /** * * @author jim */ public class RayVector { public final int x; public final int y; public RayVector(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { if (obj instanceof RayVector) { RayVector v = (RayVector)obj; if (v.x * y == v.y * x) return true; } return false; } @Override public int hashCode() { int hash = 7; hash = 41 * hash + this.x; hash = 41 * hash + this.y; return hash; } public Fraction getScalarGardient() { return new Fraction(Math.abs(y), Math.abs(x)); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("("); sb.append(x); sb.append(","); sb.append(y); sb.append(")"); return sb.toString(); } }
0
3,547
C20009
C20002
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
import java.util.*; import java.io.*; import java.math.*; import java.awt.*; import static java.lang.Math.*; import static java.lang.Integer.parseInt; import static java.lang.Double.parseDouble; import static java.lang.Long.parseLong; import static java.lang.System.*; import static java.util.Arrays.*; import static java.util.Collection.*; public class D { static int gcd(int a, int b) { return b == 0 ? a : a == 0 ? b : gcd(b, a%b); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(in)); int T = parseInt(br.readLine()); for(int t = 0; t++ < T; ) { String[] line = br.readLine().split(" "); int H = parseInt(line[0]), W = parseInt(line[1]), D = parseInt(line[2]); char[][] G = new char[H][]; for(int h = 0; h < H; h++) G[h] = br.readLine().toCharArray(); int X = 0, Y = 0; outer:for(Y = 0; Y < H; Y++) for(X = 0; X < W; X++) if(G[Y][X] == 'X') break outer; int count = 0; for(int i = -D; i <= D; i++) { for(int j = -D; j <= D; j++) { int dx = i, dy = j, scale = 2 * Math.abs((dx == 0 ? 1 : dx) * (dy == 0 ? 1 : dy)), x0, y0, x, y; int steps = (int)Math.floor(scale * D / Math.sqrt(dx * dx + dy * dy)); if(gcd(Math.abs(dx), Math.abs(dy)) != 1) continue; x0 = x = X * scale + scale / 2; y0 = y = Y * scale + scale / 2; do { steps -= 1; if(x % scale == 0 && y % scale == 0) { // at a corner int dxi = dx > 0 ? 1 : -1, dyi = dy > 0 ? 1 : -1; int xi = (x / scale) - (dxi + 1) / 2, yi = (y / scale) - (dyi + 1) / 2; if(G[yi+dyi][xi+dxi] == '#') { if(G[yi+dyi][xi] != '#' && G[yi][xi+dxi] != '#') steps = -1; // kill the light if(G[yi+dyi][xi] == '#') dy *= -1; if(G[yi][xi+dxi] == '#') dx *= -1; } else ; // otherwise step as normal } else if(x % scale == 0) { int xi = x / scale, yi = y / scale; if(G[yi][xi] == '#' || G[yi][xi-1] == '#') dx *= -1; } else if(y % scale == 0) { int xi = x / scale, yi = y / scale; if(G[yi][xi] == '#' || G[yi-1][xi] == '#') dy *= -1; } else ; // smooth sailing x += dx; y += dy; } while(steps >= 0 && !(x == x0 && y == y0)); if(steps >= 0) ++count; } } out.println("Case #" + t +": " + count) ; } } }
0
3,548
C20009
C20056
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package jp.funnything.competition.util; import java.math.BigDecimal; /** * Utility for BigDeciaml */ public class BD { public static BigDecimal ZERO = BigDecimal.ZERO; public static BigDecimal ONE = BigDecimal.ONE; public static BigDecimal add( final BigDecimal x , final BigDecimal y ) { return x.add( y ); } public static BigDecimal add( final BigDecimal x , final double y ) { return add( x , v( y ) ); } public static BigDecimal add( final double x , final BigDecimal y ) { return add( v( x ) , y ); } public static int cmp( final BigDecimal x , final BigDecimal y ) { return x.compareTo( y ); } public static int cmp( final BigDecimal x , final double y ) { return cmp( x , v( y ) ); } public static int cmp( final double x , final BigDecimal y ) { return cmp( v( x ) , y ); } public static BigDecimal div( final BigDecimal x , final BigDecimal y ) { return x.divide( y ); } public static BigDecimal div( final BigDecimal x , final double y ) { return div( x , v( y ) ); } public static BigDecimal div( final double x , final BigDecimal y ) { return div( v( x ) , y ); } public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) { return x.multiply( y ); } public static BigDecimal mul( final BigDecimal x , final double y ) { return mul( x , v( y ) ); } public static BigDecimal mul( final double x , final BigDecimal y ) { return mul( v( x ) , y ); } public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) { return x.subtract( y ); } public static BigDecimal sub( final BigDecimal x , final double y ) { return sub( x , v( y ) ); } public static BigDecimal sub( final double x , final BigDecimal y ) { return sub( v( x ) , y ); } public static BigDecimal v( final double value ) { return BigDecimal.valueOf( value ); } }
0
3,549
C20009
C20018
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package template; import java.util.Objects; public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{ private T1 o1; private T2 o2; public Pair(T1 o1, T2 o2) { this.o1 = o1; this.o2 = o2; } @Override public boolean equals(Object other) { if (!(other instanceof Pair)) {return false;} return (compareTo((Pair<T1, T2>)other) == 0); } @Override public int compareTo(Pair<T1, T2> other) { int c1 = ((Comparable<T1>) o1).compareTo(other.getO1()); if (c1 != 0) {return c1;} int c2 = ((Comparable<T2>) o2).compareTo(other.getO2()); return c2; } @Override public int hashCode() { int hash = 5; hash = 83 * hash + Objects.hashCode(this.o1); hash = 83 * hash + Objects.hashCode(this.o2); return hash; } @Override public String toString() { return "[" + o1 + ", " + o2 + "]"; } public T1 getO1() { return o1; } public void setO1(T1 o1) { this.o1 = o1; } public T2 getO2() { return o2; } public void setO2(T2 o2) { this.o2 = o2; } }
0
3,550
C20009
C20051
package com.forthgo.google.g2012r0; import com.forthgo.math.Helper; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; /** * Created by Xan Gregg. * Date: 4/14/12 */ public class ProblemD { private static final int SELF = 2; private static final int MIRROR = 1; public static void main(String[] args) { try { Scanner in = new Scanner(new File("D.in")); PrintWriter out = new PrintWriter(new FileWriter("D.out")); //PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int h = in.nextInt(); int w = in.nextInt(); int d = in.nextInt(); int k = solve(in, h, w, d); out.printf("Case #%d: %d%n", i + 1, k); out.flush(); } } catch (IOException e) { throw new RuntimeException(); } } private static int solve(Scanner in, int H, int W, int D) { in.nextLine(); int [][] cell = new int[W][H]; int count = 0; int x = 0; int y = 0; for (int i = 0; i < H; i++) { String row = in.nextLine(); for (int j = 0; j < W; j++) { if (row.charAt(j) == '#') cell[j][i] = MIRROR; else if (row.charAt(j) == 'X') { cell[j][i] = SELF; x = j; y = i; } else if (row.charAt(j) != '.') { throw new RuntimeException(); } } } for (int i = 1; i < W; i++) { if (cell[x + i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < W; i++) { if (cell[x - i][y] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y + i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } for (int i = 1; i < H; i++) { if (cell[x][y - i] == MIRROR) { if (2 * i - 1 <= D) count++; break; } } //room.offset(x, y); for (int xdir = 1; xdir <= D; xdir++) { int my = (int) Math.sqrt(D * D - xdir * xdir); for (int ydir = 1; ydir <= my; ydir++) { if (gcd(xdir, ydir) == 1) { int k = (int) (D / Math.sqrt(xdir * xdir + ydir * ydir)); for (int xsign = -1; xsign <= 1; xsign += 2) for (int ysign = -1; ysign <= 1; ysign += 2) count += countPaths(cell, x, y, k * xdir, k * ydir, xsign, ysign); } } } return count; } private static int countPaths(int [][] cell, int x, int y, int xdir, int ydir, int xsign, int ysign) { int dx = 0; int dy = 0; while (dx <= xdir && dy < ydir || dx < xdir && dy <= ydir) { int x2next = 2 * dx + 1; int y2next = x2next * ydir / xdir; if (y2next == 2 * dy + 1 && y2next * xdir == ydir * x2next) { int xcell = cell[x + xsign][y]; int ycell = cell[x][y + ysign]; int xycell = cell[x + xsign][y + ysign]; // corner if (xycell == MIRROR && xcell == MIRROR && ycell == MIRROR) { xsign = -xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell == MIRROR && ycell != MIRROR) { y += ysign; xsign = -xsign; } else if (xycell == MIRROR && xcell != MIRROR && ycell == MIRROR) { x += xsign; ysign = -ysign; } else if (xycell == MIRROR && xcell != MIRROR && ycell != MIRROR) { return 0; // kills beam } else if (xycell != MIRROR) { // pass through x += xsign; y += ysign; } else throw new RuntimeException(); dx++; dy++; } else if (y2next < 2 * dy + 1) { // next x cell if (cell[x + xsign][y] == MIRROR) { xsign = -xsign; } else { // empty x += xsign; } dx++; } else if (y2next >= 2 * dy + 1) { // next y cell if (cell[x][y + ysign] == MIRROR) { ysign = -ysign; } else { // empty y += ysign; } dy++; } else throw new RuntimeException(); if (dx > xdir || dy > ydir) break; if (cell[x][y] == SELF) { if ((2 * dy) * xdir == ydir * (2 * dx)) { // System.out.printf("%2d %2d %2d %2d %2d %2d %6.3f%n", xdir, ydir, dx, dy, xsign, ysign, Math.sqrt(dx * dx + dy * dy)); return 1; } } } return 0; } public static int gcd(int a, int b) { if (a < 0 || b < 0) return -1; while (b != 0) { int x = a % b; a = b; b = x; } return a; } }
package jp.funnything.competition.util; import java.util.List; import com.google.common.collect.Lists; public class Lists2 { public static < T > List< T > newArrayListAsArray( final int length ) { final List< T > list = Lists.newArrayListWithCapacity( length ); for ( int index = 0 ; index < length ; index++ ) { list.add( null ); } return list; } }
0
3,551
C20060
C20004
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
0
3,552
C20060
C20005
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package gcj; import com.sun.org.apache.xerces.internal.impl.xs.opti.DefaultXMLDocumentHandler; import java.util.*; import java.io.*; public class HallOfMirrors { final static String PROBLEM_NAME = "mirrors"; final static String WORK_DIR = "D:\\Gcj\\" + PROBLEM_NAME + "\\"; int H, W, D; double stX, stY; int res = 0; String[] map; int gcd(int a, int b) { while (a>0 && b>0) if (a>b) a %= b; else b %= a; return a + b; } final double EPS = 1e-10; double getT(double cur, int V) { cur *= 2; V *= 2; if (V == 0) return 1e100; if (V > 0) { double want = Math.ceil(cur + EPS); return (want - cur) / V; } else { double want = Math.floor(cur - EPS); return (want - cur) / V; } } boolean isInteger(double x) { return Math.abs(x - Math.floor(x)) <= EPS || Math.abs(x - Math.ceil(x)) <= EPS; } boolean inMirror(double x, double y) { int xx = (int)Math.floor(x); int yy = (int)Math.floor(y); return map[xx].charAt(yy) == '#'; } void process(int dx, int dy) { double curX = stX, curY = stY; double dist = 0.0; while (true) { double t1 = getT(curX, dx); double t2 = getT(curY, dy); double t = Math.min(t1, t2); double nextX = curX + t * dx; double nextY = curY + t * dy; double piece = Math.sqrt((nextX - curX) * (nextX - curX) + (nextY - curY) * (nextY - curY)); dist += piece; if (dist > D + EPS) return; if (Math.abs(nextX - stX) <= EPS && Math.abs(nextY - stY) <= EPS) { res++; return; } curX = nextX; curY = nextY; boolean fx = isInteger(nextX); boolean fy = isInteger(nextY); if (fx && fy) { // corner // A B // C D boolean A = inMirror(nextX - EPS, nextY - EPS); boolean B = inMirror(nextX - EPS, nextY + EPS); boolean C = inMirror(nextX + EPS, nextY - EPS); boolean D = inMirror(nextX + EPS, nextY + EPS); int cnt = (A ? 1 : 0) + (B ? 1 : 0) + (C ? 1 : 0) + (D ? 1 : 0); if (cnt == 3) { dx = -dx; dy = -dy; } else if ((A && B && !C && !D) || (!A && !B && C & D)) { dx = -dx; } else if ((A && C && !B && !D) || (!A && !C && B && D)) { dy = -dy; } else if (inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY + (dy > 0 ? EPS : -EPS))) { return; } else { // just continue; } } else if (fx && !fy) { // horizontal if (dx != 0 && inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY)) dx = -dx; } else if (!fx && fy) { // vertical if (dy != 0 && inMirror(nextX, nextY + (dy > 0 ? EPS : -EPS))) dy = -dy; } else { // nothing } } } void solve(Scanner sc, PrintWriter pw) { H = sc.nextInt(); W = sc.nextInt(); D = sc.nextInt(); map = new String[H]; for (int i=0; i<H; i++) map[i] = sc.next(); for (int i=0; i<H; i++) for (int j=0; j<W; j++) if (map[i].charAt(j) == 'X') { stX = i + 0.5; stY = j + 0.5; } for (int dx=-D; dx<=D; dx++) for (int dy=-D; dy<=D; dy++) if (dx*dx + dy*dy > 0 && dx*dx + dy*dy <= D * D && gcd(Math.abs(dx), Math.abs(dy)) == 1) { process(dx, dy); } pw.println(res); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new FileReader(WORK_DIR + "input.txt")); PrintWriter pw = new PrintWriter(new FileWriter(WORK_DIR + "output.txt")); int caseCnt = sc.nextInt(); for (int caseNum=0; caseNum<caseCnt; caseNum++) { System.out.println("Processing test case " + (caseNum + 1)); pw.print("Case #" + (caseNum+1) + ": "); new HallOfMirrors().solve(sc, pw); } pw.flush(); pw.close(); sc.close(); } }
0
3,553
C20060
C20065
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package jp.funnything.prototype; import static java.lang.Math.abs; import java.io.File; import java.io.IOException; import jp.funnything.competition.util.CompetitionIO; import jp.funnything.competition.util.Packer; import org.apache.commons.io.FileUtils; import org.apache.commons.math.fraction.Fraction; import org.apache.commons.math.util.MathUtils; public class Runner { public static void main( final String[] args ) throws Exception { new Runner().run(); } boolean isValid( final int d , final boolean[][] map , final int ox , final int oy , int dx , int dy ) { final Fraction fox = new Fraction( ox * 2 + 1 ); final Fraction foy = new Fraction( oy * 2 + 1 ); Fraction x = new Fraction( ox * 2 + 1 ); Fraction y = new Fraction( oy * 2 + 1 ); Fraction sumDiffX = new Fraction( 0 ); Fraction sumDiffY = new Fraction( 0 ); for ( ; ; ) { final Fraction diffX = new Fraction( dx > 0 ? ( int ) Math.floor( x.doubleValue() + 1 ) : ( int ) Math.ceil( x.doubleValue() - 1 ) ).subtract( x ); final Fraction diffY = new Fraction( dy > 0 ? ( int ) Math.floor( y.doubleValue() + 1 ) : ( int ) Math.ceil( y.doubleValue() - 1 ) ).subtract( y ); if ( abs( diffX.doubleValue() * dy ) < abs( diffY.doubleValue() * dx ) ) { x = x.add( diffX ); y = y.add( diffX.multiply( dy ).divide( dx ) ); sumDiffX = sumDiffX.add( diffX.abs() ); sumDiffY = sumDiffY.add( diffX.multiply( dy ).divide( dx ).abs() ); } else { y = y.add( diffY ); x = x.add( diffY.multiply( dx ).divide( dy ) ); sumDiffY = sumDiffY.add( diffY.abs() ); sumDiffX = sumDiffX.add( diffY.multiply( dx ).divide( dy ).abs() ); } if ( sumDiffX.multiply( sumDiffX ).add( sumDiffY.multiply( sumDiffY ) ).compareTo( new Fraction( d * d * 2 * 2 ) ) > 0 ) { return false; } if ( x.equals( fox ) && y.equals( foy ) ) { return true; } final int nx = x.intValue() / 2 + ( dx > 0 ? 0 : -1 ); final int ny = y.intValue() / 2 + ( dy > 0 ? 0 : -1 ); if ( x.getDenominator() == 1 && x.getNumerator() % 2 == 0 && y.getDenominator() == 1 && y.getNumerator() % 2 == 0 ) { if ( map[ ny ][ nx ] ) { final int px = x.intValue() / 2 + ( dx > 0 ? -1 : 0 ); final int py = y.intValue() / 2 + ( dy > 0 ? -1 : 0 ); if ( map[ py ][ nx ] ) { if ( map[ ny ][ px ] ) { dx = -dx; dy = -dy; } else { dx = -dx; } } else { if ( map[ ny ][ px ] ) { dy = -dy; } else { return false; } } } } else { if ( x.getDenominator() == 1 && x.getNumerator() % 2 == 0 ) { if ( map[ y.intValue() / 2 ][ nx ] ) { dx = -dx; } } else if ( y.getDenominator() == 1 && y.getNumerator() % 2 == 0 ) { if ( map[ ny ][ x.intValue() / 2 ] ) { dy = -dy; } } } } } void pack() { try { final File dist = new File( "dist" ); if ( dist.exists() ) { FileUtils.deleteQuietly( dist ); } final File workspace = new File( dist , "workspace" ); FileUtils.copyDirectory( new File( "src/main/java" ) , workspace ); FileUtils.copyDirectory( new File( "../../../../CompetitionUtil/Lib/src/main/java" ) , workspace ); Packer.pack( workspace , new File( dist , "sources.zip" ) ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } void run() throws Exception { final CompetitionIO io = new CompetitionIO(); final int t = io.readInt(); for ( int index = 0 ; index < t ; index++ ) { final int[] values = io.readInts(); final int h = values[ 0 ]; final int w = values[ 1 ]; final int d = values[ 2 ]; final char[][] map = new char[ h ][]; for ( int y = 0 ; y < h ; y++ ) { final char[] l = io.read().toCharArray(); if ( l.length != w ) { throw new RuntimeException( "assert" ); } map[ y ] = l; } io.write( index + 1 , solve( d , map ) ); } io.close(); pack(); } int solve( final int d , final char[][] map ) { int count = 0; int ox = -1; int oy = -1; final boolean[][] parsed = new boolean[ map.length ][]; for ( int y = 0 ; y < map.length ; y++ ) { parsed[ y ] = new boolean[ map[ y ].length ]; for ( int x = 0 ; x < map[ y ].length ; x++ ) { final char c = map[ y ][ x ]; if ( c == '#' ) { parsed[ y ][ x ] = true; } if ( c == 'X' ) { ox = x; oy = y; } } } for ( int dy = -d ; dy <= d ; dy++ ) { for ( int dx = -d ; dx <= d ; dx++ ) { if ( dx == 0 && dy == 0 ) { continue; } if ( MathUtils.gcd( dx , dy ) != 1 ) { continue; } if ( dx * dx + dy * dy > d * d ) { continue; } if ( isValid( d , parsed , ox , oy , dx , dy ) ) { count++; } } } return count; } }
0
3,554
C20060
C20006
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package problemD; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ProblemD { static Rational posX = null; static Rational posY = null; static Rational tarX = null; static Rational tarY = null; static boolean[][] array = null; public static void main(String[] args) throws FileNotFoundException { // Scanner sc = new Scanner(new File("D-practice.in")); // Scanner sc = new Scanner(new File("D-small.in")); Scanner sc = new Scanner(new File("D-large.in")); int cases = sc.nextInt(); for (int i = 1; i <= cases; i++) { // do case things here int H = sc.nextInt(); int W = sc.nextInt(); int D = sc.nextInt(); D *= 2; array = new boolean[H][W]; for (int j = 0; j < H; j++) { String s = sc.next(); for (int k = 0; k < W; k++) { array[j][k] = (s.charAt(k) == '#'); if (s.charAt(k) == 'X') { posX = new Rational(2 * k + 1, 1); posY = new Rational(2 * j + 1, 1); tarX = posX; tarY = posY; } } } int count = 0; boolean checked[][] = new boolean[2 * D + 1][2 * D + 1]; for (int j = 2; j <= D; j += 2) { for (int x = -j; x <= j; x += 2) { if (D + j >= 0 && D + j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + j][D + x]) { if (followRay(j, x, D)) { count++; } int k = 1; while (D + (k * j) >= 0 && D + (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * j)][D + (k * x)] = true; k++; } } } if (D + j >= 0 && D + j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + x][D + j]) { if (followRay(x, j, D)) { count++; } int k = 1; while (D + (k * j) >= 0 && D + (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * x)][D + (k * j)] = true; k++; } } } if (D - j >= 0 && D - j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D - j][D + x]) { if (followRay(-j, x, D)) { count++; } int k = 1; while (D - (k * j) >= 0 && D - (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D - (k * j)][D + (k * x)] = true; k++; } } } if (D - j >= 0 && D - j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + x][D - j]) { if (followRay(x, -j, D)) { count++; } int k = 1; while (D - (k * j) >= 0 && D - (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * x)][D - (k * j)] = true; k++; } } } } } // System.out.println(count); System.out.format("Case #%d: %d\n", i, count); } } private static boolean followRay(int dirX, int dirY, int max) { double distance = 0; posX = tarX; posY = tarY; distance += step(dirX, dirY); while (distance <= max) { if (tarX.equals(posX) && tarY.equals(posY)) { return true; } // check mirror, adjust direction if (dirX == 0) { if (posY.n == 1 && posY.z % 2 == 0) { int y = posY.z / 2; if (y >= array.length || y == 0) { return false; } int x = posX.z / posX.n / 2; if (dirY > 0 && array[y][x] || dirY < 0 && array[y - 1][x]) { dirY = -1 * dirY; } } } else if (dirY == 0) { if (posX.n == 1 && posX.z % 2 == 0) { int x = posX.z / 2; if (x >= array[0].length || x == 0) { return false; } int y = posY.z / posY.n / 2; if (dirX > 0 && array[y][x] || dirX < 0 && array[y][x - 1]) { dirX = -1 * dirX; } } } if (posX.n == 1 && posX.z % 2 == 0) { if (posY.n == 1 && posY.z % 2 == 0) { // corner int x = posX.z / 2; int y = posY.z / 2; boolean mirrored = false; if (dirX > 0) { if (array[y][x] && array[y - 1][x]) { dirX = -1 * dirX; mirrored = true; } } else { if (array[y][x - 1] && array[y - 1][x - 1]) { dirX = -1 * dirX; mirrored = true; } } if (dirY > 0) { if (array[y][x] && array[y][x - 1]) { dirY = -1 * dirY; mirrored = true; } } else { if (array[y - 1][x] && array[y - 1][x - 1]) { dirY = -1 * dirY; mirrored = true; } } if (!mirrored) { if (dirX > 0) { if (dirY > 0) { if (array[y][x]) { return false; } } else { if (array[y - 1][x]) { return false; } } } else { if (dirY > 0) { if (array[y][x - 1]) { return false; } } else { if (array[y - 1][x - 1]) { return false; } } } } } else { int x = posX.z / 2; if (x >= array[0].length || x == 0) { return false; } int y = posY.z / posY.n / 2; if (dirX > 0 && array[y][x] || dirX < 0 && array[y][x - 1]) { dirX = -1 * dirX; } } } else if (posY.n == 1 && posY.z % 2 == 0) { int y = posY.z / 2; if (y >= array.length || y == 0) { return false; } int x = posX.z / posX.n / 2; if (dirY > 0 && array[y][x] || dirY < 0 && array[y - 1][x]) { dirY = -1 * dirY; } } distance += step(dirX, dirY); } return false; } // steps until the next coord becomes integer private static double step(int dirX, int dirY) { if (dirY == 0) { if (dirX > 0) { posX = posX.plus(Rational.one); } else { posX = posX.minus(Rational.one); } return 1; } if (dirX == 0) { if (dirY > 0) { posY = posY.plus(Rational.one); } else { posY = posY.minus(Rational.one); } return 1; } if (posX.n == 1) { Rational distY = posY.fractal(); Rational speed = new Rational(Math.abs(dirY), Math.abs(dirX)); if (dirY > 0) { distY = Rational.one.minus(distY); } if (distY.equals(Rational.zero)) { distY = Rational.one; } if (distY.minus(speed).positive()) { if (dirX > 0) { posX = posX.plus(Rational.one); } else { posX = posX.minus(Rational.one); } if (dirY > 0) { posY = posY.plus(speed); } else { posY = posY.minus(speed); } return Math.sqrt(1 + speed.times(speed).value()); } else { if (dirY > 0) { posY = posY.plus(distY); } else { posY = posY.minus(distY); } Rational distX = distY.divides(speed); if (dirX > 0) { posX = posX.plus(distX); } else { posX = posX.minus(distX); } return Math.sqrt(distY.times(distY).value() + distX.times(distX).value()); } } else { Rational distX = posX.fractal(); Rational speed = new Rational(Math.abs(dirX), Math.abs(dirY)); if (dirX > 0) { distX = Rational.one.minus(distX); } if (distX.minus(speed).positive()) { if (dirY > 0) { posY = posY.plus(Rational.one); } else { posY = posY.minus(Rational.one); } if (dirX > 0) { posX = posX.plus(speed); } else { posX = posX.minus(speed); } return Math.sqrt(1 + speed.times(speed).value()); } else { if (dirX > 0) { posX = posX.plus(distX); } else { posX = posX.minus(distX); } Rational distY = distX.divides(speed); if (dirY > 0) { posY = posY.plus(distY); } else { posY = posY.minus(distY); } return Math.sqrt(distY.times(distY).value() + distX.times(distX).value()); } } } // private static void out(boolean[] array) { // System.out.println(Arrays.toString(array)); // } // // private static void out(boolean[][] array) { // int count = 0; // for (boolean[] a : array) { // System.out.print(count++ + ":"); // out(a); // } // } private static class Rational { static final Rational one = new Rational(1, 1); static final Rational zero = new Rational(0, 1); public int z; public int n; // create and initialize a new Rational object public Rational(int z, int n) { if (n == 0) { throw new RuntimeException("Denominator is zero"); } int g = gcd(z, n); this.z = z / g; this.n = n / g; } // return string representation of (this) public String toString() { if (n == 1) { return z + ""; } else { return z + "/" + n; } } // return (this * b) public Rational times(Rational b) { return new Rational(this.z * b.z, this.n * b.n); } // return (this + b) public Rational plus(Rational b) { int z = (this.z * b.n) + (this.n * b.z); int n = this.n * b.n; return new Rational(z, n); } // return (this - b) public Rational minus(Rational b) { int z = (this.z * b.n) - (this.n * b.z); int n = this.n * b.n; return new Rational(z, n); } // return fractal amount public Rational fractal() { return new Rational(z % n, n); } // return (1 / this) public Rational reciprocal() { return new Rational(n, z); } // return (this / b) public Rational divides(Rational b) { return this.times(b.reciprocal()); } public boolean positive() { return z * n >= 0; } public boolean equals(Rational r) { return r.z == this.z && r.n == this.n; } public double value() { return 1.0 * z / n; } private int gcd(int m, int n) { if (0 == n) return m; else return gcd(n, m % n); } } }
0
3,555
C20060
C20067
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
import java.io.*; import java.util.*; public class Solution { private StringTokenizer st; private BufferedReader in; private PrintWriter out; final String file = "D-large"; public void solve() throws IOException { int tests = nextInt(); for (int test = 0; test < tests; ++test) { int n = nextInt(); int m = nextInt(); int d = nextInt(); char[][] f = new char[n][m]; int x0 = -1, y0 = -1; for (int i = 0; i < n; ++i) { f[i] = next().toCharArray(); for (int j = 0; j < m; ++j) { if (f[i][j] == 'X') { x0 = i; y0 = j; } } } int ans = 0; for (int dx = -d; dx <= d; ++dx) { for (int dy = -d; dy <= d; ++dy) { if ((dx != 0 || dy != 0) && dx * dx + dy * dy <= d * d && raytrace(x0, y0, x0 + dx, y0 + dy, f, 0.)) { // System.err.println(dx + " " + dy); ans++; } } } System.err.printf("Case #%d: %d%n", test + 1, ans); out.printf("Case #%d: %d%n", test + 1, ans); } } final double EPS = 1e-8; private boolean raytrace(int x0, int y0, int x1, int y1, char[][] f, double t) { double firstt = Double.POSITIVE_INFINITY; int dx = x1 - x0; int dy = y1 - y0; double tx = Double.POSITIVE_INFINITY; for (int i = Math.max(0, Math.min(x0, x1)); i < f.length && i <= Math.max(x0, x1); ++i) { for (int j = Math.max(0, Math.min(y0, y1)); j < f[0].length && j <= Math.max(y0, y1); ++j) { if (f[i][j] == 'X') { double tt = dx != 0 ? (double)(i - x0) / dx : (double)(j - y0) / dy; if (Math.abs(x0 + dx * tt - i) < EPS && Math.abs(y0 + dy * tt - j) < EPS) { tx = tt; } } if (f[i][j] != '#') { continue; } double minx = dx == 0 ? Double.NEGATIVE_INFINITY : Math.min((i - 0.5 - x0) / dx, (i + 0.5 - x0) / dx); double maxx = dx == 0 ? Double.POSITIVE_INFINITY : Math.max((i - 0.5 - x0) / dx, (i + 0.5 - x0) / dx); double miny = dy == 0 ? Double.NEGATIVE_INFINITY : Math.min((j - 0.5 - y0) / dy, (j + 0.5 - y0) / dy); double maxy = dy == 0 ? Double.POSITIVE_INFINITY : Math.max((j - 0.5 - y0) / dy, (j + 0.5 - y0) / dy); if (maxx < miny - EPS || maxy < minx - EPS) { continue; } double tt = Math.max(minx, miny); if (tt > t + EPS) { firstt = Math.min(firstt, tt); } } } if (firstt > 1.) { return x1 >= 0 && x1 < f.length && y1 >= 0 && y1 < f[0].length && f[x1][y1] == 'X'; } if (tx > t + EPS && tx < firstt) { return false; } int sx = Integer.signum(dx); int sy = Integer.signum(dy); double x = x0 + dx * firstt - 0.5; double y = y0 + dy * firstt - 0.5; int rx = (int)Math.round(x); int ry = (int)Math.round(y); if (Math.abs(rx - x) < EPS && Math.abs(ry - y) < EPS) { if (dx == 0 || dy == 0) { throw new AssertionError(); } boolean f11 = get(f, rx + (1 + sx) / 2, ry + (1 + sy) / 2); boolean f01 = get(f, rx + (1 - sx) / 2, ry + (1 + sy) / 2); boolean f10 = get(f, rx + (1 + sx) / 2, ry + (1 - sy) / 2); if (get(f, rx + (1 - sx) / 2, ry + (1 - sy) / 2) || !f11 && !f01 && !f10) { throw new AssertionError(); } if (!f11) { return raytrace(x0, y0, x1, y1, f, firstt); } if (f01 && f10 && f11) { return raytrace(2 * rx + 1 - x0, 2 * ry + 1 - y0, 2 * rx + 1 - x1, 2 * ry + 1 - y1, f, firstt); } if (f10 && f11) { return raytrace(2 * rx + 1 - x0, y0, 2 * rx + 1 - x1, y1, f, firstt); } if (f01 && f11) { return raytrace(x0, 2 * ry + 1 - y0, x1, 2 * ry + 1 - y1, f, firstt); } return false; } if (Math.abs(rx - x) < EPS) { return raytrace(2 * rx + 1 - x0, y0, 2 * rx + 1 - x1, y1, f, firstt); } if (Math.abs(ry - y) < EPS) { return raytrace(x0, 2 * ry + 1 - y0, x1, 2 * ry + 1 - y1, f, firstt); } throw new AssertionError(); } private boolean get(char[][] f, int i, int j) { return i >= 0 && i < f.length && j >= 0 && j < f[0].length && f[i][j] == '#'; } public void run() throws IOException { in = new BufferedReader(new FileReader(file + ".in")); out = new PrintWriter(file + ".out"); eat(""); solve(); out.close(); } void eat(String s) { st = new StringTokenizer(s); } String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } eat(line); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); new Solution().run(); } }
0
3,556
C20060
C20008
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; public class SimpleMirrors { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader(args[0])); PrintWriter out = new PrintWriter(args[1]); // number of testcases String sCount = in.readLine(); int count = Integer.parseInt(sCount); for(int idx=1; idx<=count; idx++) { String[] parts = in.readLine().split(" "); int h = Integer.parseInt(parts[0]); int w = Integer.parseInt(parts[1]); int d = Integer.parseInt(parts[2]); int x = -1, y = -1; // small dataset => just find the "X" for(int i=0; i<h; i++) { String l = in.readLine(); int p = l.indexOf('X'); if(p > -1) { y = 10*i - 5; x = 10*p - 5; } } out.println("Case #" + idx + ": " + process(10*(h-2), 10*(w-2), 10*d, x, y)); } out.close(); } private static int process(int h, int w, int d, int ox, int oy) { System.out.println("h=" + h + ", w=" + w + ", d=" + d + ", ox=" + ox + ", oy=" + oy); MirrorSet ms = new MirrorSet( Arrays.asList(new Mirror[] { new Mirror(Facing.POS, Orient.HORIZ, 0, 0, w), new Mirror(Facing.NEG, Orient.HORIZ, h, 0, w), new Mirror(Facing.POS, Orient.VERT, 0, 0, h), new Mirror(Facing.NEG, Orient.VERT, w, 0, h), }), ox, oy, d, w, h ); Set<Point> pts = ms.transitiveImages(); Set<Double> angles = ms.angles(pts); System.out.println(" " + pts.size() + ": " + pts); System.out.println(" " + angles.size() + ": " + angles); return angles.size(); } private static class MirrorSet { private final Collection<Mirror> mirrors; private final int ox, oy, limit, w, h; public MirrorSet(Collection<Mirror> mirrors, int ox, int oy, int limit, int w, int h) { this.mirrors = mirrors; this.ox = ox; this.oy = oy; this.limit = limit; this.w = w; this.h = h; } public Set<Point> images(Set<Point> in) { HashSet<Point> out = new HashSet<Point>(); for(Point i : in) { for(Mirror m : mirrors) { Point o = m.image(i); if(o != null && ! in.contains(o) && Math.sqrt((ox-o.x)*(ox-o.x) + (oy-o.y)*(oy-o.y)) <= limit) out.add(o); } } return out; } public Set<Point> transitiveImages() { Set<Point> im = new HashSet<Point>(); im.add(new Point(ox, oy)); Set<Point> newer; do { newer = images(im); im.addAll(newer); } while(newer.size() > 0); return im; } public Set<Double> angles(Set<Point> in) { Set<Double> out = new HashSet<Double>(); for(Point i : in) { if(i.x != ox || i.y != oy) out.add(Math.atan2(i.y-oy, i.x-ox)); } return out; } } private static enum Facing { POS, NEG } private static enum Orient { VERT, HORIZ } private static class Mirror { private final Facing facing; private final Orient orient; private final int pos, start, stop; public Mirror(Facing facing, Orient orient, int pos, int start, int stop) { this.facing = facing; this.orient = orient; this.pos = pos; this.start = start; this.stop = stop; } public Point image(Point p) { switch(orient) { case VERT: if(facing == Facing.POS && p.x >= pos || facing == Facing.NEG && p.x <= pos) { return new Point(2 * pos - p.x, p.y); } else return null; case HORIZ: if(facing == Facing.POS && p.y >= pos || facing == Facing.NEG && p.y <= pos) { return new Point(p.x, 2 * pos - p.y); } else return null; } return null; } } private static class Point { public final int x, y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { return obj instanceof Point && ((Point)obj).x == x && ((Point)obj).y == y; } @Override public int hashCode() { return (x << 16) ^ y; } @Override public String toString() { return "(" + x + ", " + y + ")"; } } }
0
3,557
C20060
C20063
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
0
3,558
C20060
C20015
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
0
3,559
C20060
C20050
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,560
C20060
C20085
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
0
3,561
C20060
C20020
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
0
3,562
C20060
C20062
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,563
C20060
C20059
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
0
3,564
C20060
C20064
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
0
3,565
C20060
C20076
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
0
3,566
C20060
C20026
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
import java.io.*; import java.util.*; import java.math.*; class D { private static final boolean DEBUG_ON = true; private static final boolean ECHO_ON = true; private static BufferedReader input; private static BufferedWriter output; private static final int INF = Integer.MAX_VALUE / 2; private static final int MOD = 10007; private static int H, W, D, row, col; public static int gcd(int n, int m) {return (0 == m) ? (n) : gcd(m, n % m);} public static int sqrt(int X) { int answer = 1, interval = 1; for (int i = String.valueOf(X).length() / 2; i > 0; i--) {interval *= 10;} while (interval >= 1) { while ((answer * answer) <= X) {answer += interval;} answer -= interval; interval /= 10; } return answer; } public static void main(String[] args) { try { input = new BufferedReader(new FileReader(args[0] + ".in")); output = new BufferedWriter(new FileWriter(args[0] + ".out")); String line = input.readLine(); int testcases = getInt(line, 0); for (int testcase = 1; testcase <= testcases; testcase++) { char[][] real = getCharMatrix(input); HashSet<Integer> valid = new HashSet<Integer>(); for (int i = row - D; i <= row + D; i++) { int range = sqrt((D * D) - ((i - row) * (i - row))); for (int j = col - range; j <= col + range; j++) { int diffX = i - row; int diffY = j - col; if (0 == diffX && 0 == diffY) {continue;} int gcd = gcd(Math.abs(diffX), Math.abs(diffY)); int direction = (((diffX/gcd) + D) << 16) + ((diffY/gcd) + D); if (valid.contains(direction)) {continue;} int x = 100 * row + 50; int y = 100 * col + 50; for (int k = 0; k < 100; k++) { int nextX = x + diffX; int nextY = y + diffY; int xCell = x / 100; int yCell = y / 100; int nextXCell = nextX / 100; if (0 == nextX % 100) {nextXCell = (nextX + diffX) / 100;} int nextYCell = nextY / 100; if (0 == nextY % 100) {nextYCell = (nextY + diffY) / 100;} if (xCell == nextXCell && yCell == nextYCell) {x = nextX; y = nextY;} else if (xCell != nextXCell && yCell == nextYCell) { if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;} else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if (xCell == nextXCell && yCell != nextYCell) { if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;} else { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else { int cornerX = -1, cornerY = -1; if (nextXCell < xCell && nextYCell < yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * (nextYCell + 1);} else if (nextXCell > xCell && nextYCell < yCell) {cornerX = 100 * nextXCell; cornerY = 100 * (nextYCell + 1);} else if (nextXCell < xCell && nextYCell > yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * nextYCell;} else if (nextXCell > xCell && nextYCell > yCell) {cornerX = 100 * nextXCell; cornerY = 100 * nextYCell;} if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { diffX = -diffX; diffY = -diffY; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {x = nextX; y = nextY;} // passing corner else { diffX = -diffX; diffY = -diffY; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {break;} // hitting corner else if (Math.abs((cornerX - x) * (nextY - y)) < Math.abs((nextX - x) * (cornerY - y))) // hitting Y { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { if (Math.abs((cornerX - x) * (nextY - y)) <= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;} else { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { if (Math.abs((cornerX - x) * (nextY - y)) >= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;} else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) {x = nextX; y = nextY;} } if ((100 * row + 50) == x && (100 * col + 50) == y) {valid.add(direction); break;} } } } String result = "Case #" + testcase + ": " + valid.size(); output(result); } input.close(); output.close(); } catch (Exception e) { e.printStackTrace(); } } public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));} public static long getLong(String line, int index) {return Long.parseLong(getString(line, index));} public static double getDouble(String line, int index) {return Double.parseDouble(getString(line, index));} public static String getString(String line, int index) { line = line.trim(); while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;} if ((-1) == line.indexOf(' ')) {return line;} else {return line.substring(0, line.indexOf(' '));} } public static int[] getIntArray(String line) { String[] strings = getStringArray(line); int[] numbers = new int[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Integer.parseInt(strings[i]);} return numbers; } public static long[] getLongArray(String line) { String[] strings = getStringArray(line); long[] numbers = new long[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Long.parseLong(strings[i]);} return numbers; } public static double[] getDoubleArray(String line) { String[] strings = getStringArray(line); double[] numbers = new double[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Double.parseDouble(strings[i]);} return numbers; } public static String[] getStringArray(String line) {return line.trim().split("(\\s)+", 0);} public static int[] getIntArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); int[] numbers = new int[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Integer.parseInt(strings[i - begin]);} return numbers; } public static long[] getLongArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); long[] numbers = new long[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Long.parseLong(strings[i - begin]);} return numbers; } public static double[] getDoubleArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); double[] numbers = new double[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Double.parseDouble(strings[i - begin]);} return numbers; } public static String[] getStringArray(String line, int begin, int end) { String[] lines = line.trim().split("(\\s)+", 0); String[] results = new String[end - begin]; for (int i = begin; i < end; i++) {results[i - begin] = lines[i];} return results; } public static char[][] getCharMatrix(BufferedReader input) throws Exception { String line = input.readLine(); H = getInt(line, 0); W = getInt(line, 1); D = getInt(line, 2); char[][] matrix = new char[H][W]; for (int i = 0; i < H; i++) { line = input.readLine(); for (int j = 0; j < W; j++) { char c = matrix[i][j] = line.charAt(j); if ('X' == c) {row = i; col = j;} } } return matrix; } public static int[][] getIntMatrix(BufferedReader input) throws Exception { String line = input.readLine(); int R = getInt(line, 0); int C = getInt(line, 1); int[][] matrix = new int[R][C]; for (int i = 0; i < R; i++) { line = input.readLine(); for (int j = 0; j < C; j++) {matrix[i][j] = getInt(line, j);} } return matrix; } public static boolean[][] newBooleanMatrix(int R, int C, boolean value) { boolean[][] matrix = new boolean[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static char[][] newCharMatrix(int R, int C, char value) { char[][] matrix = new char[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static int[][] newIntMatrix(int R, int C, int value) { int[][] matrix = new int[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static long[][] newLongMatrix(int R, int C, long value) { long[][] matrix = new long[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static double[][] newDoubleMatrix(int R, int C, double value) { double[][] matrix = new double[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static void output(String s) throws Exception { if (ECHO_ON) {System.out.println(s);} output.write(s); output.newLine(); } public static String toKey(boolean[] array) { StringBuffer buffer = new StringBuffer(array.length + ","); for (int i = 0; i < array.length / 16; i++) { char c = 0; for (int j = 0; j < 16; j++) { c <<= 1; if (array[i * 16 + j]) {c += 1;} } buffer.append(c + ""); } char c = 0; for (int j = 0; j < (array.length % 16); j++) { c <<= 1; if (array[(array.length / 16) * 16 + j]) {c += 1;} } buffer.append(c + ""); return buffer.toString(); } public static String toKey(int[] array, int bit) { StringBuffer buffer = new StringBuffer(array.length + ","); if (bit > 16) { for (int i = 0; i < array.length; i++) { char c1 = (char)(array[i] >> 16); char c2 = (char)(array[i] & 0xFFFF); buffer.append("" + c1 + c2); } } else { int n = 16 / bit; for (int i = 0; i < array.length / n; i++) { char c = 0; for (int j = 0; j < n; j++) { c <<= bit; c += array[i * n + j]; } buffer.append(c + ""); } char c = 0; for (int j = 0; j < (array.length % n); j++) { c <<= bit; c += array[(array.length / n) * n + j]; } buffer.append(c + ""); } return buffer.toString(); } public static void debug(String s) {if (DEBUG_ON) {System.out.println(s);}} public static void debug(String s0, double l0) {if (DEBUG_ON) {System.out.println(s0+" = "+l0);}} public static void debug(String s0, double l0, String s1, double l1) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2) {if (DEBUG_ON) { System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3, String s4, double l4) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3+"; "+s4+" = "+l4);}} public static void debug(boolean[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(boolean[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append((array[i] == true ? "1" : "0") + separator);} buffer.append((array[array.length - 1] == true) ? "1" : "0"); System.out.println(buffer.toString()); } } public static void debug(boolean[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(boolean[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} public static void debug(char[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(char[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);} buffer.append(array[array.length - 1]); System.out.println(buffer.toString()); } } public static void debug(char[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(char[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} public static void debug(int[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(int[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);} buffer.append(array[array.length - 1]); System.out.println(buffer.toString()); } } public static void debug(int[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(int[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} }
0
3,567
C20060
C20078
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
import java.awt.Point; public class FracVec { public final Frac row, col; public FracVec(Frac row, Frac col) { this.row = row; this.col = col; } public FracVec(Point pos) { row = new Frac(pos.y); col = new Frac(pos.x); } public FracVec flipHoriz() { return new FracVec(row, col.neg()); } public FracVec flipVert() { return new FracVec(row.neg(), col); } public boolean isCorner() { return row.denom == 1 && col.denom == 1; } public FracVec add(FracVec other) { return new FracVec(row.add(other.row), col.add(other.col)); } public FracVec sub(FracVec other) { return new FracVec(row.sub(other.row), col.sub(other.col)); } public FracVec neg() { return new FracVec(row.neg(), col.neg()); } public boolean equals(FracVec other) { return row.equals(other.row) && col.equals(other.col); } public boolean equals(Point p) { return row.equals(p.y) && col.equals(p.x); } @Override public boolean equals(Object other) { return other instanceof FracVec && equals((FracVec) other); } @Override public int hashCode() { return (31 + row.hashCode()) * 31 + col.hashCode(); } public String toString() { return "( " + row + ", " + col + " )"; } public FracVec multiply(Frac scalar) { return new FracVec(row.mult(scalar), col.mult(scalar)); } public int compareLengthTo(int d) { return row.mult(row).add(col.mult(col)).compareTo(new Frac(d * d)); } public Point getNextCell(FracVec dm) { int resRow, resCol; if (dm.row.sgn() < 0) { resRow = row.roundDownAddSmallDown(); } else { resRow = row.roundDownAddSmallUp(); } if (dm.col.sgn() < 0) resCol = col.roundDownAddSmallDown(); else resCol = col.roundDownAddSmallUp(); return new Point(resCol, resRow); } }
0
3,568
C20060
C20057
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package jp.funnything.competition.util; import java.util.Arrays; import java.util.List; import com.google.common.collect.Lists; import com.google.common.primitives.Ints; import com.google.common.primitives.Longs; public class Prime { public static class PrimeData { public int[] list; public boolean[] map; private PrimeData( final int[] values , final boolean[] map ) { list = values; this.map = map; } } public static long[] factorize( long n , final int[] primes ) { final List< Long > factor = Lists.newArrayList(); for ( final int p : primes ) { if ( n < p * p ) { break; } while ( n % p == 0 ) { factor.add( ( long ) p ); n /= p; } } if ( n > 1 ) { factor.add( n ); } return Longs.toArray( factor ); } public static PrimeData prepare( final int n ) { final List< Integer > primes = Lists.newArrayList(); final boolean[] map = new boolean[ n ]; Arrays.fill( map , true ); map[ 0 ] = map[ 1 ] = false; primes.add( 2 ); for ( int composite = 2 * 2 ; composite < n ; composite += 2 ) { map[ composite ] = false; } for ( int value = 3 ; value < n ; value += 2 ) { if ( map[ value ] ) { primes.add( value ); for ( int composite = value * 2 ; composite < n ; composite += value ) { map[ composite ] = false; } } } return new PrimeData( Ints.toArray( primes ) , map ); } }
0
3,569
C20060
C20030
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; /** * * @author jim */ public class RayVector { public final int x; public final int y; public RayVector(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { if (obj instanceof RayVector) { RayVector v = (RayVector)obj; if (v.x * y == v.y * x) return true; } return false; } @Override public int hashCode() { int hash = 7; hash = 41 * hash + this.x; hash = 41 * hash + this.y; return hash; } public Fraction getScalarGardient() { return new Fraction(Math.abs(y), Math.abs(x)); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("("); sb.append(x); sb.append(","); sb.append(y); sb.append(")"); return sb.toString(); } }
0
3,570
C20060
C20002
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
import java.util.*; import java.io.*; import java.math.*; import java.awt.*; import static java.lang.Math.*; import static java.lang.Integer.parseInt; import static java.lang.Double.parseDouble; import static java.lang.Long.parseLong; import static java.lang.System.*; import static java.util.Arrays.*; import static java.util.Collection.*; public class D { static int gcd(int a, int b) { return b == 0 ? a : a == 0 ? b : gcd(b, a%b); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(in)); int T = parseInt(br.readLine()); for(int t = 0; t++ < T; ) { String[] line = br.readLine().split(" "); int H = parseInt(line[0]), W = parseInt(line[1]), D = parseInt(line[2]); char[][] G = new char[H][]; for(int h = 0; h < H; h++) G[h] = br.readLine().toCharArray(); int X = 0, Y = 0; outer:for(Y = 0; Y < H; Y++) for(X = 0; X < W; X++) if(G[Y][X] == 'X') break outer; int count = 0; for(int i = -D; i <= D; i++) { for(int j = -D; j <= D; j++) { int dx = i, dy = j, scale = 2 * Math.abs((dx == 0 ? 1 : dx) * (dy == 0 ? 1 : dy)), x0, y0, x, y; int steps = (int)Math.floor(scale * D / Math.sqrt(dx * dx + dy * dy)); if(gcd(Math.abs(dx), Math.abs(dy)) != 1) continue; x0 = x = X * scale + scale / 2; y0 = y = Y * scale + scale / 2; do { steps -= 1; if(x % scale == 0 && y % scale == 0) { // at a corner int dxi = dx > 0 ? 1 : -1, dyi = dy > 0 ? 1 : -1; int xi = (x / scale) - (dxi + 1) / 2, yi = (y / scale) - (dyi + 1) / 2; if(G[yi+dyi][xi+dxi] == '#') { if(G[yi+dyi][xi] != '#' && G[yi][xi+dxi] != '#') steps = -1; // kill the light if(G[yi+dyi][xi] == '#') dy *= -1; if(G[yi][xi+dxi] == '#') dx *= -1; } else ; // otherwise step as normal } else if(x % scale == 0) { int xi = x / scale, yi = y / scale; if(G[yi][xi] == '#' || G[yi][xi-1] == '#') dx *= -1; } else if(y % scale == 0) { int xi = x / scale, yi = y / scale; if(G[yi][xi] == '#' || G[yi-1][xi] == '#') dy *= -1; } else ; // smooth sailing x += dx; y += dy; } while(steps >= 0 && !(x == x0 && y == y0)); if(steps >= 0) ++count; } } out.println("Case #" + t +": " + count) ; } } }
0
3,571
C20060
C20056
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package jp.funnything.competition.util; import java.math.BigDecimal; /** * Utility for BigDeciaml */ public class BD { public static BigDecimal ZERO = BigDecimal.ZERO; public static BigDecimal ONE = BigDecimal.ONE; public static BigDecimal add( final BigDecimal x , final BigDecimal y ) { return x.add( y ); } public static BigDecimal add( final BigDecimal x , final double y ) { return add( x , v( y ) ); } public static BigDecimal add( final double x , final BigDecimal y ) { return add( v( x ) , y ); } public static int cmp( final BigDecimal x , final BigDecimal y ) { return x.compareTo( y ); } public static int cmp( final BigDecimal x , final double y ) { return cmp( x , v( y ) ); } public static int cmp( final double x , final BigDecimal y ) { return cmp( v( x ) , y ); } public static BigDecimal div( final BigDecimal x , final BigDecimal y ) { return x.divide( y ); } public static BigDecimal div( final BigDecimal x , final double y ) { return div( x , v( y ) ); } public static BigDecimal div( final double x , final BigDecimal y ) { return div( v( x ) , y ); } public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) { return x.multiply( y ); } public static BigDecimal mul( final BigDecimal x , final double y ) { return mul( x , v( y ) ); } public static BigDecimal mul( final double x , final BigDecimal y ) { return mul( v( x ) , y ); } public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) { return x.subtract( y ); } public static BigDecimal sub( final BigDecimal x , final double y ) { return sub( x , v( y ) ); } public static BigDecimal sub( final double x , final BigDecimal y ) { return sub( v( x ) , y ); } public static BigDecimal v( final double value ) { return BigDecimal.valueOf( value ); } }
0
3,572
C20060
C20018
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package template; import java.util.Objects; public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{ private T1 o1; private T2 o2; public Pair(T1 o1, T2 o2) { this.o1 = o1; this.o2 = o2; } @Override public boolean equals(Object other) { if (!(other instanceof Pair)) {return false;} return (compareTo((Pair<T1, T2>)other) == 0); } @Override public int compareTo(Pair<T1, T2> other) { int c1 = ((Comparable<T1>) o1).compareTo(other.getO1()); if (c1 != 0) {return c1;} int c2 = ((Comparable<T2>) o2).compareTo(other.getO2()); return c2; } @Override public int hashCode() { int hash = 5; hash = 83 * hash + Objects.hashCode(this.o1); hash = 83 * hash + Objects.hashCode(this.o2); return hash; } @Override public String toString() { return "[" + o1 + ", " + o2 + "]"; } public T1 getO1() { return o1; } public void setO1(T1 o1) { this.o1 = o1; } public T2 getO2() { return o2; } public void setO2(T2 o2) { this.o2 = o2; } }
0
3,573
C20060
C20051
package jp.funnything.competition.util; import java.math.BigInteger; /** * Utility for BigInteger */ public class BI { public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = BigInteger.ONE; public static BigInteger add( final BigInteger x , final BigInteger y ) { return x.add( y ); } public static BigInteger add( final BigInteger x , final long y ) { return add( x , v( y ) ); } public static BigInteger add( final long x , final BigInteger y ) { return add( v( x ) , y ); } public static int cmp( final BigInteger x , final BigInteger y ) { return x.compareTo( y ); } public static int cmp( final BigInteger x , final long y ) { return cmp( x , v( y ) ); } public static int cmp( final long x , final BigInteger y ) { return cmp( v( x ) , y ); } public static BigInteger div( final BigInteger x , final BigInteger y ) { return x.divide( y ); } public static BigInteger div( final BigInteger x , final long y ) { return div( x , v( y ) ); } public static BigInteger div( final long x , final BigInteger y ) { return div( v( x ) , y ); } public static BigInteger mod( final BigInteger x , final BigInteger y ) { return x.mod( y ); } public static BigInteger mod( final BigInteger x , final long y ) { return mod( x , v( y ) ); } public static BigInteger mod( final long x , final BigInteger y ) { return mod( v( x ) , y ); } public static BigInteger mul( final BigInteger x , final BigInteger y ) { return x.multiply( y ); } public static BigInteger mul( final BigInteger x , final long y ) { return mul( x , v( y ) ); } public static BigInteger mul( final long x , final BigInteger y ) { return mul( v( x ) , y ); } public static BigInteger sub( final BigInteger x , final BigInteger y ) { return x.subtract( y ); } public static BigInteger sub( final BigInteger x , final long y ) { return sub( x , v( y ) ); } public static BigInteger sub( final long x , final BigInteger y ) { return sub( v( x ) , y ); } public static BigInteger v( final long value ) { return BigInteger.valueOf( value ); } }
package jp.funnything.competition.util; import java.util.List; import com.google.common.collect.Lists; public class Lists2 { public static < T > List< T > newArrayListAsArray( final int length ) { final List< T > list = Lists.newArrayListWithCapacity( length ); for ( int index = 0 ; index < length ; index++ ) { list.add( null ); } return list; } }
0
3,574
C20004
C20005
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package gcj; import com.sun.org.apache.xerces.internal.impl.xs.opti.DefaultXMLDocumentHandler; import java.util.*; import java.io.*; public class HallOfMirrors { final static String PROBLEM_NAME = "mirrors"; final static String WORK_DIR = "D:\\Gcj\\" + PROBLEM_NAME + "\\"; int H, W, D; double stX, stY; int res = 0; String[] map; int gcd(int a, int b) { while (a>0 && b>0) if (a>b) a %= b; else b %= a; return a + b; } final double EPS = 1e-10; double getT(double cur, int V) { cur *= 2; V *= 2; if (V == 0) return 1e100; if (V > 0) { double want = Math.ceil(cur + EPS); return (want - cur) / V; } else { double want = Math.floor(cur - EPS); return (want - cur) / V; } } boolean isInteger(double x) { return Math.abs(x - Math.floor(x)) <= EPS || Math.abs(x - Math.ceil(x)) <= EPS; } boolean inMirror(double x, double y) { int xx = (int)Math.floor(x); int yy = (int)Math.floor(y); return map[xx].charAt(yy) == '#'; } void process(int dx, int dy) { double curX = stX, curY = stY; double dist = 0.0; while (true) { double t1 = getT(curX, dx); double t2 = getT(curY, dy); double t = Math.min(t1, t2); double nextX = curX + t * dx; double nextY = curY + t * dy; double piece = Math.sqrt((nextX - curX) * (nextX - curX) + (nextY - curY) * (nextY - curY)); dist += piece; if (dist > D + EPS) return; if (Math.abs(nextX - stX) <= EPS && Math.abs(nextY - stY) <= EPS) { res++; return; } curX = nextX; curY = nextY; boolean fx = isInteger(nextX); boolean fy = isInteger(nextY); if (fx && fy) { // corner // A B // C D boolean A = inMirror(nextX - EPS, nextY - EPS); boolean B = inMirror(nextX - EPS, nextY + EPS); boolean C = inMirror(nextX + EPS, nextY - EPS); boolean D = inMirror(nextX + EPS, nextY + EPS); int cnt = (A ? 1 : 0) + (B ? 1 : 0) + (C ? 1 : 0) + (D ? 1 : 0); if (cnt == 3) { dx = -dx; dy = -dy; } else if ((A && B && !C && !D) || (!A && !B && C & D)) { dx = -dx; } else if ((A && C && !B && !D) || (!A && !C && B && D)) { dy = -dy; } else if (inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY + (dy > 0 ? EPS : -EPS))) { return; } else { // just continue; } } else if (fx && !fy) { // horizontal if (dx != 0 && inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY)) dx = -dx; } else if (!fx && fy) { // vertical if (dy != 0 && inMirror(nextX, nextY + (dy > 0 ? EPS : -EPS))) dy = -dy; } else { // nothing } } } void solve(Scanner sc, PrintWriter pw) { H = sc.nextInt(); W = sc.nextInt(); D = sc.nextInt(); map = new String[H]; for (int i=0; i<H; i++) map[i] = sc.next(); for (int i=0; i<H; i++) for (int j=0; j<W; j++) if (map[i].charAt(j) == 'X') { stX = i + 0.5; stY = j + 0.5; } for (int dx=-D; dx<=D; dx++) for (int dy=-D; dy<=D; dy++) if (dx*dx + dy*dy > 0 && dx*dx + dy*dy <= D * D && gcd(Math.abs(dx), Math.abs(dy)) == 1) { process(dx, dy); } pw.println(res); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new FileReader(WORK_DIR + "input.txt")); PrintWriter pw = new PrintWriter(new FileWriter(WORK_DIR + "output.txt")); int caseCnt = sc.nextInt(); for (int caseNum=0; caseNum<caseCnt; caseNum++) { System.out.println("Processing test case " + (caseNum + 1)); pw.print("Case #" + (caseNum+1) + ": "); new HallOfMirrors().solve(sc, pw); } pw.flush(); pw.close(); sc.close(); } }
0
3,575
C20004
C20065
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package jp.funnything.prototype; import static java.lang.Math.abs; import java.io.File; import java.io.IOException; import jp.funnything.competition.util.CompetitionIO; import jp.funnything.competition.util.Packer; import org.apache.commons.io.FileUtils; import org.apache.commons.math.fraction.Fraction; import org.apache.commons.math.util.MathUtils; public class Runner { public static void main( final String[] args ) throws Exception { new Runner().run(); } boolean isValid( final int d , final boolean[][] map , final int ox , final int oy , int dx , int dy ) { final Fraction fox = new Fraction( ox * 2 + 1 ); final Fraction foy = new Fraction( oy * 2 + 1 ); Fraction x = new Fraction( ox * 2 + 1 ); Fraction y = new Fraction( oy * 2 + 1 ); Fraction sumDiffX = new Fraction( 0 ); Fraction sumDiffY = new Fraction( 0 ); for ( ; ; ) { final Fraction diffX = new Fraction( dx > 0 ? ( int ) Math.floor( x.doubleValue() + 1 ) : ( int ) Math.ceil( x.doubleValue() - 1 ) ).subtract( x ); final Fraction diffY = new Fraction( dy > 0 ? ( int ) Math.floor( y.doubleValue() + 1 ) : ( int ) Math.ceil( y.doubleValue() - 1 ) ).subtract( y ); if ( abs( diffX.doubleValue() * dy ) < abs( diffY.doubleValue() * dx ) ) { x = x.add( diffX ); y = y.add( diffX.multiply( dy ).divide( dx ) ); sumDiffX = sumDiffX.add( diffX.abs() ); sumDiffY = sumDiffY.add( diffX.multiply( dy ).divide( dx ).abs() ); } else { y = y.add( diffY ); x = x.add( diffY.multiply( dx ).divide( dy ) ); sumDiffY = sumDiffY.add( diffY.abs() ); sumDiffX = sumDiffX.add( diffY.multiply( dx ).divide( dy ).abs() ); } if ( sumDiffX.multiply( sumDiffX ).add( sumDiffY.multiply( sumDiffY ) ).compareTo( new Fraction( d * d * 2 * 2 ) ) > 0 ) { return false; } if ( x.equals( fox ) && y.equals( foy ) ) { return true; } final int nx = x.intValue() / 2 + ( dx > 0 ? 0 : -1 ); final int ny = y.intValue() / 2 + ( dy > 0 ? 0 : -1 ); if ( x.getDenominator() == 1 && x.getNumerator() % 2 == 0 && y.getDenominator() == 1 && y.getNumerator() % 2 == 0 ) { if ( map[ ny ][ nx ] ) { final int px = x.intValue() / 2 + ( dx > 0 ? -1 : 0 ); final int py = y.intValue() / 2 + ( dy > 0 ? -1 : 0 ); if ( map[ py ][ nx ] ) { if ( map[ ny ][ px ] ) { dx = -dx; dy = -dy; } else { dx = -dx; } } else { if ( map[ ny ][ px ] ) { dy = -dy; } else { return false; } } } } else { if ( x.getDenominator() == 1 && x.getNumerator() % 2 == 0 ) { if ( map[ y.intValue() / 2 ][ nx ] ) { dx = -dx; } } else if ( y.getDenominator() == 1 && y.getNumerator() % 2 == 0 ) { if ( map[ ny ][ x.intValue() / 2 ] ) { dy = -dy; } } } } } void pack() { try { final File dist = new File( "dist" ); if ( dist.exists() ) { FileUtils.deleteQuietly( dist ); } final File workspace = new File( dist , "workspace" ); FileUtils.copyDirectory( new File( "src/main/java" ) , workspace ); FileUtils.copyDirectory( new File( "../../../../CompetitionUtil/Lib/src/main/java" ) , workspace ); Packer.pack( workspace , new File( dist , "sources.zip" ) ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } void run() throws Exception { final CompetitionIO io = new CompetitionIO(); final int t = io.readInt(); for ( int index = 0 ; index < t ; index++ ) { final int[] values = io.readInts(); final int h = values[ 0 ]; final int w = values[ 1 ]; final int d = values[ 2 ]; final char[][] map = new char[ h ][]; for ( int y = 0 ; y < h ; y++ ) { final char[] l = io.read().toCharArray(); if ( l.length != w ) { throw new RuntimeException( "assert" ); } map[ y ] = l; } io.write( index + 1 , solve( d , map ) ); } io.close(); pack(); } int solve( final int d , final char[][] map ) { int count = 0; int ox = -1; int oy = -1; final boolean[][] parsed = new boolean[ map.length ][]; for ( int y = 0 ; y < map.length ; y++ ) { parsed[ y ] = new boolean[ map[ y ].length ]; for ( int x = 0 ; x < map[ y ].length ; x++ ) { final char c = map[ y ][ x ]; if ( c == '#' ) { parsed[ y ][ x ] = true; } if ( c == 'X' ) { ox = x; oy = y; } } } for ( int dy = -d ; dy <= d ; dy++ ) { for ( int dx = -d ; dx <= d ; dx++ ) { if ( dx == 0 && dy == 0 ) { continue; } if ( MathUtils.gcd( dx , dy ) != 1 ) { continue; } if ( dx * dx + dy * dy > d * d ) { continue; } if ( isValid( d , parsed , ox , oy , dx , dy ) ) { count++; } } } return count; } }
0
3,576
C20004
C20006
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package problemD; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ProblemD { static Rational posX = null; static Rational posY = null; static Rational tarX = null; static Rational tarY = null; static boolean[][] array = null; public static void main(String[] args) throws FileNotFoundException { // Scanner sc = new Scanner(new File("D-practice.in")); // Scanner sc = new Scanner(new File("D-small.in")); Scanner sc = new Scanner(new File("D-large.in")); int cases = sc.nextInt(); for (int i = 1; i <= cases; i++) { // do case things here int H = sc.nextInt(); int W = sc.nextInt(); int D = sc.nextInt(); D *= 2; array = new boolean[H][W]; for (int j = 0; j < H; j++) { String s = sc.next(); for (int k = 0; k < W; k++) { array[j][k] = (s.charAt(k) == '#'); if (s.charAt(k) == 'X') { posX = new Rational(2 * k + 1, 1); posY = new Rational(2 * j + 1, 1); tarX = posX; tarY = posY; } } } int count = 0; boolean checked[][] = new boolean[2 * D + 1][2 * D + 1]; for (int j = 2; j <= D; j += 2) { for (int x = -j; x <= j; x += 2) { if (D + j >= 0 && D + j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + j][D + x]) { if (followRay(j, x, D)) { count++; } int k = 1; while (D + (k * j) >= 0 && D + (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * j)][D + (k * x)] = true; k++; } } } if (D + j >= 0 && D + j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + x][D + j]) { if (followRay(x, j, D)) { count++; } int k = 1; while (D + (k * j) >= 0 && D + (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * x)][D + (k * j)] = true; k++; } } } if (D - j >= 0 && D - j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D - j][D + x]) { if (followRay(-j, x, D)) { count++; } int k = 1; while (D - (k * j) >= 0 && D - (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D - (k * j)][D + (k * x)] = true; k++; } } } if (D - j >= 0 && D - j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + x][D - j]) { if (followRay(x, -j, D)) { count++; } int k = 1; while (D - (k * j) >= 0 && D - (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * x)][D - (k * j)] = true; k++; } } } } } // System.out.println(count); System.out.format("Case #%d: %d\n", i, count); } } private static boolean followRay(int dirX, int dirY, int max) { double distance = 0; posX = tarX; posY = tarY; distance += step(dirX, dirY); while (distance <= max) { if (tarX.equals(posX) && tarY.equals(posY)) { return true; } // check mirror, adjust direction if (dirX == 0) { if (posY.n == 1 && posY.z % 2 == 0) { int y = posY.z / 2; if (y >= array.length || y == 0) { return false; } int x = posX.z / posX.n / 2; if (dirY > 0 && array[y][x] || dirY < 0 && array[y - 1][x]) { dirY = -1 * dirY; } } } else if (dirY == 0) { if (posX.n == 1 && posX.z % 2 == 0) { int x = posX.z / 2; if (x >= array[0].length || x == 0) { return false; } int y = posY.z / posY.n / 2; if (dirX > 0 && array[y][x] || dirX < 0 && array[y][x - 1]) { dirX = -1 * dirX; } } } if (posX.n == 1 && posX.z % 2 == 0) { if (posY.n == 1 && posY.z % 2 == 0) { // corner int x = posX.z / 2; int y = posY.z / 2; boolean mirrored = false; if (dirX > 0) { if (array[y][x] && array[y - 1][x]) { dirX = -1 * dirX; mirrored = true; } } else { if (array[y][x - 1] && array[y - 1][x - 1]) { dirX = -1 * dirX; mirrored = true; } } if (dirY > 0) { if (array[y][x] && array[y][x - 1]) { dirY = -1 * dirY; mirrored = true; } } else { if (array[y - 1][x] && array[y - 1][x - 1]) { dirY = -1 * dirY; mirrored = true; } } if (!mirrored) { if (dirX > 0) { if (dirY > 0) { if (array[y][x]) { return false; } } else { if (array[y - 1][x]) { return false; } } } else { if (dirY > 0) { if (array[y][x - 1]) { return false; } } else { if (array[y - 1][x - 1]) { return false; } } } } } else { int x = posX.z / 2; if (x >= array[0].length || x == 0) { return false; } int y = posY.z / posY.n / 2; if (dirX > 0 && array[y][x] || dirX < 0 && array[y][x - 1]) { dirX = -1 * dirX; } } } else if (posY.n == 1 && posY.z % 2 == 0) { int y = posY.z / 2; if (y >= array.length || y == 0) { return false; } int x = posX.z / posX.n / 2; if (dirY > 0 && array[y][x] || dirY < 0 && array[y - 1][x]) { dirY = -1 * dirY; } } distance += step(dirX, dirY); } return false; } // steps until the next coord becomes integer private static double step(int dirX, int dirY) { if (dirY == 0) { if (dirX > 0) { posX = posX.plus(Rational.one); } else { posX = posX.minus(Rational.one); } return 1; } if (dirX == 0) { if (dirY > 0) { posY = posY.plus(Rational.one); } else { posY = posY.minus(Rational.one); } return 1; } if (posX.n == 1) { Rational distY = posY.fractal(); Rational speed = new Rational(Math.abs(dirY), Math.abs(dirX)); if (dirY > 0) { distY = Rational.one.minus(distY); } if (distY.equals(Rational.zero)) { distY = Rational.one; } if (distY.minus(speed).positive()) { if (dirX > 0) { posX = posX.plus(Rational.one); } else { posX = posX.minus(Rational.one); } if (dirY > 0) { posY = posY.plus(speed); } else { posY = posY.minus(speed); } return Math.sqrt(1 + speed.times(speed).value()); } else { if (dirY > 0) { posY = posY.plus(distY); } else { posY = posY.minus(distY); } Rational distX = distY.divides(speed); if (dirX > 0) { posX = posX.plus(distX); } else { posX = posX.minus(distX); } return Math.sqrt(distY.times(distY).value() + distX.times(distX).value()); } } else { Rational distX = posX.fractal(); Rational speed = new Rational(Math.abs(dirX), Math.abs(dirY)); if (dirX > 0) { distX = Rational.one.minus(distX); } if (distX.minus(speed).positive()) { if (dirY > 0) { posY = posY.plus(Rational.one); } else { posY = posY.minus(Rational.one); } if (dirX > 0) { posX = posX.plus(speed); } else { posX = posX.minus(speed); } return Math.sqrt(1 + speed.times(speed).value()); } else { if (dirX > 0) { posX = posX.plus(distX); } else { posX = posX.minus(distX); } Rational distY = distX.divides(speed); if (dirY > 0) { posY = posY.plus(distY); } else { posY = posY.minus(distY); } return Math.sqrt(distY.times(distY).value() + distX.times(distX).value()); } } } // private static void out(boolean[] array) { // System.out.println(Arrays.toString(array)); // } // // private static void out(boolean[][] array) { // int count = 0; // for (boolean[] a : array) { // System.out.print(count++ + ":"); // out(a); // } // } private static class Rational { static final Rational one = new Rational(1, 1); static final Rational zero = new Rational(0, 1); public int z; public int n; // create and initialize a new Rational object public Rational(int z, int n) { if (n == 0) { throw new RuntimeException("Denominator is zero"); } int g = gcd(z, n); this.z = z / g; this.n = n / g; } // return string representation of (this) public String toString() { if (n == 1) { return z + ""; } else { return z + "/" + n; } } // return (this * b) public Rational times(Rational b) { return new Rational(this.z * b.z, this.n * b.n); } // return (this + b) public Rational plus(Rational b) { int z = (this.z * b.n) + (this.n * b.z); int n = this.n * b.n; return new Rational(z, n); } // return (this - b) public Rational minus(Rational b) { int z = (this.z * b.n) - (this.n * b.z); int n = this.n * b.n; return new Rational(z, n); } // return fractal amount public Rational fractal() { return new Rational(z % n, n); } // return (1 / this) public Rational reciprocal() { return new Rational(n, z); } // return (this / b) public Rational divides(Rational b) { return this.times(b.reciprocal()); } public boolean positive() { return z * n >= 0; } public boolean equals(Rational r) { return r.z == this.z && r.n == this.n; } public double value() { return 1.0 * z / n; } private int gcd(int m, int n) { if (0 == n) return m; else return gcd(n, m % n); } } }
0
3,577
C20004
C20067
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
import java.io.*; import java.util.*; public class Solution { private StringTokenizer st; private BufferedReader in; private PrintWriter out; final String file = "D-large"; public void solve() throws IOException { int tests = nextInt(); for (int test = 0; test < tests; ++test) { int n = nextInt(); int m = nextInt(); int d = nextInt(); char[][] f = new char[n][m]; int x0 = -1, y0 = -1; for (int i = 0; i < n; ++i) { f[i] = next().toCharArray(); for (int j = 0; j < m; ++j) { if (f[i][j] == 'X') { x0 = i; y0 = j; } } } int ans = 0; for (int dx = -d; dx <= d; ++dx) { for (int dy = -d; dy <= d; ++dy) { if ((dx != 0 || dy != 0) && dx * dx + dy * dy <= d * d && raytrace(x0, y0, x0 + dx, y0 + dy, f, 0.)) { // System.err.println(dx + " " + dy); ans++; } } } System.err.printf("Case #%d: %d%n", test + 1, ans); out.printf("Case #%d: %d%n", test + 1, ans); } } final double EPS = 1e-8; private boolean raytrace(int x0, int y0, int x1, int y1, char[][] f, double t) { double firstt = Double.POSITIVE_INFINITY; int dx = x1 - x0; int dy = y1 - y0; double tx = Double.POSITIVE_INFINITY; for (int i = Math.max(0, Math.min(x0, x1)); i < f.length && i <= Math.max(x0, x1); ++i) { for (int j = Math.max(0, Math.min(y0, y1)); j < f[0].length && j <= Math.max(y0, y1); ++j) { if (f[i][j] == 'X') { double tt = dx != 0 ? (double)(i - x0) / dx : (double)(j - y0) / dy; if (Math.abs(x0 + dx * tt - i) < EPS && Math.abs(y0 + dy * tt - j) < EPS) { tx = tt; } } if (f[i][j] != '#') { continue; } double minx = dx == 0 ? Double.NEGATIVE_INFINITY : Math.min((i - 0.5 - x0) / dx, (i + 0.5 - x0) / dx); double maxx = dx == 0 ? Double.POSITIVE_INFINITY : Math.max((i - 0.5 - x0) / dx, (i + 0.5 - x0) / dx); double miny = dy == 0 ? Double.NEGATIVE_INFINITY : Math.min((j - 0.5 - y0) / dy, (j + 0.5 - y0) / dy); double maxy = dy == 0 ? Double.POSITIVE_INFINITY : Math.max((j - 0.5 - y0) / dy, (j + 0.5 - y0) / dy); if (maxx < miny - EPS || maxy < minx - EPS) { continue; } double tt = Math.max(minx, miny); if (tt > t + EPS) { firstt = Math.min(firstt, tt); } } } if (firstt > 1.) { return x1 >= 0 && x1 < f.length && y1 >= 0 && y1 < f[0].length && f[x1][y1] == 'X'; } if (tx > t + EPS && tx < firstt) { return false; } int sx = Integer.signum(dx); int sy = Integer.signum(dy); double x = x0 + dx * firstt - 0.5; double y = y0 + dy * firstt - 0.5; int rx = (int)Math.round(x); int ry = (int)Math.round(y); if (Math.abs(rx - x) < EPS && Math.abs(ry - y) < EPS) { if (dx == 0 || dy == 0) { throw new AssertionError(); } boolean f11 = get(f, rx + (1 + sx) / 2, ry + (1 + sy) / 2); boolean f01 = get(f, rx + (1 - sx) / 2, ry + (1 + sy) / 2); boolean f10 = get(f, rx + (1 + sx) / 2, ry + (1 - sy) / 2); if (get(f, rx + (1 - sx) / 2, ry + (1 - sy) / 2) || !f11 && !f01 && !f10) { throw new AssertionError(); } if (!f11) { return raytrace(x0, y0, x1, y1, f, firstt); } if (f01 && f10 && f11) { return raytrace(2 * rx + 1 - x0, 2 * ry + 1 - y0, 2 * rx + 1 - x1, 2 * ry + 1 - y1, f, firstt); } if (f10 && f11) { return raytrace(2 * rx + 1 - x0, y0, 2 * rx + 1 - x1, y1, f, firstt); } if (f01 && f11) { return raytrace(x0, 2 * ry + 1 - y0, x1, 2 * ry + 1 - y1, f, firstt); } return false; } if (Math.abs(rx - x) < EPS) { return raytrace(2 * rx + 1 - x0, y0, 2 * rx + 1 - x1, y1, f, firstt); } if (Math.abs(ry - y) < EPS) { return raytrace(x0, 2 * ry + 1 - y0, x1, 2 * ry + 1 - y1, f, firstt); } throw new AssertionError(); } private boolean get(char[][] f, int i, int j) { return i >= 0 && i < f.length && j >= 0 && j < f[0].length && f[i][j] == '#'; } public void run() throws IOException { in = new BufferedReader(new FileReader(file + ".in")); out = new PrintWriter(file + ".out"); eat(""); solve(); out.close(); } void eat(String s) { st = new StringTokenizer(s); } String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } eat(line); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); new Solution().run(); } }
0
3,578
C20004
C20008
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; public class SimpleMirrors { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader(args[0])); PrintWriter out = new PrintWriter(args[1]); // number of testcases String sCount = in.readLine(); int count = Integer.parseInt(sCount); for(int idx=1; idx<=count; idx++) { String[] parts = in.readLine().split(" "); int h = Integer.parseInt(parts[0]); int w = Integer.parseInt(parts[1]); int d = Integer.parseInt(parts[2]); int x = -1, y = -1; // small dataset => just find the "X" for(int i=0; i<h; i++) { String l = in.readLine(); int p = l.indexOf('X'); if(p > -1) { y = 10*i - 5; x = 10*p - 5; } } out.println("Case #" + idx + ": " + process(10*(h-2), 10*(w-2), 10*d, x, y)); } out.close(); } private static int process(int h, int w, int d, int ox, int oy) { System.out.println("h=" + h + ", w=" + w + ", d=" + d + ", ox=" + ox + ", oy=" + oy); MirrorSet ms = new MirrorSet( Arrays.asList(new Mirror[] { new Mirror(Facing.POS, Orient.HORIZ, 0, 0, w), new Mirror(Facing.NEG, Orient.HORIZ, h, 0, w), new Mirror(Facing.POS, Orient.VERT, 0, 0, h), new Mirror(Facing.NEG, Orient.VERT, w, 0, h), }), ox, oy, d, w, h ); Set<Point> pts = ms.transitiveImages(); Set<Double> angles = ms.angles(pts); System.out.println(" " + pts.size() + ": " + pts); System.out.println(" " + angles.size() + ": " + angles); return angles.size(); } private static class MirrorSet { private final Collection<Mirror> mirrors; private final int ox, oy, limit, w, h; public MirrorSet(Collection<Mirror> mirrors, int ox, int oy, int limit, int w, int h) { this.mirrors = mirrors; this.ox = ox; this.oy = oy; this.limit = limit; this.w = w; this.h = h; } public Set<Point> images(Set<Point> in) { HashSet<Point> out = new HashSet<Point>(); for(Point i : in) { for(Mirror m : mirrors) { Point o = m.image(i); if(o != null && ! in.contains(o) && Math.sqrt((ox-o.x)*(ox-o.x) + (oy-o.y)*(oy-o.y)) <= limit) out.add(o); } } return out; } public Set<Point> transitiveImages() { Set<Point> im = new HashSet<Point>(); im.add(new Point(ox, oy)); Set<Point> newer; do { newer = images(im); im.addAll(newer); } while(newer.size() > 0); return im; } public Set<Double> angles(Set<Point> in) { Set<Double> out = new HashSet<Double>(); for(Point i : in) { if(i.x != ox || i.y != oy) out.add(Math.atan2(i.y-oy, i.x-ox)); } return out; } } private static enum Facing { POS, NEG } private static enum Orient { VERT, HORIZ } private static class Mirror { private final Facing facing; private final Orient orient; private final int pos, start, stop; public Mirror(Facing facing, Orient orient, int pos, int start, int stop) { this.facing = facing; this.orient = orient; this.pos = pos; this.start = start; this.stop = stop; } public Point image(Point p) { switch(orient) { case VERT: if(facing == Facing.POS && p.x >= pos || facing == Facing.NEG && p.x <= pos) { return new Point(2 * pos - p.x, p.y); } else return null; case HORIZ: if(facing == Facing.POS && p.y >= pos || facing == Facing.NEG && p.y <= pos) { return new Point(p.x, 2 * pos - p.y); } else return null; } return null; } } private static class Point { public final int x, y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { return obj instanceof Point && ((Point)obj).x == x && ((Point)obj).y == y; } @Override public int hashCode() { return (x << 16) ^ y; } @Override public String toString() { return "(" + x + ", " + y + ")"; } } }
0
3,579
C20004
C20063
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
0
3,580
C20004
C20015
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
0
3,581
C20004
C20050
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,582
C20004
C20085
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
0
3,583
C20004
C20020
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
0
3,584
C20004
C20062
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,585
C20004
C20059
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
0
3,586
C20004
C20064
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
0
3,587
C20004
C20076
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
0
3,588
C20004
C20026
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
import java.io.*; import java.util.*; import java.math.*; class D { private static final boolean DEBUG_ON = true; private static final boolean ECHO_ON = true; private static BufferedReader input; private static BufferedWriter output; private static final int INF = Integer.MAX_VALUE / 2; private static final int MOD = 10007; private static int H, W, D, row, col; public static int gcd(int n, int m) {return (0 == m) ? (n) : gcd(m, n % m);} public static int sqrt(int X) { int answer = 1, interval = 1; for (int i = String.valueOf(X).length() / 2; i > 0; i--) {interval *= 10;} while (interval >= 1) { while ((answer * answer) <= X) {answer += interval;} answer -= interval; interval /= 10; } return answer; } public static void main(String[] args) { try { input = new BufferedReader(new FileReader(args[0] + ".in")); output = new BufferedWriter(new FileWriter(args[0] + ".out")); String line = input.readLine(); int testcases = getInt(line, 0); for (int testcase = 1; testcase <= testcases; testcase++) { char[][] real = getCharMatrix(input); HashSet<Integer> valid = new HashSet<Integer>(); for (int i = row - D; i <= row + D; i++) { int range = sqrt((D * D) - ((i - row) * (i - row))); for (int j = col - range; j <= col + range; j++) { int diffX = i - row; int diffY = j - col; if (0 == diffX && 0 == diffY) {continue;} int gcd = gcd(Math.abs(diffX), Math.abs(diffY)); int direction = (((diffX/gcd) + D) << 16) + ((diffY/gcd) + D); if (valid.contains(direction)) {continue;} int x = 100 * row + 50; int y = 100 * col + 50; for (int k = 0; k < 100; k++) { int nextX = x + diffX; int nextY = y + diffY; int xCell = x / 100; int yCell = y / 100; int nextXCell = nextX / 100; if (0 == nextX % 100) {nextXCell = (nextX + diffX) / 100;} int nextYCell = nextY / 100; if (0 == nextY % 100) {nextYCell = (nextY + diffY) / 100;} if (xCell == nextXCell && yCell == nextYCell) {x = nextX; y = nextY;} else if (xCell != nextXCell && yCell == nextYCell) { if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;} else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if (xCell == nextXCell && yCell != nextYCell) { if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;} else { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else { int cornerX = -1, cornerY = -1; if (nextXCell < xCell && nextYCell < yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * (nextYCell + 1);} else if (nextXCell > xCell && nextYCell < yCell) {cornerX = 100 * nextXCell; cornerY = 100 * (nextYCell + 1);} else if (nextXCell < xCell && nextYCell > yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * nextYCell;} else if (nextXCell > xCell && nextYCell > yCell) {cornerX = 100 * nextXCell; cornerY = 100 * nextYCell;} if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { diffX = -diffX; diffY = -diffY; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {x = nextX; y = nextY;} // passing corner else { diffX = -diffX; diffY = -diffY; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {break;} // hitting corner else if (Math.abs((cornerX - x) * (nextY - y)) < Math.abs((nextX - x) * (cornerY - y))) // hitting Y { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) { if (Math.abs((cornerX - x) * (nextY - y)) <= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;} else { diffY = -diffY; if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));} else {y = (100 * nextYCell) + ((100 * nextYCell) - y);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell]) { if (Math.abs((cornerX - x) * (nextY - y)) >= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;} else { diffX = -diffX; if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));} else {x = (100 * nextXCell) + ((100 * nextXCell) - x);} x = x + diffX; y = y + diffY; } } else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) {x = nextX; y = nextY;} } if ((100 * row + 50) == x && (100 * col + 50) == y) {valid.add(direction); break;} } } } String result = "Case #" + testcase + ": " + valid.size(); output(result); } input.close(); output.close(); } catch (Exception e) { e.printStackTrace(); } } public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));} public static long getLong(String line, int index) {return Long.parseLong(getString(line, index));} public static double getDouble(String line, int index) {return Double.parseDouble(getString(line, index));} public static String getString(String line, int index) { line = line.trim(); while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;} if ((-1) == line.indexOf(' ')) {return line;} else {return line.substring(0, line.indexOf(' '));} } public static int[] getIntArray(String line) { String[] strings = getStringArray(line); int[] numbers = new int[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Integer.parseInt(strings[i]);} return numbers; } public static long[] getLongArray(String line) { String[] strings = getStringArray(line); long[] numbers = new long[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Long.parseLong(strings[i]);} return numbers; } public static double[] getDoubleArray(String line) { String[] strings = getStringArray(line); double[] numbers = new double[strings.length]; for (int i = 0; i < strings.length; i++) {numbers[i] = Double.parseDouble(strings[i]);} return numbers; } public static String[] getStringArray(String line) {return line.trim().split("(\\s)+", 0);} public static int[] getIntArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); int[] numbers = new int[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Integer.parseInt(strings[i - begin]);} return numbers; } public static long[] getLongArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); long[] numbers = new long[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Long.parseLong(strings[i - begin]);} return numbers; } public static double[] getDoubleArray(String line, int begin, int end) { String[] strings = getStringArray(line, begin, end); double[] numbers = new double[end - begin]; for (int i = begin; i < end; i++) {numbers[i - begin] = Double.parseDouble(strings[i - begin]);} return numbers; } public static String[] getStringArray(String line, int begin, int end) { String[] lines = line.trim().split("(\\s)+", 0); String[] results = new String[end - begin]; for (int i = begin; i < end; i++) {results[i - begin] = lines[i];} return results; } public static char[][] getCharMatrix(BufferedReader input) throws Exception { String line = input.readLine(); H = getInt(line, 0); W = getInt(line, 1); D = getInt(line, 2); char[][] matrix = new char[H][W]; for (int i = 0; i < H; i++) { line = input.readLine(); for (int j = 0; j < W; j++) { char c = matrix[i][j] = line.charAt(j); if ('X' == c) {row = i; col = j;} } } return matrix; } public static int[][] getIntMatrix(BufferedReader input) throws Exception { String line = input.readLine(); int R = getInt(line, 0); int C = getInt(line, 1); int[][] matrix = new int[R][C]; for (int i = 0; i < R; i++) { line = input.readLine(); for (int j = 0; j < C; j++) {matrix[i][j] = getInt(line, j);} } return matrix; } public static boolean[][] newBooleanMatrix(int R, int C, boolean value) { boolean[][] matrix = new boolean[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static char[][] newCharMatrix(int R, int C, char value) { char[][] matrix = new char[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static int[][] newIntMatrix(int R, int C, int value) { int[][] matrix = new int[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static long[][] newLongMatrix(int R, int C, long value) { long[][] matrix = new long[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static double[][] newDoubleMatrix(int R, int C, double value) { double[][] matrix = new double[R][C]; for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;} return matrix; } public static void output(String s) throws Exception { if (ECHO_ON) {System.out.println(s);} output.write(s); output.newLine(); } public static String toKey(boolean[] array) { StringBuffer buffer = new StringBuffer(array.length + ","); for (int i = 0; i < array.length / 16; i++) { char c = 0; for (int j = 0; j < 16; j++) { c <<= 1; if (array[i * 16 + j]) {c += 1;} } buffer.append(c + ""); } char c = 0; for (int j = 0; j < (array.length % 16); j++) { c <<= 1; if (array[(array.length / 16) * 16 + j]) {c += 1;} } buffer.append(c + ""); return buffer.toString(); } public static String toKey(int[] array, int bit) { StringBuffer buffer = new StringBuffer(array.length + ","); if (bit > 16) { for (int i = 0; i < array.length; i++) { char c1 = (char)(array[i] >> 16); char c2 = (char)(array[i] & 0xFFFF); buffer.append("" + c1 + c2); } } else { int n = 16 / bit; for (int i = 0; i < array.length / n; i++) { char c = 0; for (int j = 0; j < n; j++) { c <<= bit; c += array[i * n + j]; } buffer.append(c + ""); } char c = 0; for (int j = 0; j < (array.length % n); j++) { c <<= bit; c += array[(array.length / n) * n + j]; } buffer.append(c + ""); } return buffer.toString(); } public static void debug(String s) {if (DEBUG_ON) {System.out.println(s);}} public static void debug(String s0, double l0) {if (DEBUG_ON) {System.out.println(s0+" = "+l0);}} public static void debug(String s0, double l0, String s1, double l1) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2) {if (DEBUG_ON) { System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3);}} public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3, String s4, double l4) {if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3+"; "+s4+" = "+l4);}} public static void debug(boolean[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(boolean[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append((array[i] == true ? "1" : "0") + separator);} buffer.append((array[array.length - 1] == true) ? "1" : "0"); System.out.println(buffer.toString()); } } public static void debug(boolean[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(boolean[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} public static void debug(char[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(char[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);} buffer.append(array[array.length - 1]); System.out.println(buffer.toString()); } } public static void debug(char[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(char[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} public static void debug(int[] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(int[] array, String separator) { if (DEBUG_ON) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);} buffer.append(array[array.length - 1]); System.out.println(buffer.toString()); } } public static void debug(int[][] array) {if (DEBUG_ON) {debug(array, " ");}} public static void debug(int[][] array, String separator) {if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}} }
0
3,589
C20004
C20078
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
import java.awt.Point; public class FracVec { public final Frac row, col; public FracVec(Frac row, Frac col) { this.row = row; this.col = col; } public FracVec(Point pos) { row = new Frac(pos.y); col = new Frac(pos.x); } public FracVec flipHoriz() { return new FracVec(row, col.neg()); } public FracVec flipVert() { return new FracVec(row.neg(), col); } public boolean isCorner() { return row.denom == 1 && col.denom == 1; } public FracVec add(FracVec other) { return new FracVec(row.add(other.row), col.add(other.col)); } public FracVec sub(FracVec other) { return new FracVec(row.sub(other.row), col.sub(other.col)); } public FracVec neg() { return new FracVec(row.neg(), col.neg()); } public boolean equals(FracVec other) { return row.equals(other.row) && col.equals(other.col); } public boolean equals(Point p) { return row.equals(p.y) && col.equals(p.x); } @Override public boolean equals(Object other) { return other instanceof FracVec && equals((FracVec) other); } @Override public int hashCode() { return (31 + row.hashCode()) * 31 + col.hashCode(); } public String toString() { return "( " + row + ", " + col + " )"; } public FracVec multiply(Frac scalar) { return new FracVec(row.mult(scalar), col.mult(scalar)); } public int compareLengthTo(int d) { return row.mult(row).add(col.mult(col)).compareTo(new Frac(d * d)); } public Point getNextCell(FracVec dm) { int resRow, resCol; if (dm.row.sgn() < 0) { resRow = row.roundDownAddSmallDown(); } else { resRow = row.roundDownAddSmallUp(); } if (dm.col.sgn() < 0) resCol = col.roundDownAddSmallDown(); else resCol = col.roundDownAddSmallUp(); return new Point(resCol, resRow); } }
0
3,590
C20004
C20057
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package jp.funnything.competition.util; import java.util.Arrays; import java.util.List; import com.google.common.collect.Lists; import com.google.common.primitives.Ints; import com.google.common.primitives.Longs; public class Prime { public static class PrimeData { public int[] list; public boolean[] map; private PrimeData( final int[] values , final boolean[] map ) { list = values; this.map = map; } } public static long[] factorize( long n , final int[] primes ) { final List< Long > factor = Lists.newArrayList(); for ( final int p : primes ) { if ( n < p * p ) { break; } while ( n % p == 0 ) { factor.add( ( long ) p ); n /= p; } } if ( n > 1 ) { factor.add( n ); } return Longs.toArray( factor ); } public static PrimeData prepare( final int n ) { final List< Integer > primes = Lists.newArrayList(); final boolean[] map = new boolean[ n ]; Arrays.fill( map , true ); map[ 0 ] = map[ 1 ] = false; primes.add( 2 ); for ( int composite = 2 * 2 ; composite < n ; composite += 2 ) { map[ composite ] = false; } for ( int value = 3 ; value < n ; value += 2 ) { if ( map[ value ] ) { primes.add( value ); for ( int composite = value * 2 ; composite < n ; composite += value ) { map[ composite ] = false; } } } return new PrimeData( Ints.toArray( primes ) , map ); } }
0
3,591
C20004
C20030
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uk.co.epii.codejam.hallofmirrors; /** * * @author jim */ public class RayVector { public final int x; public final int y; public RayVector(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { if (obj instanceof RayVector) { RayVector v = (RayVector)obj; if (v.x * y == v.y * x) return true; } return false; } @Override public int hashCode() { int hash = 7; hash = 41 * hash + this.x; hash = 41 * hash + this.y; return hash; } public Fraction getScalarGardient() { return new Fraction(Math.abs(y), Math.abs(x)); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("("); sb.append(x); sb.append(","); sb.append(y); sb.append(")"); return sb.toString(); } }
0
3,592
C20004
C20002
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
import java.util.*; import java.io.*; import java.math.*; import java.awt.*; import static java.lang.Math.*; import static java.lang.Integer.parseInt; import static java.lang.Double.parseDouble; import static java.lang.Long.parseLong; import static java.lang.System.*; import static java.util.Arrays.*; import static java.util.Collection.*; public class D { static int gcd(int a, int b) { return b == 0 ? a : a == 0 ? b : gcd(b, a%b); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(in)); int T = parseInt(br.readLine()); for(int t = 0; t++ < T; ) { String[] line = br.readLine().split(" "); int H = parseInt(line[0]), W = parseInt(line[1]), D = parseInt(line[2]); char[][] G = new char[H][]; for(int h = 0; h < H; h++) G[h] = br.readLine().toCharArray(); int X = 0, Y = 0; outer:for(Y = 0; Y < H; Y++) for(X = 0; X < W; X++) if(G[Y][X] == 'X') break outer; int count = 0; for(int i = -D; i <= D; i++) { for(int j = -D; j <= D; j++) { int dx = i, dy = j, scale = 2 * Math.abs((dx == 0 ? 1 : dx) * (dy == 0 ? 1 : dy)), x0, y0, x, y; int steps = (int)Math.floor(scale * D / Math.sqrt(dx * dx + dy * dy)); if(gcd(Math.abs(dx), Math.abs(dy)) != 1) continue; x0 = x = X * scale + scale / 2; y0 = y = Y * scale + scale / 2; do { steps -= 1; if(x % scale == 0 && y % scale == 0) { // at a corner int dxi = dx > 0 ? 1 : -1, dyi = dy > 0 ? 1 : -1; int xi = (x / scale) - (dxi + 1) / 2, yi = (y / scale) - (dyi + 1) / 2; if(G[yi+dyi][xi+dxi] == '#') { if(G[yi+dyi][xi] != '#' && G[yi][xi+dxi] != '#') steps = -1; // kill the light if(G[yi+dyi][xi] == '#') dy *= -1; if(G[yi][xi+dxi] == '#') dx *= -1; } else ; // otherwise step as normal } else if(x % scale == 0) { int xi = x / scale, yi = y / scale; if(G[yi][xi] == '#' || G[yi][xi-1] == '#') dx *= -1; } else if(y % scale == 0) { int xi = x / scale, yi = y / scale; if(G[yi][xi] == '#' || G[yi-1][xi] == '#') dy *= -1; } else ; // smooth sailing x += dx; y += dy; } while(steps >= 0 && !(x == x0 && y == y0)); if(steps >= 0) ++count; } } out.println("Case #" + t +": " + count) ; } } }
0
3,593
C20004
C20056
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package jp.funnything.competition.util; import java.math.BigDecimal; /** * Utility for BigDeciaml */ public class BD { public static BigDecimal ZERO = BigDecimal.ZERO; public static BigDecimal ONE = BigDecimal.ONE; public static BigDecimal add( final BigDecimal x , final BigDecimal y ) { return x.add( y ); } public static BigDecimal add( final BigDecimal x , final double y ) { return add( x , v( y ) ); } public static BigDecimal add( final double x , final BigDecimal y ) { return add( v( x ) , y ); } public static int cmp( final BigDecimal x , final BigDecimal y ) { return x.compareTo( y ); } public static int cmp( final BigDecimal x , final double y ) { return cmp( x , v( y ) ); } public static int cmp( final double x , final BigDecimal y ) { return cmp( v( x ) , y ); } public static BigDecimal div( final BigDecimal x , final BigDecimal y ) { return x.divide( y ); } public static BigDecimal div( final BigDecimal x , final double y ) { return div( x , v( y ) ); } public static BigDecimal div( final double x , final BigDecimal y ) { return div( v( x ) , y ); } public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) { return x.multiply( y ); } public static BigDecimal mul( final BigDecimal x , final double y ) { return mul( x , v( y ) ); } public static BigDecimal mul( final double x , final BigDecimal y ) { return mul( v( x ) , y ); } public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) { return x.subtract( y ); } public static BigDecimal sub( final BigDecimal x , final double y ) { return sub( x , v( y ) ); } public static BigDecimal sub( final double x , final BigDecimal y ) { return sub( v( x ) , y ); } public static BigDecimal v( final double value ) { return BigDecimal.valueOf( value ); } }
0
3,594
C20004
C20018
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package template; import java.util.Objects; public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{ private T1 o1; private T2 o2; public Pair(T1 o1, T2 o2) { this.o1 = o1; this.o2 = o2; } @Override public boolean equals(Object other) { if (!(other instanceof Pair)) {return false;} return (compareTo((Pair<T1, T2>)other) == 0); } @Override public int compareTo(Pair<T1, T2> other) { int c1 = ((Comparable<T1>) o1).compareTo(other.getO1()); if (c1 != 0) {return c1;} int c2 = ((Comparable<T2>) o2).compareTo(other.getO2()); return c2; } @Override public int hashCode() { int hash = 5; hash = 83 * hash + Objects.hashCode(this.o1); hash = 83 * hash + Objects.hashCode(this.o2); return hash; } @Override public String toString() { return "[" + o1 + ", " + o2 + "]"; } public T1 getO1() { return o1; } public void setO1(T1 o1) { this.o1 = o1; } public T2 getO2() { return o2; } public void setO2(T2 o2) { this.o2 = o2; } }
0
3,595
C20004
C20051
import java.util.*; public class D { public static boolean wall[][]; public static int X; public static int Y; public static int[] dxs; public static int[] dys; public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int c = 1; c <= T; c++){ int H = in.nextInt(); int W = in.nextInt(); int D = in.nextInt(); dxs = new int[4]; dys = new int[4]; dxs[0] = dxs[1] = 1; dxs[2] = dxs[3] = -1; dys[0] = dys[2] = 1; dys[1] = dys[3] = -1; wall = new boolean[H][W]; int mex, mey; mex = 0; mey = 0; in.nextLine(); for(int i = 0; i < H; i++){ String line = in.nextLine(); for(int j = 0; j < W; j++){ switch( line.charAt(j)){ case '#': wall[i][j] = true; break; case 'X': mex = j; mey = i; case '.': wall[i][j] = false; break; } } }//done reading input. int count = 0; //check NESW int nd = 1, ed = 1, sd = 1, wd = 1; while(!wall[mey][mex+ed]) ed++; while(!wall[mey][mex-wd]) wd++; while(!wall[mey+nd][mex]) nd++; while(!wall[mey-sd][mex]) sd++; if(2*ed-1 <= D) count++; if(2*wd-1 <= D) count++; if(2*sd-1 <= D) count++; if(2*nd-1 <= D) count++; for(int x = 1; x <= D; x++) for(int y = 1; y <= D; y++){ if(x*x + y*y > D*D) continue; for(int d = 0; d < 4; d++){ int posx = mex; int posy = mey; int dx = dxs[d]; int dy = dys[d]; boolean fail = false; int distx = 0; int disty = 0; for(int s = 1; s <= 2*x*y && !fail; s++){ distx += dx; disty += dy; //System.out.println("distx = " + distx +"; disty = " + disty); if(s < 2*x*y && distx == 0 && disty == 0) fail = true; if((s % x == 0) && ( (s/x) % 2 == 1) && (s % y == 0) && ( (s/y) % 2 == 1)){ if(!wall[posy + dy][posx + dx]){ posy += dy; posx += dx; }else if(wall[posy][posx + dx] && wall[posy + dy][posx]){ dx *= -1; dy *= -1; } else if(wall[posy][posx + dx]){ dx *= -1; posy += dy; } else if(wall[posy + dy][posx]){ posx += dx; dy *= -1; } else{ fail = true; } } else if((s % x == 0) && ( (s/x) % 2 == 1)){ if(wall[posy][posx + dx]) dx *= -1; else posx += dx; } else if((s % y == 0) && ( (s/y) % 2 == 1)){ if(wall[posy + dy][posx]) dy *= -1; else posy += dy; } } if(!fail && posx == mex && posy == mey){ //System.out.println("x = " + x +"; y = " + y); count++; } } } System.out.println("Case #" + c +": " + count); } } }
package jp.funnything.competition.util; import java.util.List; import com.google.common.collect.Lists; public class Lists2 { public static < T > List< T > newArrayListAsArray( final int length ) { final List< T > list = Lists.newArrayListWithCapacity( length ); for ( int index = 0 ; index < length ; index++ ) { list.add( null ); } return list; } }
0
3,596
C20005
C20065
package gcj; import com.sun.org.apache.xerces.internal.impl.xs.opti.DefaultXMLDocumentHandler; import java.util.*; import java.io.*; public class HallOfMirrors { final static String PROBLEM_NAME = "mirrors"; final static String WORK_DIR = "D:\\Gcj\\" + PROBLEM_NAME + "\\"; int H, W, D; double stX, stY; int res = 0; String[] map; int gcd(int a, int b) { while (a>0 && b>0) if (a>b) a %= b; else b %= a; return a + b; } final double EPS = 1e-10; double getT(double cur, int V) { cur *= 2; V *= 2; if (V == 0) return 1e100; if (V > 0) { double want = Math.ceil(cur + EPS); return (want - cur) / V; } else { double want = Math.floor(cur - EPS); return (want - cur) / V; } } boolean isInteger(double x) { return Math.abs(x - Math.floor(x)) <= EPS || Math.abs(x - Math.ceil(x)) <= EPS; } boolean inMirror(double x, double y) { int xx = (int)Math.floor(x); int yy = (int)Math.floor(y); return map[xx].charAt(yy) == '#'; } void process(int dx, int dy) { double curX = stX, curY = stY; double dist = 0.0; while (true) { double t1 = getT(curX, dx); double t2 = getT(curY, dy); double t = Math.min(t1, t2); double nextX = curX + t * dx; double nextY = curY + t * dy; double piece = Math.sqrt((nextX - curX) * (nextX - curX) + (nextY - curY) * (nextY - curY)); dist += piece; if (dist > D + EPS) return; if (Math.abs(nextX - stX) <= EPS && Math.abs(nextY - stY) <= EPS) { res++; return; } curX = nextX; curY = nextY; boolean fx = isInteger(nextX); boolean fy = isInteger(nextY); if (fx && fy) { // corner // A B // C D boolean A = inMirror(nextX - EPS, nextY - EPS); boolean B = inMirror(nextX - EPS, nextY + EPS); boolean C = inMirror(nextX + EPS, nextY - EPS); boolean D = inMirror(nextX + EPS, nextY + EPS); int cnt = (A ? 1 : 0) + (B ? 1 : 0) + (C ? 1 : 0) + (D ? 1 : 0); if (cnt == 3) { dx = -dx; dy = -dy; } else if ((A && B && !C && !D) || (!A && !B && C & D)) { dx = -dx; } else if ((A && C && !B && !D) || (!A && !C && B && D)) { dy = -dy; } else if (inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY + (dy > 0 ? EPS : -EPS))) { return; } else { // just continue; } } else if (fx && !fy) { // horizontal if (dx != 0 && inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY)) dx = -dx; } else if (!fx && fy) { // vertical if (dy != 0 && inMirror(nextX, nextY + (dy > 0 ? EPS : -EPS))) dy = -dy; } else { // nothing } } } void solve(Scanner sc, PrintWriter pw) { H = sc.nextInt(); W = sc.nextInt(); D = sc.nextInt(); map = new String[H]; for (int i=0; i<H; i++) map[i] = sc.next(); for (int i=0; i<H; i++) for (int j=0; j<W; j++) if (map[i].charAt(j) == 'X') { stX = i + 0.5; stY = j + 0.5; } for (int dx=-D; dx<=D; dx++) for (int dy=-D; dy<=D; dy++) if (dx*dx + dy*dy > 0 && dx*dx + dy*dy <= D * D && gcd(Math.abs(dx), Math.abs(dy)) == 1) { process(dx, dy); } pw.println(res); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new FileReader(WORK_DIR + "input.txt")); PrintWriter pw = new PrintWriter(new FileWriter(WORK_DIR + "output.txt")); int caseCnt = sc.nextInt(); for (int caseNum=0; caseNum<caseCnt; caseNum++) { System.out.println("Processing test case " + (caseNum + 1)); pw.print("Case #" + (caseNum+1) + ": "); new HallOfMirrors().solve(sc, pw); } pw.flush(); pw.close(); sc.close(); } }
package jp.funnything.prototype; import static java.lang.Math.abs; import java.io.File; import java.io.IOException; import jp.funnything.competition.util.CompetitionIO; import jp.funnything.competition.util.Packer; import org.apache.commons.io.FileUtils; import org.apache.commons.math.fraction.Fraction; import org.apache.commons.math.util.MathUtils; public class Runner { public static void main( final String[] args ) throws Exception { new Runner().run(); } boolean isValid( final int d , final boolean[][] map , final int ox , final int oy , int dx , int dy ) { final Fraction fox = new Fraction( ox * 2 + 1 ); final Fraction foy = new Fraction( oy * 2 + 1 ); Fraction x = new Fraction( ox * 2 + 1 ); Fraction y = new Fraction( oy * 2 + 1 ); Fraction sumDiffX = new Fraction( 0 ); Fraction sumDiffY = new Fraction( 0 ); for ( ; ; ) { final Fraction diffX = new Fraction( dx > 0 ? ( int ) Math.floor( x.doubleValue() + 1 ) : ( int ) Math.ceil( x.doubleValue() - 1 ) ).subtract( x ); final Fraction diffY = new Fraction( dy > 0 ? ( int ) Math.floor( y.doubleValue() + 1 ) : ( int ) Math.ceil( y.doubleValue() - 1 ) ).subtract( y ); if ( abs( diffX.doubleValue() * dy ) < abs( diffY.doubleValue() * dx ) ) { x = x.add( diffX ); y = y.add( diffX.multiply( dy ).divide( dx ) ); sumDiffX = sumDiffX.add( diffX.abs() ); sumDiffY = sumDiffY.add( diffX.multiply( dy ).divide( dx ).abs() ); } else { y = y.add( diffY ); x = x.add( diffY.multiply( dx ).divide( dy ) ); sumDiffY = sumDiffY.add( diffY.abs() ); sumDiffX = sumDiffX.add( diffY.multiply( dx ).divide( dy ).abs() ); } if ( sumDiffX.multiply( sumDiffX ).add( sumDiffY.multiply( sumDiffY ) ).compareTo( new Fraction( d * d * 2 * 2 ) ) > 0 ) { return false; } if ( x.equals( fox ) && y.equals( foy ) ) { return true; } final int nx = x.intValue() / 2 + ( dx > 0 ? 0 : -1 ); final int ny = y.intValue() / 2 + ( dy > 0 ? 0 : -1 ); if ( x.getDenominator() == 1 && x.getNumerator() % 2 == 0 && y.getDenominator() == 1 && y.getNumerator() % 2 == 0 ) { if ( map[ ny ][ nx ] ) { final int px = x.intValue() / 2 + ( dx > 0 ? -1 : 0 ); final int py = y.intValue() / 2 + ( dy > 0 ? -1 : 0 ); if ( map[ py ][ nx ] ) { if ( map[ ny ][ px ] ) { dx = -dx; dy = -dy; } else { dx = -dx; } } else { if ( map[ ny ][ px ] ) { dy = -dy; } else { return false; } } } } else { if ( x.getDenominator() == 1 && x.getNumerator() % 2 == 0 ) { if ( map[ y.intValue() / 2 ][ nx ] ) { dx = -dx; } } else if ( y.getDenominator() == 1 && y.getNumerator() % 2 == 0 ) { if ( map[ ny ][ x.intValue() / 2 ] ) { dy = -dy; } } } } } void pack() { try { final File dist = new File( "dist" ); if ( dist.exists() ) { FileUtils.deleteQuietly( dist ); } final File workspace = new File( dist , "workspace" ); FileUtils.copyDirectory( new File( "src/main/java" ) , workspace ); FileUtils.copyDirectory( new File( "../../../../CompetitionUtil/Lib/src/main/java" ) , workspace ); Packer.pack( workspace , new File( dist , "sources.zip" ) ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } void run() throws Exception { final CompetitionIO io = new CompetitionIO(); final int t = io.readInt(); for ( int index = 0 ; index < t ; index++ ) { final int[] values = io.readInts(); final int h = values[ 0 ]; final int w = values[ 1 ]; final int d = values[ 2 ]; final char[][] map = new char[ h ][]; for ( int y = 0 ; y < h ; y++ ) { final char[] l = io.read().toCharArray(); if ( l.length != w ) { throw new RuntimeException( "assert" ); } map[ y ] = l; } io.write( index + 1 , solve( d , map ) ); } io.close(); pack(); } int solve( final int d , final char[][] map ) { int count = 0; int ox = -1; int oy = -1; final boolean[][] parsed = new boolean[ map.length ][]; for ( int y = 0 ; y < map.length ; y++ ) { parsed[ y ] = new boolean[ map[ y ].length ]; for ( int x = 0 ; x < map[ y ].length ; x++ ) { final char c = map[ y ][ x ]; if ( c == '#' ) { parsed[ y ][ x ] = true; } if ( c == 'X' ) { ox = x; oy = y; } } } for ( int dy = -d ; dy <= d ; dy++ ) { for ( int dx = -d ; dx <= d ; dx++ ) { if ( dx == 0 && dy == 0 ) { continue; } if ( MathUtils.gcd( dx , dy ) != 1 ) { continue; } if ( dx * dx + dy * dy > d * d ) { continue; } if ( isValid( d , parsed , ox , oy , dx , dy ) ) { count++; } } } return count; } }
0
3,597
C20005
C20006
package gcj; import com.sun.org.apache.xerces.internal.impl.xs.opti.DefaultXMLDocumentHandler; import java.util.*; import java.io.*; public class HallOfMirrors { final static String PROBLEM_NAME = "mirrors"; final static String WORK_DIR = "D:\\Gcj\\" + PROBLEM_NAME + "\\"; int H, W, D; double stX, stY; int res = 0; String[] map; int gcd(int a, int b) { while (a>0 && b>0) if (a>b) a %= b; else b %= a; return a + b; } final double EPS = 1e-10; double getT(double cur, int V) { cur *= 2; V *= 2; if (V == 0) return 1e100; if (V > 0) { double want = Math.ceil(cur + EPS); return (want - cur) / V; } else { double want = Math.floor(cur - EPS); return (want - cur) / V; } } boolean isInteger(double x) { return Math.abs(x - Math.floor(x)) <= EPS || Math.abs(x - Math.ceil(x)) <= EPS; } boolean inMirror(double x, double y) { int xx = (int)Math.floor(x); int yy = (int)Math.floor(y); return map[xx].charAt(yy) == '#'; } void process(int dx, int dy) { double curX = stX, curY = stY; double dist = 0.0; while (true) { double t1 = getT(curX, dx); double t2 = getT(curY, dy); double t = Math.min(t1, t2); double nextX = curX + t * dx; double nextY = curY + t * dy; double piece = Math.sqrt((nextX - curX) * (nextX - curX) + (nextY - curY) * (nextY - curY)); dist += piece; if (dist > D + EPS) return; if (Math.abs(nextX - stX) <= EPS && Math.abs(nextY - stY) <= EPS) { res++; return; } curX = nextX; curY = nextY; boolean fx = isInteger(nextX); boolean fy = isInteger(nextY); if (fx && fy) { // corner // A B // C D boolean A = inMirror(nextX - EPS, nextY - EPS); boolean B = inMirror(nextX - EPS, nextY + EPS); boolean C = inMirror(nextX + EPS, nextY - EPS); boolean D = inMirror(nextX + EPS, nextY + EPS); int cnt = (A ? 1 : 0) + (B ? 1 : 0) + (C ? 1 : 0) + (D ? 1 : 0); if (cnt == 3) { dx = -dx; dy = -dy; } else if ((A && B && !C && !D) || (!A && !B && C & D)) { dx = -dx; } else if ((A && C && !B && !D) || (!A && !C && B && D)) { dy = -dy; } else if (inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY + (dy > 0 ? EPS : -EPS))) { return; } else { // just continue; } } else if (fx && !fy) { // horizontal if (dx != 0 && inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY)) dx = -dx; } else if (!fx && fy) { // vertical if (dy != 0 && inMirror(nextX, nextY + (dy > 0 ? EPS : -EPS))) dy = -dy; } else { // nothing } } } void solve(Scanner sc, PrintWriter pw) { H = sc.nextInt(); W = sc.nextInt(); D = sc.nextInt(); map = new String[H]; for (int i=0; i<H; i++) map[i] = sc.next(); for (int i=0; i<H; i++) for (int j=0; j<W; j++) if (map[i].charAt(j) == 'X') { stX = i + 0.5; stY = j + 0.5; } for (int dx=-D; dx<=D; dx++) for (int dy=-D; dy<=D; dy++) if (dx*dx + dy*dy > 0 && dx*dx + dy*dy <= D * D && gcd(Math.abs(dx), Math.abs(dy)) == 1) { process(dx, dy); } pw.println(res); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new FileReader(WORK_DIR + "input.txt")); PrintWriter pw = new PrintWriter(new FileWriter(WORK_DIR + "output.txt")); int caseCnt = sc.nextInt(); for (int caseNum=0; caseNum<caseCnt; caseNum++) { System.out.println("Processing test case " + (caseNum + 1)); pw.print("Case #" + (caseNum+1) + ": "); new HallOfMirrors().solve(sc, pw); } pw.flush(); pw.close(); sc.close(); } }
package problemD; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ProblemD { static Rational posX = null; static Rational posY = null; static Rational tarX = null; static Rational tarY = null; static boolean[][] array = null; public static void main(String[] args) throws FileNotFoundException { // Scanner sc = new Scanner(new File("D-practice.in")); // Scanner sc = new Scanner(new File("D-small.in")); Scanner sc = new Scanner(new File("D-large.in")); int cases = sc.nextInt(); for (int i = 1; i <= cases; i++) { // do case things here int H = sc.nextInt(); int W = sc.nextInt(); int D = sc.nextInt(); D *= 2; array = new boolean[H][W]; for (int j = 0; j < H; j++) { String s = sc.next(); for (int k = 0; k < W; k++) { array[j][k] = (s.charAt(k) == '#'); if (s.charAt(k) == 'X') { posX = new Rational(2 * k + 1, 1); posY = new Rational(2 * j + 1, 1); tarX = posX; tarY = posY; } } } int count = 0; boolean checked[][] = new boolean[2 * D + 1][2 * D + 1]; for (int j = 2; j <= D; j += 2) { for (int x = -j; x <= j; x += 2) { if (D + j >= 0 && D + j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + j][D + x]) { if (followRay(j, x, D)) { count++; } int k = 1; while (D + (k * j) >= 0 && D + (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * j)][D + (k * x)] = true; k++; } } } if (D + j >= 0 && D + j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + x][D + j]) { if (followRay(x, j, D)) { count++; } int k = 1; while (D + (k * j) >= 0 && D + (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * x)][D + (k * j)] = true; k++; } } } if (D - j >= 0 && D - j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D - j][D + x]) { if (followRay(-j, x, D)) { count++; } int k = 1; while (D - (k * j) >= 0 && D - (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D - (k * j)][D + (k * x)] = true; k++; } } } if (D - j >= 0 && D - j < checked.length && D + x >= 0 && D + x < checked.length) { if (!checked[D + x][D - j]) { if (followRay(x, -j, D)) { count++; } int k = 1; while (D - (k * j) >= 0 && D - (k * j) < checked.length && D + (k * x) >= 0 && D + (k * x) < checked.length) { checked[D + (k * x)][D - (k * j)] = true; k++; } } } } } // System.out.println(count); System.out.format("Case #%d: %d\n", i, count); } } private static boolean followRay(int dirX, int dirY, int max) { double distance = 0; posX = tarX; posY = tarY; distance += step(dirX, dirY); while (distance <= max) { if (tarX.equals(posX) && tarY.equals(posY)) { return true; } // check mirror, adjust direction if (dirX == 0) { if (posY.n == 1 && posY.z % 2 == 0) { int y = posY.z / 2; if (y >= array.length || y == 0) { return false; } int x = posX.z / posX.n / 2; if (dirY > 0 && array[y][x] || dirY < 0 && array[y - 1][x]) { dirY = -1 * dirY; } } } else if (dirY == 0) { if (posX.n == 1 && posX.z % 2 == 0) { int x = posX.z / 2; if (x >= array[0].length || x == 0) { return false; } int y = posY.z / posY.n / 2; if (dirX > 0 && array[y][x] || dirX < 0 && array[y][x - 1]) { dirX = -1 * dirX; } } } if (posX.n == 1 && posX.z % 2 == 0) { if (posY.n == 1 && posY.z % 2 == 0) { // corner int x = posX.z / 2; int y = posY.z / 2; boolean mirrored = false; if (dirX > 0) { if (array[y][x] && array[y - 1][x]) { dirX = -1 * dirX; mirrored = true; } } else { if (array[y][x - 1] && array[y - 1][x - 1]) { dirX = -1 * dirX; mirrored = true; } } if (dirY > 0) { if (array[y][x] && array[y][x - 1]) { dirY = -1 * dirY; mirrored = true; } } else { if (array[y - 1][x] && array[y - 1][x - 1]) { dirY = -1 * dirY; mirrored = true; } } if (!mirrored) { if (dirX > 0) { if (dirY > 0) { if (array[y][x]) { return false; } } else { if (array[y - 1][x]) { return false; } } } else { if (dirY > 0) { if (array[y][x - 1]) { return false; } } else { if (array[y - 1][x - 1]) { return false; } } } } } else { int x = posX.z / 2; if (x >= array[0].length || x == 0) { return false; } int y = posY.z / posY.n / 2; if (dirX > 0 && array[y][x] || dirX < 0 && array[y][x - 1]) { dirX = -1 * dirX; } } } else if (posY.n == 1 && posY.z % 2 == 0) { int y = posY.z / 2; if (y >= array.length || y == 0) { return false; } int x = posX.z / posX.n / 2; if (dirY > 0 && array[y][x] || dirY < 0 && array[y - 1][x]) { dirY = -1 * dirY; } } distance += step(dirX, dirY); } return false; } // steps until the next coord becomes integer private static double step(int dirX, int dirY) { if (dirY == 0) { if (dirX > 0) { posX = posX.plus(Rational.one); } else { posX = posX.minus(Rational.one); } return 1; } if (dirX == 0) { if (dirY > 0) { posY = posY.plus(Rational.one); } else { posY = posY.minus(Rational.one); } return 1; } if (posX.n == 1) { Rational distY = posY.fractal(); Rational speed = new Rational(Math.abs(dirY), Math.abs(dirX)); if (dirY > 0) { distY = Rational.one.minus(distY); } if (distY.equals(Rational.zero)) { distY = Rational.one; } if (distY.minus(speed).positive()) { if (dirX > 0) { posX = posX.plus(Rational.one); } else { posX = posX.minus(Rational.one); } if (dirY > 0) { posY = posY.plus(speed); } else { posY = posY.minus(speed); } return Math.sqrt(1 + speed.times(speed).value()); } else { if (dirY > 0) { posY = posY.plus(distY); } else { posY = posY.minus(distY); } Rational distX = distY.divides(speed); if (dirX > 0) { posX = posX.plus(distX); } else { posX = posX.minus(distX); } return Math.sqrt(distY.times(distY).value() + distX.times(distX).value()); } } else { Rational distX = posX.fractal(); Rational speed = new Rational(Math.abs(dirX), Math.abs(dirY)); if (dirX > 0) { distX = Rational.one.minus(distX); } if (distX.minus(speed).positive()) { if (dirY > 0) { posY = posY.plus(Rational.one); } else { posY = posY.minus(Rational.one); } if (dirX > 0) { posX = posX.plus(speed); } else { posX = posX.minus(speed); } return Math.sqrt(1 + speed.times(speed).value()); } else { if (dirX > 0) { posX = posX.plus(distX); } else { posX = posX.minus(distX); } Rational distY = distX.divides(speed); if (dirY > 0) { posY = posY.plus(distY); } else { posY = posY.minus(distY); } return Math.sqrt(distY.times(distY).value() + distX.times(distX).value()); } } } // private static void out(boolean[] array) { // System.out.println(Arrays.toString(array)); // } // // private static void out(boolean[][] array) { // int count = 0; // for (boolean[] a : array) { // System.out.print(count++ + ":"); // out(a); // } // } private static class Rational { static final Rational one = new Rational(1, 1); static final Rational zero = new Rational(0, 1); public int z; public int n; // create and initialize a new Rational object public Rational(int z, int n) { if (n == 0) { throw new RuntimeException("Denominator is zero"); } int g = gcd(z, n); this.z = z / g; this.n = n / g; } // return string representation of (this) public String toString() { if (n == 1) { return z + ""; } else { return z + "/" + n; } } // return (this * b) public Rational times(Rational b) { return new Rational(this.z * b.z, this.n * b.n); } // return (this + b) public Rational plus(Rational b) { int z = (this.z * b.n) + (this.n * b.z); int n = this.n * b.n; return new Rational(z, n); } // return (this - b) public Rational minus(Rational b) { int z = (this.z * b.n) - (this.n * b.z); int n = this.n * b.n; return new Rational(z, n); } // return fractal amount public Rational fractal() { return new Rational(z % n, n); } // return (1 / this) public Rational reciprocal() { return new Rational(n, z); } // return (this / b) public Rational divides(Rational b) { return this.times(b.reciprocal()); } public boolean positive() { return z * n >= 0; } public boolean equals(Rational r) { return r.z == this.z && r.n == this.n; } public double value() { return 1.0 * z / n; } private int gcd(int m, int n) { if (0 == n) return m; else return gcd(n, m % n); } } }
0
3,598
C20005
C20067
package gcj; import com.sun.org.apache.xerces.internal.impl.xs.opti.DefaultXMLDocumentHandler; import java.util.*; import java.io.*; public class HallOfMirrors { final static String PROBLEM_NAME = "mirrors"; final static String WORK_DIR = "D:\\Gcj\\" + PROBLEM_NAME + "\\"; int H, W, D; double stX, stY; int res = 0; String[] map; int gcd(int a, int b) { while (a>0 && b>0) if (a>b) a %= b; else b %= a; return a + b; } final double EPS = 1e-10; double getT(double cur, int V) { cur *= 2; V *= 2; if (V == 0) return 1e100; if (V > 0) { double want = Math.ceil(cur + EPS); return (want - cur) / V; } else { double want = Math.floor(cur - EPS); return (want - cur) / V; } } boolean isInteger(double x) { return Math.abs(x - Math.floor(x)) <= EPS || Math.abs(x - Math.ceil(x)) <= EPS; } boolean inMirror(double x, double y) { int xx = (int)Math.floor(x); int yy = (int)Math.floor(y); return map[xx].charAt(yy) == '#'; } void process(int dx, int dy) { double curX = stX, curY = stY; double dist = 0.0; while (true) { double t1 = getT(curX, dx); double t2 = getT(curY, dy); double t = Math.min(t1, t2); double nextX = curX + t * dx; double nextY = curY + t * dy; double piece = Math.sqrt((nextX - curX) * (nextX - curX) + (nextY - curY) * (nextY - curY)); dist += piece; if (dist > D + EPS) return; if (Math.abs(nextX - stX) <= EPS && Math.abs(nextY - stY) <= EPS) { res++; return; } curX = nextX; curY = nextY; boolean fx = isInteger(nextX); boolean fy = isInteger(nextY); if (fx && fy) { // corner // A B // C D boolean A = inMirror(nextX - EPS, nextY - EPS); boolean B = inMirror(nextX - EPS, nextY + EPS); boolean C = inMirror(nextX + EPS, nextY - EPS); boolean D = inMirror(nextX + EPS, nextY + EPS); int cnt = (A ? 1 : 0) + (B ? 1 : 0) + (C ? 1 : 0) + (D ? 1 : 0); if (cnt == 3) { dx = -dx; dy = -dy; } else if ((A && B && !C && !D) || (!A && !B && C & D)) { dx = -dx; } else if ((A && C && !B && !D) || (!A && !C && B && D)) { dy = -dy; } else if (inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY + (dy > 0 ? EPS : -EPS))) { return; } else { // just continue; } } else if (fx && !fy) { // horizontal if (dx != 0 && inMirror(nextX + (dx > 0 ? EPS : -EPS), nextY)) dx = -dx; } else if (!fx && fy) { // vertical if (dy != 0 && inMirror(nextX, nextY + (dy > 0 ? EPS : -EPS))) dy = -dy; } else { // nothing } } } void solve(Scanner sc, PrintWriter pw) { H = sc.nextInt(); W = sc.nextInt(); D = sc.nextInt(); map = new String[H]; for (int i=0; i<H; i++) map[i] = sc.next(); for (int i=0; i<H; i++) for (int j=0; j<W; j++) if (map[i].charAt(j) == 'X') { stX = i + 0.5; stY = j + 0.5; } for (int dx=-D; dx<=D; dx++) for (int dy=-D; dy<=D; dy++) if (dx*dx + dy*dy > 0 && dx*dx + dy*dy <= D * D && gcd(Math.abs(dx), Math.abs(dy)) == 1) { process(dx, dy); } pw.println(res); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new FileReader(WORK_DIR + "input.txt")); PrintWriter pw = new PrintWriter(new FileWriter(WORK_DIR + "output.txt")); int caseCnt = sc.nextInt(); for (int caseNum=0; caseNum<caseCnt; caseNum++) { System.out.println("Processing test case " + (caseNum + 1)); pw.print("Case #" + (caseNum+1) + ": "); new HallOfMirrors().solve(sc, pw); } pw.flush(); pw.close(); sc.close(); } }
import java.io.*; import java.util.*; public class Solution { private StringTokenizer st; private BufferedReader in; private PrintWriter out; final String file = "D-large"; public void solve() throws IOException { int tests = nextInt(); for (int test = 0; test < tests; ++test) { int n = nextInt(); int m = nextInt(); int d = nextInt(); char[][] f = new char[n][m]; int x0 = -1, y0 = -1; for (int i = 0; i < n; ++i) { f[i] = next().toCharArray(); for (int j = 0; j < m; ++j) { if (f[i][j] == 'X') { x0 = i; y0 = j; } } } int ans = 0; for (int dx = -d; dx <= d; ++dx) { for (int dy = -d; dy <= d; ++dy) { if ((dx != 0 || dy != 0) && dx * dx + dy * dy <= d * d && raytrace(x0, y0, x0 + dx, y0 + dy, f, 0.)) { // System.err.println(dx + " " + dy); ans++; } } } System.err.printf("Case #%d: %d%n", test + 1, ans); out.printf("Case #%d: %d%n", test + 1, ans); } } final double EPS = 1e-8; private boolean raytrace(int x0, int y0, int x1, int y1, char[][] f, double t) { double firstt = Double.POSITIVE_INFINITY; int dx = x1 - x0; int dy = y1 - y0; double tx = Double.POSITIVE_INFINITY; for (int i = Math.max(0, Math.min(x0, x1)); i < f.length && i <= Math.max(x0, x1); ++i) { for (int j = Math.max(0, Math.min(y0, y1)); j < f[0].length && j <= Math.max(y0, y1); ++j) { if (f[i][j] == 'X') { double tt = dx != 0 ? (double)(i - x0) / dx : (double)(j - y0) / dy; if (Math.abs(x0 + dx * tt - i) < EPS && Math.abs(y0 + dy * tt - j) < EPS) { tx = tt; } } if (f[i][j] != '#') { continue; } double minx = dx == 0 ? Double.NEGATIVE_INFINITY : Math.min((i - 0.5 - x0) / dx, (i + 0.5 - x0) / dx); double maxx = dx == 0 ? Double.POSITIVE_INFINITY : Math.max((i - 0.5 - x0) / dx, (i + 0.5 - x0) / dx); double miny = dy == 0 ? Double.NEGATIVE_INFINITY : Math.min((j - 0.5 - y0) / dy, (j + 0.5 - y0) / dy); double maxy = dy == 0 ? Double.POSITIVE_INFINITY : Math.max((j - 0.5 - y0) / dy, (j + 0.5 - y0) / dy); if (maxx < miny - EPS || maxy < minx - EPS) { continue; } double tt = Math.max(minx, miny); if (tt > t + EPS) { firstt = Math.min(firstt, tt); } } } if (firstt > 1.) { return x1 >= 0 && x1 < f.length && y1 >= 0 && y1 < f[0].length && f[x1][y1] == 'X'; } if (tx > t + EPS && tx < firstt) { return false; } int sx = Integer.signum(dx); int sy = Integer.signum(dy); double x = x0 + dx * firstt - 0.5; double y = y0 + dy * firstt - 0.5; int rx = (int)Math.round(x); int ry = (int)Math.round(y); if (Math.abs(rx - x) < EPS && Math.abs(ry - y) < EPS) { if (dx == 0 || dy == 0) { throw new AssertionError(); } boolean f11 = get(f, rx + (1 + sx) / 2, ry + (1 + sy) / 2); boolean f01 = get(f, rx + (1 - sx) / 2, ry + (1 + sy) / 2); boolean f10 = get(f, rx + (1 + sx) / 2, ry + (1 - sy) / 2); if (get(f, rx + (1 - sx) / 2, ry + (1 - sy) / 2) || !f11 && !f01 && !f10) { throw new AssertionError(); } if (!f11) { return raytrace(x0, y0, x1, y1, f, firstt); } if (f01 && f10 && f11) { return raytrace(2 * rx + 1 - x0, 2 * ry + 1 - y0, 2 * rx + 1 - x1, 2 * ry + 1 - y1, f, firstt); } if (f10 && f11) { return raytrace(2 * rx + 1 - x0, y0, 2 * rx + 1 - x1, y1, f, firstt); } if (f01 && f11) { return raytrace(x0, 2 * ry + 1 - y0, x1, 2 * ry + 1 - y1, f, firstt); } return false; } if (Math.abs(rx - x) < EPS) { return raytrace(2 * rx + 1 - x0, y0, 2 * rx + 1 - x1, y1, f, firstt); } if (Math.abs(ry - y) < EPS) { return raytrace(x0, 2 * ry + 1 - y0, x1, 2 * ry + 1 - y1, f, firstt); } throw new AssertionError(); } private boolean get(char[][] f, int i, int j) { return i >= 0 && i < f.length && j >= 0 && j < f[0].length && f[i][j] == '#'; } public void run() throws IOException { in = new BufferedReader(new FileReader(file + ".in")); out = new PrintWriter(file + ".out"); eat(""); solve(); out.close(); } void eat(String s) { st = new StringTokenizer(s); } String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } eat(line); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); new Solution().run(); } }
0
3,599