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
|
---|---|---|---|---|---|
C20031 | C20078 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public enum Square {
ME,
MIRROR,
EMPTY;
public static Square parse(char in) {
if (in == '#') return MIRROR;
else if (in == '.') return EMPTY;
else if (in == 'X') return ME;
else return null;
}
}
| 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 | 500 |
C20031 | C20057 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public enum Square {
ME,
MIRROR,
EMPTY;
public static Square parse(char in) {
if (in == '#') return MIRROR;
else if (in == '.') return EMPTY;
else if (in == 'X') return ME;
else return null;
}
}
| 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 | 501 |
C20031 | C20030 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public enum Square {
ME,
MIRROR,
EMPTY;
public static Square parse(char in) {
if (in == '#') return MIRROR;
else if (in == '.') return EMPTY;
else if (in == 'X') return ME;
else return null;
}
}
| /*
* 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 | 502 |
C20031 | C20002 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public enum Square {
ME,
MIRROR,
EMPTY;
public static Square parse(char in) {
if (in == '#') return MIRROR;
else if (in == '.') return EMPTY;
else if (in == 'X') return ME;
else return null;
}
}
| 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 | 503 |
C20031 | C20056 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public enum Square {
ME,
MIRROR,
EMPTY;
public static Square parse(char in) {
if (in == '#') return MIRROR;
else if (in == '.') return EMPTY;
else if (in == 'X') return ME;
else return null;
}
}
| 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 | 504 |
C20031 | C20018 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public enum Square {
ME,
MIRROR,
EMPTY;
public static Square parse(char in) {
if (in == '#') return MIRROR;
else if (in == '.') return EMPTY;
else if (in == 'X') return ME;
else return null;
}
}
| 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 | 505 |
C20031 | C20051 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public enum Square {
ME,
MIRROR,
EMPTY;
public static Square parse(char in) {
if (in == '#') return MIRROR;
else if (in == '.') return EMPTY;
else if (in == 'X') return ME;
else return null;
}
}
| 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 | 506 |
C20003 | C20029 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| 0 | 507 |
C20003 | C20040 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package jp.funnything.competition.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.apache.commons.io.IOUtils;
public class QuestionReader {
private final BufferedReader _reader;
public QuestionReader( final File input ) {
try {
_reader = new BufferedReader( new FileReader( input ) );
} catch ( final FileNotFoundException e ) {
throw new RuntimeException( e );
}
}
public void close() {
IOUtils.closeQuietly( _reader );
}
public String read() {
try {
return _reader.readLine();
} catch ( final IOException e ) {
throw new RuntimeException( e );
}
}
public BigDecimal[] readBigDecimals() {
return readBigDecimals( " " );
}
public BigDecimal[] readBigDecimals( final String separator ) {
final String[] tokens = readTokens( separator );
final BigDecimal[] values = new BigDecimal[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = new BigDecimal( tokens[ index ] );
}
return values;
}
public BigInteger[] readBigInts() {
return readBigInts( " " );
}
public BigInteger[] readBigInts( final String separator ) {
final String[] tokens = readTokens( separator );
final BigInteger[] values = new BigInteger[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = new BigInteger( tokens[ index ] );
}
return values;
}
public int readInt() {
final int[] values = readInts();
if ( values.length != 1 ) {
throw new IllegalStateException( "Try to read single interger, but the line contains zero or multiple values" );
}
return values[ 0 ];
}
public int[] readInts() {
return readInts( " " );
}
public int[] readInts( final String separator ) {
final String[] tokens = readTokens( separator );
final int[] values = new int[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = Integer.parseInt( tokens[ index ] );
}
return values;
}
public long readLong() {
final long[] values = readLongs();
if ( values.length != 1 ) {
throw new IllegalStateException( "Try to read single interger, but the line contains zero or multiple values" );
}
return values[ 0 ];
}
public long[] readLongs() {
return readLongs( " " );
}
public long[] readLongs( final String separator ) {
final String[] tokens = readTokens( separator );
final long[] values = new long[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = Long.parseLong( tokens[ index ] );
}
return values;
}
public String[] readTokens() {
return readTokens( " " );
}
public String[] readTokens( final String separator ) {
return read().split( separator );
}
}
| 0 | 508 |
C20003 | C20035 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Surround {
private Square[][] surround;
public Surround(Square[][] surround) {
this.surround = surround;
}
public Square get(int x, int y) {
return surround[y][x];
}
}
| 0 | 509 |
C20003 | C20045 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 510 |
C20003 | C20052 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 511 |
C20003 | C20073 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* Main.java
*
* Created on 14.04.2012, 10:03:46
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package codejam12;
import qualification.CodeJamQuali;
/**
*
* @author Besitzer
*/
public class Main {
/**
* @param args the command line arguments
*/
/*public static void main(String[] args) {
char[] C = new char[26];
CodeJamQuali CJQ =new CodeJamQuali();
CJQ.fillDict("our language is impossible to understand","ejp mysljylc kd kxveddknmc re jsicpdrysi",C);
CJQ.fillDict("there are twenty six factorial possibilities","rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd",C);
CJQ.fillDict("so it is okay if you want to just give up","de kr kd eoya kw aej tysr re ujdr lkgc jv",C);
C['z'-'a']='q';
C['q'-'a']='z';
System.out.println("abcdefghijklmnopqrstuvwxyz");
System.out.println(C);
for(int i =0;i<26;i++)if(C[i]=='z')System.out.println("found");
}*/
public static void main(String[] args) {
CodeJamQuali CJQ =new CodeJamQuali();
//CJQ.go("src/qualification/A-small-attempt0.in", 1);
CJQ.go("src/qualification/D-large.in", 4);
//System.out.println(new java.math.BigInteger("2").gcd(java.math.BigInteger.ZERO));
}
}
| 0 | 512 |
C20003 | C20043 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package jp.funnything.competition.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
public class Packer {
private static void add( final ZipArchiveOutputStream out , final File file , final int pathPrefix ) {
if ( file.isDirectory() ) {
final File[] children = file.listFiles();
if ( children.length > 0 ) {
for ( final File child : children ) {
add( out , child , pathPrefix );
}
} else {
addEntry( out , file , pathPrefix , false );
}
} else {
addEntry( out , file , pathPrefix , true );
}
}
private static void addEntry( final ZipArchiveOutputStream out , final File file , final int pathPrefix , final boolean isFile ) {
try {
out.putArchiveEntry( new ZipArchiveEntry( file.getPath().substring( pathPrefix ) + ( isFile ? "" : "/" ) ) );
if ( isFile ) {
final FileInputStream in = FileUtils.openInputStream( file );
IOUtils.copy( in , out );
IOUtils.closeQuietly( in );
}
out.closeArchiveEntry();
} catch ( final IOException e ) {
throw new RuntimeException( e );
}
}
public static void pack( final File source , final File destination ) {
try {
final ZipArchiveOutputStream out = new ZipArchiveOutputStream( destination );
add( out , source , FilenameUtils.getPath( source.getPath() ).length() );
out.finish();
out.close();
} catch ( final IOException e ) {
throw new RuntimeException( e );
}
}
}
| 0 | 513 |
C20003 | C20083 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class QuestionD {
public static void doPuzzle() {
try {
File questionfile = new File("D.in");
BufferedReader questionreader = new BufferedReader(new FileReader(questionfile));
File answerfile = new File("D.out");
PrintWriter answerwriter = new PrintWriter(new BufferedWriter(new FileWriter(answerfile)));
String[] params = null;
String question = questionreader.readLine();
int T = Integer.parseInt(question);
int[] A = new int[T];
int[] B = new int[T];
for (int i = 0; i < T; i++) {
question = questionreader.readLine();
params = question.split(" ");
int H = Integer.parseInt(params[0]);
int W = Integer.parseInt(params[1]);
int D = Integer.parseInt(params[2]);
String[] M = new String[H];
for (int j = 0; j < H; j++) M[j] = questionreader.readLine();
answerwriter.println("Case #" + (i+1) + ": " + analyze(H, W, D, M));
}
answerwriter.close();
questionreader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
}
private static int analyze(int H, int W, int D, String[] M) {
int CX = 0;
int CY = 0;
char[][] realMap = new char[H][W];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char c = M[i].charAt(j);
realMap[i][j] = c;
if (c == 'X') {
CX = i;
CY = j;
}
}
}
boolean[][] virtualMap = new boolean[D*2+1][D*2+1];
int answer = 0;
for (int i = 0; i < D; i++) {
for (int j = 0; j <= D*2; j++) {
if (checkMirror(H, W, D, CX, CY, realMap, virtualMap, i, j)) answer++;
}
}
{
int i = D;
for (int j = 0; j < D; j++) {
if (checkMirror(H, W, D, CX, CY, realMap, virtualMap, i, j)) answer++;
}
for (int j = D*2; j > D; j--) {
if (checkMirror(H, W, D, CX, CY, realMap, virtualMap, i, j)) answer++;
}
}
for (int i = D*2; i > D; i--) {
for (int j = 0; j <= D*2; j++) {
if (checkMirror(H, W, D, CX, CY, realMap, virtualMap, i, j)) answer++;
}
}
return answer;
}
private static int[] delta = { 1, -1 };
private static int[] reverse = { 1, 0 };
private static boolean checkMirror(int H, int W, int D, int CX, int CY, char[][] realMap, boolean[][] virtualMap, int PX, int PY) {
if (virtualMap[PX][PY]) return false;
int dx = Math.abs(PX - D);
int dy = Math.abs(PY - D);
if ((D * D) < (dx * dx + dy * dy)) return false;
dx *= 2;
dy *= 2;
int rx = CX;
int ry = CY;
int drx = (PX > D) ? 0 : 1;
int dry = (PY > D) ? 0 : 1;
int dpx = drx;
int dpy = dry;
if (dx > dy) {
for (int x = 1, oy = 0; x <= dx; x++) {
int y = (dy * x) / dx;
if ((((dy * x) % dx) == 0) && ((x % 2) == 0) && ((y % 2) == 0)) {
//å°éãã©ã°
if (virtualMap[D+(x/2)*delta[dpx]][D+(y/2)*delta[dpy]]) return false;
virtualMap[D+(x/2)*delta[dpx]][D+(y/2)*delta[dpy]] = true;
//åå¤å®
if (realMap[rx][ry] == 'X') return true;
oy = y;
continue;
} else if (y > oy) {
oy = y;
if (((dy * x) % dx) == 0) {
if (((x % 2) == 1) && ((y % 2) == 1)) {
//è§åå°
if (realMap[rx+delta[drx]][ry+delta[dry]] != '#') {
rx += delta[drx];
ry += delta[dry];
} else {
if (realMap[rx+delta[drx]][ry] != '#') rx += delta[drx];
else drx = reverse[drx];
if (realMap[rx][ry+delta[dry]] != '#') ry += delta[dry];
else dry = reverse[dry];
}
continue;
}
}
if ((y % 2) == 1) {
//yåå°
if (realMap[rx][ry+delta[dry]] != '#') ry += delta[dry];
else dry = reverse[dry];
}
}
if ((x % 2) == 1) {
//xåå°
if (realMap[rx+delta[drx]][ry] != '#') rx += delta[drx];
else drx = reverse[drx];
}
}
} else {
for (int y = 1, ox = 0; y <= dy; y++) {
int x = (dx * y) / dy;
if ((((dx * y) % dy) == 0) && ((x % 2) == 0) && ((y % 2) == 0)) {
//å°éãã©ã°
if (virtualMap[D+(x/2)*delta[dpx]][D+(y/2)*delta[dpy]]) return false;
virtualMap[D+(x/2)*delta[dpx]][D+(y/2)*delta[dpy]] = true;
//åå¤å®
if (realMap[rx][ry] == 'X') return true;
ox = x;
continue;
} else if (x > ox) {
ox = x;
if (((dx * y) % dy) == 0) {
if (((x % 2) == 1) && ((y % 2) == 1)) {
//è§åå°
if (realMap[rx+delta[drx]][ry+delta[dry]] != '#') {
rx += delta[drx];
ry += delta[dry];
} else {
if (realMap[rx+delta[drx]][ry] != '#') rx += delta[drx];
else drx = reverse[drx];
if (realMap[rx][ry+delta[dry]] != '#') ry += delta[dry];
else dry = reverse[dry];
}
continue;
}
}
if ((x % 2) == 1) {
//xåå°
if (realMap[rx+delta[drx]][ry] != '#') rx += delta[drx];
else drx = reverse[drx];
}
}
if ((y % 2) == 1) {
//yåå°
if (realMap[rx][ry+delta[dry]] != '#') ry += delta[dry];
else dry = reverse[dry];
}
}
}
return false;
}
}
| 0 | 514 |
C20003 | C20023 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package template;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class TestCase {
private boolean isSolved;
private Object solution;
private Map<String, Integer> intProperties;
private Map<String, ArrayList<Integer>> intArrayProperties;
private Map<String, ArrayList<ArrayList<Integer>>> intArray2DProperties;
private Map<String, Double> doubleProperties;
private Map<String, ArrayList<Double>> doubleArrayProperties;
private Map<String, ArrayList<ArrayList<Double>>> doubleArray2DProperties;
private Map<String, String> stringProperties;
private Map<String, ArrayList<String>> stringArrayProperties;
private Map<String, ArrayList<ArrayList<String>>> stringArray2DProperties;
private Map<String, Boolean> booleanProperties;
private Map<String, ArrayList<Boolean>> booleanArrayProperties;
private Map<String, ArrayList<ArrayList<Boolean>>> booleanArray2DProperties;
private Map<String, Long> longProperties;
private Map<String, ArrayList<Long>> longArrayProperties;
private Map<String, ArrayList<ArrayList<Long>>> longArray2DProperties;
private int ref;
private double time;
public TestCase() {
initialise();
}
private void initialise() {
isSolved = false;
intProperties = new HashMap<>();
intArrayProperties = new HashMap<>();
intArray2DProperties = new HashMap<>();
doubleProperties = new HashMap<>();
doubleArrayProperties = new HashMap<>();
doubleArray2DProperties = new HashMap<>();
stringProperties = new HashMap<>();
stringArrayProperties = new HashMap<>();
stringArray2DProperties = new HashMap<>();
booleanProperties = new HashMap<>();
booleanArrayProperties = new HashMap<>();
booleanArray2DProperties = new HashMap<>();
longProperties = new HashMap<>();
longArrayProperties = new HashMap<>();
longArray2DProperties = new HashMap<>();
ref = 0;
}
public void setSolution(Object o) {
solution = o;
isSolved = true;
}
public Object getSolution() {
if (!isSolved) {
Utils.die("getSolution on unsolved testcase");
}
return solution;
}
public void setRef(int i) {
ref = i;
}
public int getRef() {
return ref;
}
public void setTime(double d) {
time = d;
}
public double getTime() {
return time;
}
public void setInteger(String s, Integer i) {
intProperties.put(s, i);
}
public Integer getInteger(String s) {
return intProperties.get(s);
}
public void setIntegerList(String s, ArrayList<Integer> l) {
intArrayProperties.put(s, l);
}
public void setIntegerMatrix(String s, ArrayList<ArrayList<Integer>> l) {
intArray2DProperties.put(s, l);
}
public ArrayList<Integer> getIntegerList(String s) {
return intArrayProperties.get(s);
}
public Integer getIntegerListItem(String s, int i) {
return intArrayProperties.get(s).get(i);
}
public ArrayList<ArrayList<Integer>> getIntegerMatrix(String s) {
return intArray2DProperties.get(s);
}
public ArrayList<Integer> getIntegerMatrixRow(String s, int row) {
return intArray2DProperties.get(s).get(row);
}
public Integer getIntegerMatrixItem(String s, int row, int column) {
return intArray2DProperties.get(s).get(row).get(column);
}
public ArrayList<Integer> getIntegerMatrixColumn(String s, int column) {
ArrayList<Integer> out = new ArrayList();
for(ArrayList<Integer> row : intArray2DProperties.get(s)) {
out.add(row.get(column));
}
return out;
}
public void setDouble(String s, Double i) {
doubleProperties.put(s, i);
}
public Double getDouble(String s) {
return doubleProperties.get(s);
}
public void setDoubleList(String s, ArrayList<Double> l) {
doubleArrayProperties.put(s, l);
}
public void setDoubleMatrix(String s, ArrayList<ArrayList<Double>> l) {
doubleArray2DProperties.put(s, l);
}
public ArrayList<Double> getDoubleList(String s) {
return doubleArrayProperties.get(s);
}
public Double getDoubleListItem(String s, int i) {
return doubleArrayProperties.get(s).get(i);
}
public ArrayList<ArrayList<Double>> getDoubleMatrix(String s) {
return doubleArray2DProperties.get(s);
}
public ArrayList<Double> getDoubleMatrixRow(String s, int row) {
return doubleArray2DProperties.get(s).get(row);
}
public Double getDoubleMatrixItem(String s, int row, int column) {
return doubleArray2DProperties.get(s).get(row).get(column);
}
public ArrayList<Double> getDoubleMatrixColumn(String s, int column) {
ArrayList<Double> out = new ArrayList();
for(ArrayList<Double> row : doubleArray2DProperties.get(s)) {
out.add(row.get(column));
}
return out;
}
public void setString(String s, String t) {
stringProperties.put(s, t);
}
public String getString(String s) {
return stringProperties.get(s);
}
public void setStringList(String s, ArrayList<String> l) {
stringArrayProperties.put(s, l);
}
public void setStringMatrix(String s, ArrayList<ArrayList<String>> l) {
stringArray2DProperties.put(s, l);
}
public ArrayList<String> getStringList(String s) {
return stringArrayProperties.get(s);
}
public String getStringListItem(String s, int i) {
return stringArrayProperties.get(s).get(i);
}
public ArrayList<ArrayList<String>> getStringMatrix(String s) {
return stringArray2DProperties.get(s);
}
public ArrayList<String> getStringMatrixRow(String s, int row) {
return stringArray2DProperties.get(s).get(row);
}
public String getStringMatrixItem(String s, int row, int column) {
return stringArray2DProperties.get(s).get(row).get(column);
}
public ArrayList<String> getStringMatrixColumn(String s, int column) {
ArrayList<String> out = new ArrayList();
for(ArrayList<String> row : stringArray2DProperties.get(s)) {
out.add(row.get(column));
}
return out;
}
public void setBoolean(String s, Boolean b) {
booleanProperties.put(s, b);
}
public Boolean getBoolean(String s) {
return booleanProperties.get(s);
}
public void setBooleanList(String s, ArrayList<Boolean> l) {
booleanArrayProperties.put(s, l);
}
public void setBooleanMatrix(String s, ArrayList<ArrayList<Boolean>> l) {
booleanArray2DProperties.put(s, l);
}
public ArrayList<Boolean> getBooleanList(String s) {
return booleanArrayProperties.get(s);
}
public Boolean getBooleanListItem(String s, int i) {
return booleanArrayProperties.get(s).get(i);
}
public ArrayList<ArrayList<Boolean>> getBooleanMatrix(String s) {
return booleanArray2DProperties.get(s);
}
public ArrayList<Boolean> getBooleanMatrixRow(String s, int row) {
return booleanArray2DProperties.get(s).get(row);
}
public Boolean getBooleanMatrixItem(String s, int row, int column) {
return booleanArray2DProperties.get(s).get(row).get(column);
}
public ArrayList<Boolean> getBooleanMatrixColumn(String s, int column) {
ArrayList<Boolean> out = new ArrayList();
for(ArrayList<Boolean> row : booleanArray2DProperties.get(s)) {
out.add(row.get(column));
}
return out;
}
public void setLong(String s, Long b) {
longProperties.put(s, b);
}
public Long getLong(String s) {
return longProperties.get(s);
}
public void setLongList(String s, ArrayList<Long> l) {
longArrayProperties.put(s, l);
}
public void setLongMatrix(String s, ArrayList<ArrayList<Long>> l) {
longArray2DProperties.put(s, l);
}
public ArrayList<Long> getLongList(String s) {
return longArrayProperties.get(s);
}
public Long getLongListItem(String s, int i) {
return longArrayProperties.get(s).get(i);
}
public ArrayList<ArrayList<Long>> getLongMatrix(String s) {
return longArray2DProperties.get(s);
}
public ArrayList<Long> getLongMatrixRow(String s, int row) {
return longArray2DProperties.get(s).get(row);
}
public Long getLongMatrixItem(String s, int row, int column) {
return longArray2DProperties.get(s).get(row).get(column);
}
public ArrayList<Long> getLongMatrixColumn(String s, int column) {
ArrayList<Long> out = new ArrayList();
for(ArrayList<Long> row : longArray2DProperties.get(s)) {
out.add(row.get(column));
}
return out;
}
}
| 0 | 515 |
C20003 | C20086 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| //input file must be in.txt in this directory
//output file will be out.txt
import java.io.*;
import java.util.*;
public class D
{
public static Scanner in;
public static PrintStream out;
public static void main(String [] args) throws Throwable
{
in = new Scanner(new File("in.txt"));
int cases = in.nextInt();
in.nextLine();
out = new PrintStream(new File("out.txt"));
for (int i = 1; i <= cases; i++)
{
out.print("Case #" + i + ": ");
printResult();
out.println();
}
}
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static boolean canSeeImage(int dx, int dy, int lx, int ly, int d, boolean [][] house)
{
//System.out.println("Trying (" + dx + "," + dy + "):");
int cx = lx;
int cy = ly;
int sx, sy;
if (dx > 0)
sx = 1;
else
sx = -1;
if (dy > 0)
sy = 1;
else
sy = -1;
dx = Math.abs(dx);
dy = Math.abs(dy);
int i = 0;
int totx = 0;
int toty = 0;
boolean t;
boolean mx,my;
boolean nx = false,ny = false;
while (totx*totx + toty*toty <= d*d)
{
if (dx == 0)
{
my = true;
mx = false;
t = true;
}
else
{
if (dy == 0)
{
mx = true;
my = false;
t = true;
}
else
{
mx = i % dy == 0;
my = i % dx == 0;
t = mx && my && !nx && !ny;
if (mx)
{
mx = nx;
nx = !nx;
}
if (my)
{
my = ny;
ny = !ny;
}
}
}
if (t && totx+toty > 0 && cx == lx && cy == ly)
return true;
//if (mx || my)
// printHouse(lx,ly,cx,cy,house);
if (mx)
totx++;
if (my)
toty++;
if (mx && my)
{
if (house[cx + sx][cy + sy])
{
if (house[cx][cy + sy])
{
if (house[cx + sx][cy])
{
sx = -sx;
sy = -sy;
}
else
{
sy = -sy;
cx += sx;
}
}
else
{
if (house[cx + sx][cy])
{
sx = -sx;
cy += sy;
}
else
{
return false;
}
}
}
else
{
cx += sx;
cy += sy;
}
}
else
{
if (mx)
{
if (house[cx + sx][cy])
{
sx = -sx;
}
else
{
cx += sx;
}
}
if (my)
{
if (house[cx][cy + sy])
{
sy = -sy;
}
else
{
cy += sy;
}
}
}
i++;
}
return false;
}
public static void printHouse(int lx, int ly, int cx, int cy, boolean [][] house)
{
for (int y = 0; y < house[0].length; y++)
{
for (int x = 0; x < house.length; x++)
{
if (x == cx && y == cy)
{
System.out.print('*');
}
else if (x == lx && y == ly)
{
System.out.print('X');
}
else if (house[x][y])
{
System.out.print('#');
}
else
{
System.out.print(' ');
}
}
System.out.println();
}
}
public static int[][] getPossibilities(int d)
{
ArrayList<Integer> lx = new ArrayList<Integer>();
ArrayList<Integer> ly = new ArrayList<Integer>();
int h, v;
for (int x = -d; x <= d; x++)
{
for (int y = -d; y <= d; y++)
{
h = Math.abs(x);
v = Math.abs(y);
if (gcd(h,v) == 1 && h*h + v*v <= d*d)
{
lx.add(x);
ly.add(y);
}
}
}
int [][] possibilities = new int [lx.size()][2];
for (int i = 0; i < possibilities.length; i++)
{
possibilities[i][0] = lx.get(i);
possibilities[i][1] = ly.get(i);
}
return possibilities;
}
public static void printResult()
{
int h,w,d,lx = 0,ly = 0;
h = in.nextInt();
w = in.nextInt();
d = in.nextInt();
in.nextLine();
String s;
boolean [][] house = new boolean[w][h];
for (int y = 0; y < h; y++)
{
s = in.nextLine();
for (int x = 0; x < w; x++)
{
house[x][y] = s.charAt(x) == '#';
if (s.charAt(x) == 'X')
{
lx = x;
ly = y;
}
}
}
int [][] poss = getPossibilities(d);
int images = 0;
for (int i = 0; i < poss.length; i++)
{
if (canSeeImage(poss[i][0], poss[i][1], lx, ly, d, house))
{
images++;
//System.out.println("True");
}
//else
//System.out.println("False");
}
out.print(images);
}
}
| 0 | 516 |
C20003 | C20075 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashSet;
import java.util.Scanner;
public class CodeJamD
{
public static void main(String args[]) throws Exception
{
Scanner in = new Scanner(new File("in.txt"));
BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"));
int cases = in.nextInt();
for(int casenum = 1;casenum <= cases;casenum++)
{
int H = in.nextInt();
int W = in.nextInt();
int D = in.nextInt();
in.nextLine();
in.nextLine();
boolean x[][] = new boolean[2 * (H - 2)][2 * (W - 2)];
int R = 0,C = 0;
for(int n = 0;n < H - 2;n++)
{
String str = in.nextLine();
for(int i = 0;i < W - 2;i++)
{
boolean b = false;
if(str.charAt(i + 1) == 'X')
{
R = n;
C = i;
b = true;
}
x[n][i] = b;
x[x.length - 1 - n][i] = b;
x[n][x[0].length - 1 - i] = b;
x[x.length - 1 - n][x[0].length - 1 - i] = b;
}
}
in.nextLine();
int count = 0;
HashSet<String> set = new HashSet<String>();
for(int a = -D;a <= D;a++)
{
for(int b = -D;b <= D;b++)
{
if(a * a + b * b > D * D)
continue;
if(a == 0 && b == 0)
continue;
if(x[(R + a + D * (x.length)) % (x.length)][(C + b + D * (x[0].length)) % (x[0].length)])
{
int gcf = gcf(a,b);
int a2 = a/gcf;
int b2 = b/gcf;
String s = a2 + " " + b2;
if(!set.contains(s))
{
set.add(s);
count++;
}
}
}
}
out.write("Case #" + casenum + ": " + count + "\n");
}
in.close();
out.close();
}
public static int gcf(int a,int b)
{
if(a<0)
a = -a;
if(b<0)
b = -b;
if (b == 0) return a;
else return (gcf(b, a % b));
}
} | 0 | 517 |
C20003 | C20048 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 518 |
C20003 | C20074 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package codejam2012.q;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
public class D
{
static int H, W, D;
static char[][] hall;
static int startx, starty;
static public int test(int dx, int dy, int dirx, int diry)
{
//System.out.println("test " + dx + "/" + dy + " " + dirx + "/" + diry);
int hx = startx, hy = starty;
int rx = dy, ry = dx;
int tx = 0, ty = 0;
while( tx*tx + ty*ty <= D*D ){
if( hx==startx && hy==starty && tx+ty>0 ){
if( rx==2*dy && ry==dx+dy )
return 1;
if( ry==2*dx && rx==dy+dx )
return 1;
}
if( rx < ry ){
ry -= rx;
rx = 2*dy;
if( hall[hx+dirx][hy]=='#' ){
dirx = -dirx;
} else {
hx += dirx;
}
tx++;
} else
if( rx > ry ){
rx -= ry;
ry = 2*dx;
if( hall[hx][hy+diry]=='#' ){
diry = -diry;
} else {
hy += diry;
}
ty++;
} else
if( rx==ry ){
rx = 2*dy;
ry = 2*dx;
if( hall[hx+dirx][hy+diry]!='#' ){
hx += dirx;
hy += diry;
} else
if( hall[hx+dirx][hy]!='#' && hall[hx][hy+diry]!='#' ){
return 0;
} else {
if( hall[hx+dirx][hy]=='#' ){
dirx = -dirx;
} else {
hx += dirx;
}
if( hall[hx][hy+diry]=='#' ){
diry = -diry;
} else {
hy += diry;
}
}
tx++;
ty++;
}
}
return 0;
}
static public int multiTest(int x0, int y0, int x1, int y1)
{
//System.out.println("multitest " + x0 + "/" + y0 + " " + x1 + "/" + y1) ;
int count = 0;
int x2 = x0+x1;
int y2 = y0+y1;
if( x2*x2 + y2*y2 <= D*D ){
count += multiTest(x0, y0, x2, y2);
count += test(x2, y2, +1, +1);
count += test(x2, y2, +1, -1);
count += test(x2, y2, -1, +1);
count += test(x2, y2, -1, -1);
count += multiTest(x2, y2, x1, y1);
}
return count;
}
static public void solveOne(int caseNo, Scanner in, PrintStream out)
{
H = in.nextInt();
W = in.nextInt();
D = in.nextInt();
in.nextLine();
hall = new char[W][H];
for( int y=0 ; y<H ; y++ ){
String line = in.nextLine();
for( int x=0 ; x<W ; x++ ){
hall[x][y] = line.charAt(x);
if( hall[x][y]=='X' ){
startx = x;
starty = y;
}
}
}
int count = 0;
count += test(1, 0, +1, 0);
count += test(1, 0, -1, 0);
count += test(0, 1, 0, +1);
count += test(0, 1, 0, -1);
count += multiTest(1,0,0,1);
String ans = "Case #" + caseNo + ": " + count;
System.out.println(ans);
if( out!=null ){
out.println(ans);
}
}
static public void solveAll(String infilename, String outfilename)
{
try {
FileInputStream stream = new FileInputStream(infilename);
Scanner in = new Scanner(stream);
PrintStream out = null;
if( outfilename!=null ){
out = new PrintStream(outfilename);
System.out.println("--- " + outfilename);
}
int nbcases = in.nextInt();
in.nextLine();
for( int n=1 ; n<=nbcases ; n++ ){
solveOne(n, in, out);
} // for n
} catch( IOException ex ){
ex.printStackTrace();
}
}
public static void main(String[] args)
{
if( args.length==0 ){
String cname = D.class.getName().replaceAll("\\.", "/");
String path = "src/" + cname + "-sample";
// String path = "src/" + cname + "-small-attempt0";
// String path = "src/" + cname + "-large";
solveAll(path + ".in.txt", path + ".out");
} else {
String dir = "src/" + D.class.getPackage().getName().replaceAll("\\.", "/") + "/";
String path = dir + args[0];
solveAll(path + ".in.txt", path + ".out");
}
}
}
| 0 | 519 |
C20003 | C20019 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package template;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
public class TestCaseSolver implements Runnable {
private ArrayList<TestCase> testCases;
private int ref;
public TestCaseSolver(ArrayList<TestCase> tcs, int ref) {
testCases = tcs;
this.ref = ref;
}
public TestCaseSolver(TestCase tc, int ref) {
ArrayList<TestCase> tcs = new ArrayList<>();
tcs.add(tc);
testCases = tcs;
this.ref = ref;
}
public int getRef() {
return ref;
}
@Override
public void run() {
for (TestCase tc : testCases) {
long startTime = System.nanoTime();
solve(tc);
long duration = System.nanoTime() - startTime;
double secs = (double) duration / (1000000000d);
tc.setTime(secs);
System.out.println("Thread " + ref + " solved testcase " + tc.getRef() + " in " + String.format("%.2f", secs) + " secs.");
}
}
private void solve(TestCase tc) {
int H = tc.getInteger("H");
int W = tc.getInteger("W");
int D = tc.getInteger("D");
ArrayList<String> rows = tc.getStringList("rows");
boolean[][] horizMirrors = new boolean[W+1][H+1]; //mirror from x,y to x+1,y
boolean[][] vertMirrors = new boolean[W + 1][H + 1]; //mirror from x,y to x+1,y
boolean[][] isMirrorCell = new boolean[W][H]; //mirror at x,y
Pair<Integer, Integer> myLoc = new Pair<>(0, 0);
int myX = 0;
int myY = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (rows.get(i).substring(j, j + 1).equals("X")) {
myLoc.setO1(j);
myX = j;
myLoc.setO2(H - i - 1);
myY = H - i - 1;
}
if (rows.get(i).substring(j, j + 1).equals("#")) {
isMirrorCell[j][H - i - 1] = true;
horizMirrors[j][H - i - 1] = true;
horizMirrors[j][H - i - 1 + 1] = true;
vertMirrors[j][H - i - 1] = true;
vertMirrors[j + 1][H - i - 1] = true;
}
}
}
Utils.sout("myLoc " + myLoc);
Set<Double> angles = new HashSet<>();
for (int targetx = -D; targetx <= D; targetx++) {
for (int targety = -D; targety <= D; targety++) {
if (targetx == 0 && targety == 0) {
continue;
}
boolean xflipped = false;
boolean yflipped = false;
int subgrid = Math.max(Math.abs(targetx), 1) * Math.max(Math.abs(targety), 1);
subgrid *= 2;
int mySubX = myX * subgrid + subgrid / 2;
int mySubY = myY * subgrid + subgrid / 2;
int xloc = mySubX;
int yloc = mySubY;
double unitStep = Math.sqrt((double)targetx * (double)targetx + (double)targety * (double)targety);
int numStepsAllowed = (int)((double)D * (double)subgrid / unitStep);
//walk
int steps = 0;
boolean seen = false;
boolean dead = false;
while (steps < numStepsAllowed && !seen && !dead) {
xloc += targetx * (xflipped ? -1 : 1);
yloc += targety * (yflipped ? -1 : 1);
if (xloc == mySubX && yloc == mySubY) {seen = true;}
//inner horiz edge
if(yloc % subgrid == 0 && xloc % subgrid != 0) {
if (horizMirrors[xloc / subgrid][yloc / subgrid]) {yflipped = !yflipped;}
}
//inner vert edge
if(xloc % subgrid == 0 && yloc % subgrid != 0) {
if (vertMirrors[xloc / subgrid][yloc / subgrid]) {xflipped = !xflipped;}
}
//corner
if(xloc % subgrid == 0 && yloc % subgrid == 0) {
int prevX = xloc - targetx * (xflipped ? -1 : 1);
int prevY = yloc - targety * (yflipped ? -1 : 1);
int nextX = xloc + targetx * (xflipped ? -1 : 1);
int nextY = yloc + targety * (yflipped ? -1 : 1);
boolean nextBlock = isMirrorCell[nextX / subgrid][nextY / subgrid];
boolean lastHoriz = horizMirrors[prevX / subgrid][yloc / subgrid];
boolean lastVert = vertMirrors[xloc / subgrid][prevY / subgrid];
if (lastVert && nextBlock) {
xflipped = !xflipped;
}
if (lastHoriz && nextBlock) {
yflipped = !yflipped;
}
if (!lastHoriz && !lastVert && nextBlock) {
dead = true;
}
}
steps++;
} //end while
if (seen) {
double angle = Math.atan2((double)targety, (double)targetx);
angles.add(angle);
//System.out.println("added " + targetx + " " + targety + " in " + steps + " steps " + (steps * unitStep / subgrid) + " dist.");
}
} //end y target
} //end x target
System.out.println("total " + angles.size());
tc.setSolution(new Integer(angles.size()));
}
private double distance(Pair<Integer, Integer> loc1, Pair<Integer, Integer> loc2) {
int x = Math.abs(loc1.getO1() - loc2.getO1());
int y = Math.abs(loc1.getO2() - loc2.getO2());
if (y == 0) {
return (double) x;
}
if (x == 0) {
return (double) y;
}
return Math.sqrt((double) (x * x + y * y));
}
}
| 0 | 520 |
C20003 | C20021 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package template;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.ArrayList;
public class TestCaseIO {
public static ArrayList<TestCase> loadFromFile(String infile) {
//inc checks on #lines, length of splits, etc.
BufferedReader br = Utils.newBufferedReader(infile);
ArrayList<TestCase> tcList = new ArrayList<>();
//first line is N, the number of test cases
Integer N = Utils.readInteger(br);
//cycle through N test cases
for (int x = 0; x < N; x++) {
TestCase tc = new TestCase();
tc.setRef(x + 1);
///////////////////////////////////////////////////////////
//load properties - the bit to customise per problem
ArrayList<Integer> l = Utils.readIntegerList(br, 3);
tc.setInteger("H", l.get(0));
tc.setInteger("W", l.get(1));
tc.setInteger("D", l.get(2));
tc.setStringList("rows", Utils.readMultipleStrings(br, l.get(0)));
///////////////////////////////////////////////////////////
tcList.add(tc);
}
//confirm end of file
if (Utils.readLn(br) != null) {
Utils.die("Unexpected text at end of infile");
}
return tcList;
}
public static ArrayList<TestCase> mockUp() {
ArrayList<TestCase> tcList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
TestCase tc = new TestCase();
tc.setRef(i);
int[] xs = Utils.randomIntegerArray(100, -50, 50, i * 2);
int[] ys = Utils.randomIntegerArray(100, -50, 50, i * 2 + 1);
tc.setIntegerList("xs", Utils.arrayToArrayList(xs));
tc.setIntegerList("ys", Utils.arrayToArrayList(ys));
tcList.add(tc);
}
return tcList;
}
public static void writeSolutions(ArrayList<TestCase> tcList, String outfile) {
BufferedWriter bw = Utils.newBufferedWriter(outfile, false);
for (int i = 0; i < tcList.size(); i++) {
TestCase tc = tcList.get(i);
Object solution = tc.getSolution();
String line = "Case #" + (i + 1) + ": " + solution.toString();
Utils.writeLn(bw, line);
}
Utils.closeBw(bw);
}
}
| 0 | 521 |
C20003 | C20044 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package jp.funnything.competition.util;
import java.util.Comparator;
import com.google.common.base.Objects;
public class Pair< T1 , T2 > {
public static < T1 extends Comparable< T1 > , T2 extends Comparable< T2 > > Comparator< Pair< T1 , T2 > > getFirstComarator() {
return new Comparator< Pair< T1 , T2 > >() {
@Override
public int compare( final Pair< T1 , T2 > o1 , final Pair< T1 , T2 > o2 ) {
final int c = o1.first.compareTo( o2.first );
return c != 0 ? c : o1.second.compareTo( o2.second );
}
};
}
public static < T1 extends Comparable< T1 > , T2 extends Comparable< T2 > > Comparator< Pair< T1 , T2 > > getSecondComarator() {
return new Comparator< Pair< T1 , T2 > >() {
@Override
public int compare( final Pair< T1 , T2 > o1 , final Pair< T1 , T2 > o2 ) {
final int c = o1.second.compareTo( o2.second );
return c != 0 ? c : o1.first.compareTo( o2.first );
}
};
}
public T1 first;
public T2 second;
public Pair( final Pair< T1 , T2 > that ) {
this.first = that.first;
this.second = that.second;
}
public Pair( final T1 first , final T2 second ) {
this.first = first;
this.second = second;
}
@Override
public boolean equals( final Object obj ) {
if ( this == obj ) {
return true;
}
if ( obj == null || getClass() != obj.getClass() ) {
return false;
}
final Pair< ? , ? > that = ( Pair< ? , ? > ) obj;
return Objects.equal( this.first , that.first ) && Objects.equal( this.first , that.first );
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( first == null ? 0 : first.hashCode() );
result = prime * result + ( second == null ? 0 : second.hashCode() );
return result;
}
@Override
public String toString() {
return "Pair [first=" + first + ", second=" + second + "]";
}
}
| 0 | 522 |
C20003 | C20066 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package jp.funnything.competition.util;
import java.util.Comparator;
import com.google.common.base.Objects;
public class Pair< T1 , T2 > {
public static < T1 extends Comparable< T1 > , T2 extends Comparable< T2 > > Comparator< Pair< T1 , T2 > > getFirstComarator() {
return new Comparator< Pair< T1 , T2 > >() {
@Override
public int compare( final Pair< T1 , T2 > o1 , final Pair< T1 , T2 > o2 ) {
final int c = o1.first.compareTo( o2.first );
return c != 0 ? c : o1.second.compareTo( o2.second );
}
};
}
public static < T1 extends Comparable< T1 > , T2 extends Comparable< T2 > > Comparator< Pair< T1 , T2 > > getSecondComarator() {
return new Comparator< Pair< T1 , T2 > >() {
@Override
public int compare( final Pair< T1 , T2 > o1 , final Pair< T1 , T2 > o2 ) {
final int c = o1.second.compareTo( o2.second );
return c != 0 ? c : o1.first.compareTo( o2.first );
}
};
}
public T1 first;
public T2 second;
public Pair( final Pair< T1 , T2 > that ) {
this.first = that.first;
this.second = that.second;
}
public Pair( final T1 first , final T2 second ) {
this.first = first;
this.second = second;
}
@Override
public boolean equals( final Object obj ) {
if ( this == obj ) {
return true;
}
if ( obj == null || getClass() != obj.getClass() ) {
return false;
}
final Pair< ? , ? > that = ( Pair< ? , ? > ) obj;
return Objects.equal( this.first , that.first ) && Objects.equal( this.first , that.first );
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( first == null ? 0 : first.hashCode() );
result = prime * result + ( second == null ? 0 : second.hashCode() );
return result;
}
@Override
public String toString() {
return "Pair [first=" + first + ", second=" + second + "]";
}
}
| 0 | 523 |
C20003 | C20041 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package jp.funnything.competition.util;
import java.util.Arrays;
import java.util.Iterator;
/**
* Do NOT change the element in iteration
*/
public class Permutation implements Iterable< int[] > , Iterator< int[] > {
public static int[] fromNumber( long value , final int n ) {
final int[] data = new int[ n ];
for ( int index = 0 ; index < n ; index++ ) {
data[ index ] = index;
}
for ( int index = 1 ; index < n ; index++ ) {
final int pos = ( int ) ( value % ( index + 1 ) );
value /= index + 1;
final int swap = data[ index ];
data[ index ] = data[ pos ];
data[ pos ] = swap;
}
return data;
}
public static long toNumber( final int[] perm ) {
final int[] data = Arrays.copyOf( perm , perm.length );
long c = 0;
for ( int index = data.length - 1 ; index > 0 ; index-- ) {
int pos = 0;
for ( int index_ = 1 ; index_ <= index ; index_++ ) {
if ( data[ index_ ] > data[ pos ] ) {
pos = index_;
}
}
final int t = data[ index ];
data[ index ] = data[ pos ];
data[ pos ] = t;
c = c * ( index + 1 ) + pos;
}
return c;
}
private final int _n;
private final int[] _data;
private final int[] _count;
int _k;
public Permutation( final int n ) {
_n = n;
_data = new int[ n ];
for ( int index = 0 ; index < n ; index++ ) {
_data[ index ] = index;
}
_count = new int[ n + 1 ];
for ( int index = 1 ; index <= n ; index++ ) {
_count[ index ] = index;
}
_k = 1;
}
@Override
public boolean hasNext() {
return _k < _n;
}
@Override
public Iterator< int[] > iterator() {
return this;
}
@Override
public int[] next() {
final int i = _k % 2 != 0 ? _count[ _k ] : 0;
final int t = _data[ _k ];
_data[ _k ] = _data[ i ];
_data[ i ] = t;
for ( _k = 1 ; _count[ _k ] == 0 ; _k++ ) {
_count[ _k ] = _k;
}
_count[ _k ]--;
return _data;
}
@Override
public void remove() {
}
}
| 0 | 524 |
C20003 | C20068 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package com.brootdev.gcj2012.common;
import java.io.*;
public class Data {
public final BufferedReader in;
public final PrintWriter out;
public Data(String inFile, String outFile) throws IOException {
in = new BufferedReader(new FileReader(inFile));
out = new PrintWriter(new BufferedWriter(new FileWriter(outFile)));
}
public int readIntLine() throws IOException {
return DataUtils.readIntLine(in);
}
public long readLongLine() throws IOException {
return DataUtils.readLongLine(in);
}
public int[] readIntsArrayLine() throws IOException {
return DataUtils.readIntsArrayLine(in);
}
public long[] readLongsArrayLine() throws IOException {
return DataUtils.readLongsArrayLine(in);
}
public void writeCaseHeader(long case_) {
DataUtils.writeCaseHeader(out, case_);
}
}
| 0 | 525 |
C20003 | C20017 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package problem.d.hall.of.mirrors;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
/**
*
* @author Park
*/
public class ProblemDHallOfMirrors {
static int fixD;
static int count = 0;
static int s_X=0;static int s_Y=0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
PrintStream out = new PrintStream(new File("/Users/Park/Desktop/output.txt"));
Scanner in = new Scanner(new File("/Users/Park/Desktop/D-small-attempt1.in"));
int testCase = Integer.parseInt(in.next());
for (int n = 0; n < testCase; n++) {
int memCount = 0;
int H = Integer.parseInt(in.next());
int W = Integer.parseInt(in.next());
int D = Integer.parseInt(in.next());
int fixH = (2 * H - 3);
int fixW = (2 * W - 3);
fixD = 2 * D;
int h0 = 0;
int w0 = 0;
search:
for (int i = 0; i < H; i++) {
String subMap = in.next();
for (int j = 0; j < W; j++) {
String X = subMap.substring(j, j + 1);
if (X.equals("X")) {
h0 = 2 * i - 1;
w0 = 2 * j - 1;
}
}
}
int d_Left = w0;
int d_Right = fixW - w0-1;
int d_Up = h0;
int d_Down = fixH - h0-1;
double[] chkM = new double[250000];
count=0;
//Right
calculate(2*d_Right,0,d_Right,d_Up,d_Left,d_Down,chkM);
memCount+=count;
count=0;
//Up
calculate(2*d_Up,0,d_Up,d_Left,d_Down,d_Right,chkM);
memCount+=count;
count=0;
//Left
calculate(2*d_Left,0,d_Left,d_Down,d_Right,d_Up,chkM);
memCount+=count;
count=0;
//Down
calculate(2*d_Down,0,d_Down,d_Right,d_Up,d_Left,chkM);
memCount+=count;
count=0;
//
//
out.println("Case #" + (n + 1) + ": " + memCount);
System.out.println("Case #" + (n + 1) + ": " + memCount);
}
in.close();
out.close();
}
public static void calculate(int s_X,int s_Y, int tempx,int dy,int dx,int tempy, double[] chkM) {
boolean chk =true;
for(int i=0;i<count;i++){
if(chkM[i]==((double)s_Y)/((double)s_X)){
chk= false;
break;
}
}
if (s_X * s_X + s_Y * s_Y > fixD * fixD) {
return;
}else if(chk){
chkM[count]=((double)s_Y)/((double)s_X);
count++;
}else{
return;
}
calculate(s_X+2 * dx,s_Y,dx,dy,tempx,tempy,chkM);
calculate(s_X,s_Y+2 * dy,tempx,tempy,dx,dy,chkM);
}
}
| 0 | 526 |
C20003 | C20042 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 527 |
C20003 | C20013 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.imageio.ImageIO;
class findRes
{
double angle;
double x,y;
double d;
int gridx, gridy;
boolean found = false;
}
public class Main {
public static int X, Y, D;
public static char[][] mat;
public static final double [] starts = new double[]{0,Math.PI/2, Math.PI, Math.PI*3/2};
public static final double EPS = CGConst.EPS;
public static double posx, posy;
public static int dblcmp(double d1, double d2)
{
if(Math.abs(d1-d2)<EPS)
return 0;
if(d1<d2)
return -1;
return 1;
}
public static List<double[]> mirrors;
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(new File("in.txt"));
FileOutputStream fos = new FileOutputStream(new File("out.txt"));
Scanner sc = new Scanner(fis);
PrintWriter out = new PrintWriter(fos);
int cases = sc.nextInt();
for(int cs = 1; cs <= cases; cs++)
{
Y = sc.nextInt();
X = sc.nextInt();
D = sc.nextInt();
mat = new char[Y][X];
sc.nextLine();
mirrors = new ArrayList<double[]>();
int cnt = 0;
for(int i=0; i<Y; i++)
{
String line = sc.nextLine();
for(int j=0; j<X; j++)
{
mat[i][j] = line.charAt(j);
if(mat[i][j] == 'X')
{
posx = 0.5+j;
posy = 0.5+i;
}
else if(mat[i][j] =='#')
{
mirrors.add(new double[]{j,i});
}
}
}
Set<Double> used = new TreeSet<Double>(new Comparator<Double>(){
@Override
public int compare(Double arg0, Double arg1) {
arg0 = adjust(arg0);
arg1 = adjust(arg1);
return dblcmp(arg0, arg1);
}
});
for(double start : starts)
{
for(double s1 = 0; s1 <=D; s1++)
for(double s2=1; s2<=D; s2++)
{
double r = Math.sqrt(s1*s1+s2*s2);
if(dblcmp(r,D)<=0)
{
double angle = start + Math.atan(s1/s2);
if(used.contains(angle))
continue;
else
used.add(angle);
List<Double> xlist = new ArrayList<Double>();
List<Double> ylist = new ArrayList<Double>();
double curx = posx, cury = posy;
double curd = D;
int gridx = (int)posx;
int gridy = (int)posy;
while(dblcmp(curd, 0)>0)
{
xlist.add(curx);
ylist.add(cury);
findRes fr=find(angle, curx, cury, curd,gridx,gridy);
if(fr.found)
{
cnt++;
// double ang=adjust(start + Math.atan(s1/s2));
// System.out.println(ang);
// for(int i=0; i<xlist.size(); i++)
// System.out.println("\t"+xlist.get(i)+" "+ylist.get(i));
// draw(xlist, ylist, 100,"img"+ang);
break;
}
angle = fr.angle;
curx = fr.x;
cury = fr.y;
curd = fr.d;
gridx = fr.gridx;
gridy = fr.gridy;
}
}
}
}
out.println("Case #"+cs+": "+cnt);
System.out.println("Case #"+cs+": "+cnt);
}
sc.close();
out.close();
}
private static void draw(List<Double> xlist, List<Double> ylist, int size,String name) {
int w = size*X, h = size*Y;
BufferedImage bi = new BufferedImage(w, h,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
for(int i=0; i<xlist.size()-1; i++)
{
g.setColor(Color.BLACK);
g.drawLine((int)(xlist.get(i)*size), (int)(ylist.get(i)*size),
(int)(xlist.get(i+1)*size), (int)(ylist.get(i+1)*size));
}
for(double[] arr:mirrors)
{
g.drawRect((int)arr[0]*size, (int)arr[1]*size, size, size);
}
try {
ImageIO.write(bi, "png", new File("C:\\"+name+".png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected static double adjust(double arg0) {
while(arg0 >= Math.PI*2)
arg0 -= Math.PI*2;
while(arg0 < 0)
arg0 += Math.PI*2;
return arg0;
}
final static double[][] sides = new double[][]{
{0,1,1,1},{1,0,1,1},{0,0,1,0},{0,0,0,1}
};
final static double [] toadd = new double[]{0,Math.PI,0,Math.PI};
final static int [][] nextSide = new int[][]{
{0,1},{1,0},{0,-1},{-1,0}
};
final static int [][] nextSideTwo = new int[13][2];
static
{
nextSideTwo[2] = new int[]{1,1};
nextSideTwo[6] = new int[]{1,-1};
nextSideTwo[12] = new int[]{-1,-1};
nextSideTwo[4] = new int[]{-1,1};
}
private static findRes find(double angle, double curx, double cury, double curd, int gridx, int gridy)
{
Point start = new Point(curx, cury);
Point end = new Point(curx + curd*Math.cos(angle), cury + curd*Math.sin(angle));
while(gridx >= 0 && gridx < X && gridy >=0 && gridy < Y)
{
if(gridx == (int)posx && gridy == (int)posy)
{
Point ori = new Point(posx, posy);
if(!start.equals(ori)&&CG.intersectSP(start, end, ori))
{
findRes fr = new findRes();
fr.found = true;
return fr;
}
}
// center?
List<Integer> sideList = new ArrayList<Integer>();
Point inter = null;
for(int i=0; i<sides.length; i++)
{
Point p1 = new Point(gridx+sides[i][0], gridy+sides[i][1]);
Point p2 = new Point(gridx+sides[i][2], gridy+sides[i][3]);
Point p = new Point();
int ind=CG.crossPointSS(p1, p2, start, end, p);
if(ind != 0 && !p.equals(start))
{
sideList.add(i);
inter = p;
}
}
if(inter == null)
curd = 0;
else
curd -= inter.minus(start).abs();
if(dblcmp(curd,0) <=0)
{
findRes fr = new findRes();
fr.d = 0;
return fr;
}
findRes fr = new findRes();
fr.x = inter.x;
fr.y = inter.y;
fr.d = curd;
fr.gridx = gridx;
fr.gridy = gridy;
if(sideList.size() == 1)
{
int nextx = gridx+nextSide[sideList.get(0)][0];
int nexty = gridy+nextSide[sideList.get(0)][1];
if(nextx>=0 && nextx < X && nexty >=0 && nexty < Y)
{
if(mat[nexty][nextx] == '#')
{
fr.angle = toadd[sideList.get(0)]-angle;
return fr;
}
else
{
gridx = nextx;
gridy = nexty;
start = inter;
}
}
else
{
fr.d = 0;
return fr;
}
}
else if(sideList.size() == 2)
{
boolean [] found = new boolean[3];
for(int i=0; i<sideList.size(); i++)
{
int nextx = gridx+nextSide[sideList.get(i)][0];
int nexty = gridy+nextSide[sideList.get(i)][1];
if(nextx>=0 && nextx < X && nexty >=0 && nexty < Y)
{
if(mat[nexty][nextx] == '#')
{
found[i] = true;
}
else
found[i] = false;
}
else
found[i] = false;
}
int nextx = gridx+nextSideTwo[(sideList.get(0)+1)*(sideList.get(1)+1)][0];
int nexty = gridy+nextSideTwo[(sideList.get(0)+1)*(sideList.get(1)+1)][1];
if(nextx>=0 && nextx < X && nexty >=0 && nexty < Y)
{
if(mat[nexty][nextx] == '#')
{
found[2] = true;
}
else
found[2] = false;
}
else
found[2] = false;
if(found[0] && found[1] && found[2])
{
fr.angle = angle+Math.PI;
return fr;
}
else if(!found[2])
{
gridx = nextx;
gridy = nexty;
start = inter;
}
else if(found[0])
{
gridx += nextSide[sideList.get(1)][0];
gridy += nextSide[sideList.get(1)][1];
fr.gridx = gridx;
fr.gridy = gridy;
fr.angle = toadd[sideList.get(0)]-angle;
return fr;
}
else if(found[1])
{
gridx += nextSide[sideList.get(0)][0];
gridy += nextSide[sideList.get(0)][1];
fr.gridx = gridx;
fr.gridy = gridy;
fr.angle = toadd[sideList.get(1)]-angle;
return fr;
}
else
{
fr.d = 0;
return fr;
}
}
else
{
System.out.println("fuck");
}
}
findRes fr = new findRes();
fr.d = 0;
return fr;
}
private static findRes find(double angle, double curx, double cury, double curd) {
final double MAX = 100;
Point start = new Point(curx, cury);
Point end = new Point(curx + curd*Math.cos(angle), cury + curd*Math.sin(angle));
if(dblcmp(curx, posx)!=0 || dblcmp(cury, posy) != 0)
{
Point ori = new Point(posx, posy);
double dis = ori.minus(start).abs();
if(CG.intersectSP(start, end, ori))
{
boolean found = false;
for(double [] arr:mirrors)
{
for(int i=0; i<sides.length; i++)
{
Point p1 = new Point(arr[0]+sides[i][0], arr[1]+sides[i][1]);
Point p2 = new Point(arr[0]+sides[i][2], arr[1]+sides[i][3]);
Point p = new Point();
int ind=CG.crossPointSS(p1, p2, start, end, p);
if(!p.equals(start)&&ind !=0 &&p.minus(start).abs()<dis)
found = true;
}
}
if(!found)
{
findRes res = new findRes();
res.found = true;
return res;
}
}
}
double mindis = Double.MAX_VALUE;
Point inter = null;
List<Integer> interSides = new ArrayList<Integer>();
List<double[]> interMirrors = new ArrayList<double[]>();
for(double [] arr:mirrors)
{
for(int i=0; i<sides.length; i++)
{
Point p1 = new Point(arr[0]+sides[i][0], arr[1]+sides[i][1]);
Point p2 = new Point(arr[0]+sides[i][2], arr[1]+sides[i][3]);
Point p = new Point();
int ind=CG.crossPointSS(p1, p2, start, end, p);
if(ind != 0 && !p.equals(start))
{
double dis = p.minus(start).abs();
if(dblcmp(dis, mindis)<0)
{
interSides.clear();
interMirrors.clear();
mindis = dis;
}
if(dblcmp(dis,mindis) == 0)
{
inter = p;
interSides.add(i);
interMirrors.add(arr);
}
}
}
}
findRes res = new findRes();
if(inter != null)
{
res.x = inter.x;
res.y = inter.y;
res.d = curd - inter.minus(start).abs();
}
if(interMirrors.size() == 0)
{
res.d=0;
}
else if(interMirrors.size()==1)
{
res.angle = toadd[interSides.get(0)] - angle;
}
else if(interMirrors.size() ==2)
{
Point maxPt = new Point(curx + MAX*Math.cos(angle), cury+MAX*Math.sin(angle));
int cnt = 0;
for(int i=0; i<sides.length; i++)
{
Point p1 = new Point(interMirrors.get(0)[0]+sides[i][0], interMirrors.get(0)[1]+sides[i][1]);
Point p2 = new Point(interMirrors.get(0)[0]+sides[i][2], interMirrors.get(0)[1]+sides[i][3]);
Point p = new Point();
int ind=CG.crossPointSS(p1, p2, start, maxPt, p);
if(ind != 0)
cnt++;
}
if(cnt == 2)
res.angle = angle;
else
res.d=0;
}
else if(interMirrors.size() == 4)
{
Set<Integer> used = new TreeSet<Integer>();
int rep = -1;
for(Integer side:interSides)
{
if(used.contains(side))
rep = side;
else
used.add(side);
}
if(used.size() == 4)
res.angle = angle;
else
{
res.angle = toadd[rep]-angle;
}
}
else if(interMirrors.size() == 6)
{
res.angle = angle+Math.PI;
}
else
{
System.out.println("fuck");
}
return res;
}
}
| 0 | 528 |
C20003 | C20000 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| import static java.lang.Math.*;
import java.io.*;
import java.util.*;
/**
* @author Chris Dziemborowicz <chris@dziemborowicz.com>
* @version 2012.0415
*/
public class HallOfMirrors
{
public static void main(String[] args)
throws Exception
{
// Get input files
File dir = new File("/Users/Chris/Documents/UniSVN/code-jam/hall-of-mirrors/data");
File[] inputFiles = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name)
{
return name.endsWith(".in");
}
});
// Process each input file
for (File inputFile : inputFiles) {
System.out.printf("Processing \"%s\"...\n", inputFile.getName());
String outputPath = inputFile.getPath().replaceAll("\\.in$", ".out");
BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath));
Scanner scanner = new Scanner(inputFile);
System.out.printf("Number of test cases: %s\n", scanner.nextLine());
int count = 0;
while (scanner.hasNext()) {
int h = scanner.nextInt();
int w = scanner.nextInt();
int d = scanner.nextInt();
scanner.nextLine();
String[] map = new String[h];
for (int i = 0; i < h; i++) {
map[i] = scanner.nextLine();
}
String output = String.format("Case #%d: %d\n", ++count, process(h, w, d, map));
System.out.print(output);
writer.write(output);
}
writer.close();
System.out.println("Done.\n");
}
// Compare to reference files (if any)
for (File inputFile : inputFiles) {
System.out.printf("Verifying \"%s\"...\n", inputFile.getName());
String referencePath = inputFile.getPath().replaceAll("\\.in$", ".ref");
String outputPath = inputFile.getPath().replaceAll("\\.in$", ".out");
File referenceFile = new File(referencePath);
if (referenceFile.exists()) {
InputStream referenceStream = new FileInputStream(referencePath);
InputStream outputStream = new FileInputStream(outputPath);
boolean matched = true;
int referenceRead, outputRead;
do {
byte[] referenceBuffer = new byte[4096];
byte[] outputBuffer = new byte[4096];
referenceRead = referenceStream.read(referenceBuffer);
outputRead = outputStream.read(outputBuffer);
matched = referenceRead == outputRead
&& Arrays.equals(referenceBuffer, outputBuffer);
} while (matched && referenceRead != -1);
if (matched) {
System.out.println("Verified.\n");
} else {
System.out.println("*** NOT VERIFIED ***\n");
}
} else {
System.out.println("No reference file found.\n");
}
}
}
public static int process(int h, int w, int d, String[] map)
{
int x = -1, y = -1;
for (int xx = 0; xx < map.length; xx++) {
int yy = map[xx].indexOf('X');
if (yy != -1) {
x = xx;
y = yy;
}
}
int count = 0;
for (int i = -100; i <= 100; i++) {
for (int j = -100; j <= 100; j++) {
int gcd = gcd(i, j);
if (gcd == 1 || (i == 0 && abs(j) == 1) || (j == 0 && abs(i) == 1)) {
count += process(map, x, y, i, j, d);
}
}
}
return count;
}
public static int process(String[] map, int sx, int sy, int dx, int dy, int d)
{
int x = sx;
int y = sy;
int xs = 0;
int ys = 0;
int err = abs(dx) - abs(dy);
while (true) {
if (err > 0) {
x += dx > 0 ? 1 : -1;
xs++;
err -= 2 * abs(dy);
if (map[x].charAt(y) == '#') {
dx = -dx;
x += dx > 0 ? 1 : -1;
}
} else if (err < 0) {
y += dy > 0 ? 1 : -1;
ys++;
err += 2 * abs(dx);
if (map[x].charAt(y) == '#') {
dy = -dy;
y += dy > 0 ? 1 : -1;
}
} else {
int ox = x;
x += dx > 0 ? 1 : -1;
xs++;
err -= 2 * abs(dy);
int oy = y;
y += dy > 0 ? 1 : -1;
ys++;
err += 2 * abs(dx);
if (map[x].charAt(y) == '#') {
if (map[ox].charAt(y) != '#' && map[x].charAt(oy) != '#') {
return 0;
} else if (map[ox].charAt(y) == '#' && map[x].charAt(oy) == '#') {
dx = -dx;
x += dx > 0 ? 1 : -1;
dy = -dy;
y += dy > 0 ? 1 : -1;
} else if (map[ox].charAt(y) == '#') {
dy = -dy;
y += dy > 0 ? 1 : -1;
} else {
dx = -dx;
x += dx > 0 ? 1 : -1;
}
}
}
if ((dx == 0 || xs % dx == 0) && (dy == 0 || ys % dy == 0)) {
if (sqrt(xs * xs + ys * ys) > d) {
return 0;
} else if (map[x].charAt(y) == 'X') {
return 1;
}
}
}
}
public static int gcd(int a, int b)
{
if (a == 0 || b == 0) {
return -1;
}
a = abs(a);
b = abs(b);
while (a != 0 && b != 0) {
if (a > b) {
a %= b;
} else {
b %= a;
}
}
return a == 0 ? b : a;
}
} | 0 | 529 |
C20003 | C20071 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package qual;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
public class D {
static class Ray{
// directions
public static byte U = 0; // up
public static byte L = 1; // left
public static byte C = 2; // left-up (corner)
// for printing
private static char[] SYMB = new char[]{'u', 'l', 'c'};
// replace l -> u, u -> l, to build symmetric ray
private static byte[] REPL = new byte[]{L, U, C};
private final int mDdeep;
private final int mDlength;
private byte[] mDirs;
private int dL = +1;
private int dU = +1;
private int mC = 0;
public Ray(int ddeep, int dlength, byte[] dirs){
mDdeep = ddeep;
mDlength = dlength;
mDirs = dirs;
}
public Ray(Ray ray){
mDdeep = ray.mDdeep;
mDlength = ray.mDlength;
mDirs = ray.mDirs;
dL = ray.dL;
dU = ray.dU;
mC = ray.mC;
}
public void revertU(){
dU *= -1;
}
public void revertL(){
dL *= -1;
}
public Ray(int ddeep, int dlength, int l, int u){
mDdeep = ddeep;
mDlength = dlength;
List<Byte> res = new ArrayList<Byte>();
int la = l * 2;
//int ua = u * 2;
int cu = 0;
for (int t = 1; t < la; t += 2){
if (t * u % l == 0 && t * u / l % 2 == 1) {
// corner move
res.add(Ray.C);
cu++;
} else {
if (t*u / l > 2*cu){
res.add(Ray.U);
cu++;
}
res.add(Ray.L);
}
}
mDirs = new byte[res.size()];
for (int i = 0; i < res.size(); i++){
mDirs[i] = res.get(i);
}
}
//produce symmetric ray
public Ray symm(){
byte[] dirs = new byte[mDirs.length];
for(int i = 0; i < mDirs.length; i++){
dirs[i] = REPL[mDirs[i]];
}
return new Ray(mDdeep, mDlength, dirs);
}
public String toString(){
StringBuilder res = new StringBuilder();
for (int i = 0; i < mDirs.length; i++){
res.append(SYMB[mDirs[i]]);
}
return res.toString();
}
public boolean trace(int x, int y, char[][] field){
int n = 0;
while (true){
// still parsing
if (mC < mDirs.length){
byte dir = mDirs[mC];
int nx = dir == U ? x : x + dL;
int ny = dir == L ? y : y + dU;
if (field[ny][nx] == '#'){
//mirror processing
if (dir == L){
revertL();
} else if (dir == U){
revertU();
} else {
// corner processing
char lc = field[y][nx];
char uc = field[ny][x];
if (lc == '#' && uc == '#'){
revertU();
revertL();
} else if (lc == '#'){
y = ny;
revertL();
} else if (uc == '#'){
x = nx;
revertU();
} else {
// destroy ray
return false;
}
}
} else {
x = nx;
y = ny;
}
mC++;
} else {
// we are reach ray's end
// check if we hit X
n++;
if (field[y][x] == 'X' && n*n*mDlength <= mDdeep) return true;
if (n*n*mDlength < mDdeep){
// can continue ray
mC = 0;
} else {
// can not reach X
return false;
}
}
}
}
}
public static String printField(char[][] data){
StringBuilder s = new StringBuilder();
for (int i = 0; i < data.length; i++){
for (int j = 0; j < data[i].length; j++){
s.append(data[i][j]);
}
s.append("\n");
}
return s.toString();
}
public static List<Ray> createAll(int deep){
List<Ray> rays = new ArrayList<Ray>();
int ddeep = deep * deep;
boolean [][] points = new boolean[deep + 1][deep];
for (int l = 1; l <= deep; l++){
for (int u = 0; u < l; u++){
// check point not covered already
if (points[l][u]) continue;
// check length
int dlength = l*l + u*u;
if (dlength > ddeep) continue;
//cover points by this ray
int t = 1;
while (l*t < deep + 1 && u*t < deep){
points[l*t][u*t] = true;
t++;
}
// build ray and it's symmetric ray
Ray ray = new Ray(ddeep, dlength, l, u);
rays.add(ray);
rays.add(ray.symm());
}
}
rays.add(new Ray(ddeep, 2, new byte[]{Ray.C}));
int t = rays.size();
for (int i = 0; i < t; i++){
Ray r = rays.get(i);
if (r.mDirs.length > 1 || r.mDirs[0] == Ray.C){
Ray ar = new Ray(r);
ar.revertL();
ar.revertU();
rays.add(ar);
}
if (r.mDirs.length > 1 || r.mDirs[0] != Ray.U){
Ray ar = new Ray(r);
ar.revertL();
rays.add(ar);
}
if (r.mDirs.length > 1 || r.mDirs[0] != Ray.L){
Ray ar = new Ray(r);
ar.revertU();
rays.add(ar);
}
}
return rays;
}
public static void main(String[] args) throws Exception {
/*List<Ray> rays = createAll(7);
for (Ray r : rays){
System.out.println(r);
}*/
String in_file = "q/d/l_in.txt";
String out_file = in_file.replace("_in.txt", "_out.txt");
BufferedReader in = new BufferedReader(new FileReader(in_file));
BufferedWriter out = new BufferedWriter(new FileWriter(out_file));
int n = Integer.parseInt(in.readLine());
for (int i = 1; i <= n; i++){
String[] s = in.readLine().split(" ");
int h = Integer.parseInt(s[0]);
int w = Integer.parseInt(s[1]);
int d = Integer.parseInt(s[2]);
int x = -1, y = -1;
char[][] field = new char[h][w];
for (int j = 0; j < h; j++){
String fline = in.readLine();
for (int k = 0; k < w; k++){
field[j][k] = fline.charAt(k);
if (field[j][k] == 'X'){
x = k;
y = j;
}
}
}
List<Ray> rays = createAll(d);
int count = 0;
for (Ray r : rays){
// trace ray
if (r.trace(x, y, field)) {
//System.out.println(r);
count++;
}
}
out.write("Case #" + i + ": " + count + "\n");
}
in.close();
out.close();
}
}
| 0 | 530 |
C20003 | C20010 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
public class codejamp4 {
public static char[] map;
public static void main(String[] args) throws IOException{
readFile();
}
public static int getTriple(int c, int a){
return (int)Math.floor(Math.sqrt(Math.pow(c, 2)-Math.pow(a, 2)));
}
public static int calcGCF(int a, int b) {
int s;
if (a > b)
s = b;
else
s = a;
for (int i = s; i > 0; i--) {
if ((a%i == 0) && (b%i == 0))
return i;
}
return -1;
}
public static int mapSearch(char[][] map, int height, int width, int distance, int horizontal, int vertical,
int posX, int posY,int dx,int dy){
int x = 0;
int y = 0;
while(Math.pow(x, 2)+Math.pow(y, 2)<Math.pow(distance, 2)){
if((x+0.5)*vertical<(y+0.5)*horizontal){
posX+=dx;
x+=1;
if(map[posX][posY]=='#'){
dx=-dx;
posX+=dx;
}
}
else if((x+0.5)*vertical>(y+0.5)*horizontal){
posY+=dy;
y+=1;
if(map[posX][posY]=='#'){
dy=-dy;
posY+=dy;
}
}
else{
x+=1;
y+=1;
posX+=dx;
posY+=dy;
if(map[posX][posY]=='#'){
if(map[posX-dx][posY]=='#' && map[posX][posY-dy]=='#'){
dx=-dx;
dy=-dy;
posX+=dx;
posY+=dy;
}
else if(map[posX-dx][posY]=='#'){
dy=-dy;
posY+=dy;
}
else if(map[posX][posY-dy]=='#'){
dx=-dx;
posX+=dx;
}
else{
return 0;
}
}
}
if(map[posX][posY]=='X' && y*horizontal==x*vertical &&
Math.pow(x,2)+Math.pow(y,2)<=Math.pow(distance,2)){
return 1;
}
}
return 0;
}
public static int lineSearch(char[][] map, int height, int width, int distance,
int posx, int posy, int dx, int dy){
if(posx<0 || posy<0)
return 0;
for(int i=1; i<=distance; i++){
posx+=dx;
posy+=dy;
if(map[posx][posy]=='#'){
dx=-dx;
dy=-dy;
posx+=dx;
posy+=dy;
}
if(map[posx][posy]=='X'){
return 1;
}
}
return 0;
}
public static int getResult(char[][] map, int height, int width, int distance){
int count=0;
int posx=-1;
int posy=-1;
for(int i = 0;i<height;i++){
if(posx!=-1)
break;
for(int j = 0; j<width; j++){
if(map[i][j]=='X'){
posx=i;
posy=j;
break;
}
}
}
ArrayList<ShadowPoint> exist=new ArrayList<ShadowPoint>();
for(int horizontal = 1; horizontal<distance;horizontal++){
for(int vertical = 1; vertical<=getTriple(distance,horizontal);vertical++){
int g=calcGCF(horizontal, vertical);
ShadowPoint current = new ShadowPoint(horizontal/g,vertical/g);
if(exist.contains(current))
continue;
exist.add(current);
count+=mapSearch(map, height, width, distance, horizontal, vertical, posx, posy, 1, 1);
count+=mapSearch(map, height, width, distance, horizontal, vertical, posx, posy, 1, -1);
count+=mapSearch(map, height, width, distance, horizontal, vertical, posx, posy, -1, 1);
count+=mapSearch(map, height, width, distance, horizontal, vertical, posx, posy, -1, -1);
}
}
count+=lineSearch(map, height, width, distance, posx, posy, 1, 0);
count+=lineSearch(map, height, width, distance, posx, posy, 0, 1);
count+=lineSearch(map, height, width, distance, posx, posy, -1, 0);
count+=lineSearch(map, height, width, distance, posx, posy, 0, -1);
return count;
}
public static void readFile() throws IOException{
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
FileReader input = new FileReader("in.txt");
BufferedReader bufRead = new BufferedReader(input);
String line = bufRead.readLine();
int lineCount = 1;
while (line != null){
String[] splitResult = line.split(" ");
int height = Integer.parseInt(splitResult[0]);
int width = Integer.parseInt(splitResult[1]);
int distance = Integer.parseInt(splitResult[2]);
char[][] map = new char[height][width];
for(int i=0;i<height;i++){
String currentLine = bufRead.readLine();
for(int j=0; j<width;j++){
map[i][j] = currentLine.charAt(j);
}
}
out.write("Case #" + lineCount + ": " + getResult(map,height,width,distance));
line = bufRead.readLine();
if(line!=null)
out.write('\n');
lineCount++;
}
bufRead.close();
out.close();
}
}
| 0 | 531 |
C20003 | C20070 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package com.brootdev.gcj2012.problemD;
import com.brootdev.gcj2012.common.Data;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
private static final Logger logger = Logger.getLogger(Main.class.getSimpleName());
private static final double PI2 = Math.PI * 2;
private static final double maxError = .001;
private Data data;
private long casesNumber;
private long currentCase;
private int H;
private int W;
private int D;
private double errPerDist;
private int raysNumber;
private double currErr;
private double userX;
private double userY;
private int userTileX;
private int userTileY;
private TileType[][] tiles;
private double velX;
private double velY;
private int tileX;
private int tileY;
private double localX;
private double localY;
private double dist;
private double newLocalX;
private double newLocalY;
private double distDelta;
private LocalMoveStatus localMoveStatus;
private int currentRay;
private long userHits;
private boolean userHitLast;
private boolean firstRayHit;
static {
logger.setLevel(Level.FINEST);
}
public static void main(String[] args) throws IOException {
new Main().go(args[0], args[1]);
}
public void go(String inFile, String outFile) throws IOException {
data = new Data(inFile, outFile);
casesNumber = data.readLongLine();
for (currentCase = 0; currentCase < casesNumber; currentCase++) {
data.writeCaseHeader(currentCase);
processCase();
}
data.out.flush();
}
private void processCase() throws IOException {
readMap();
castRays();
}
private void readMap() throws IOException {
int[] ints = data.readIntsArrayLine();
H = ints[0];
W = ints[1];
D = ints[2];
tiles = new TileType[H][W];
errPerDist = maxError / D;
raysNumber = (int) (2 * Math.PI / Math.asin(errPerDist));
logger.info(String.format("case=%d/%d, raysNumber=%d", currentCase, casesNumber, raysNumber));
for (int y = 0; y < H; y++) {
char[] row = data.in.readLine().toCharArray();
for (int x = 0; x < W; x++) {
char c = row[x];
switch (c) {
case 'X':
tiles[y][x] = TileType.USER;
userTileX = x;
userTileY = y;
userX = x + .5;
userY = y + .5;
break;
case '#':
tiles[y][x] = TileType.MIRROR;
break;
case '.':
tiles[y][x] = TileType.EMPTY;
break;
default:
throw new RuntimeException("Invalid tile: " + c);
}
}
}
}
private void castRays() {
userHits = 0;
userHitLast = false;
firstRayHit = false;
for (currentRay = 0; currentRay < raysNumber; currentRay++) {
double angle = Math.PI * 2 * currentRay / raysNumber;
velX = Math.sin(angle);
velY = Math.cos(angle);
tileX = userTileX;
tileY = userTileY;
localX = userX % 1;
localY = userY % 1;
dist = 0;
// logger.info(String.format("Casting ray %d/%d: velX=%f, velY=%f", ray, raysNumber, velX, velY));
while (true) {
currErr = (dist + 1.5) * errPerDist;
if (checkUserHit()) {
break;
}
performLocalMove();
dist += distDelta;
if (dist > D || performTileMove()) {
userHitLast = false;
break;
}
// logger.info(String.format("posX=%f, posY=%f, velX=%f, velY=%f, dist=%f",
// localX + tileX, localY + tileY, velX, velY, dist));
}
}
if (firstRayHit && userHitLast) {
userHits--;
}
data.out.println(userHits);
}
private void performLocalMove() {
if (velX > 0) {
distDelta = (1 - localX) / velX;
newLocalY = localY + velY * distDelta;
if (newLocalY >= 0 && newLocalY <= 1) {
newLocalX = 1;
if (velY > 0 && newLocalY >= 1 - currErr) {
newLocalY = 1;
localMoveStatus = LocalMoveStatus.BOTTOMRIGHT;
} else if (velY < 0 && newLocalY <= currErr) {
newLocalY = 0;
localMoveStatus = LocalMoveStatus.TOPRIGHT;
} else {
localMoveStatus = LocalMoveStatus.RIGHT;
}
return;
}
}
if (velX < 0) {
distDelta = - localX / velX;
newLocalY = localY + velY * distDelta;
if (newLocalY >= 0 && newLocalY <= 1) {
newLocalX = 0;
if (velY > 0 && newLocalY >= 1 - currErr) {
newLocalY = 1;
localMoveStatus = LocalMoveStatus.BOTTOMLEFT;
} else if (velY < 0 && newLocalY <= currErr) {
newLocalY = 0;
localMoveStatus = LocalMoveStatus.TOPLEFT;
} else {
localMoveStatus = LocalMoveStatus.LEFT;
}
return;
}
}
if (velY > 0) {
distDelta = (1 - localY) / velY;
newLocalY = 1;
newLocalX = localX + velX * distDelta;
if (velX > 0 && newLocalX >= 1 - currErr) {
newLocalX = 1;
localMoveStatus = LocalMoveStatus.BOTTOMRIGHT;
} else if (velX < 0 && newLocalX <= currErr) {
newLocalX = 0;
localMoveStatus = LocalMoveStatus.BOTTOMLEFT;
} else {
localMoveStatus = LocalMoveStatus.BOTTOM;
}
return;
}
if (velY < 0) {
distDelta = - localY / velY;
newLocalY = 0;
newLocalX = localX + velX * distDelta;
if (velX > 0 && newLocalX >= 1 - currErr) {
newLocalX = 1;
localMoveStatus = LocalMoveStatus.TOPRIGHT;
} else if (velX < 0 && newLocalX <= currErr) {
newLocalX = 0;
localMoveStatus = LocalMoveStatus.TOPLEFT;
} else {
localMoveStatus = LocalMoveStatus.TOP;
}
return;
}
}
private boolean checkUserHit() {
if (dist == 0 || tileX != userTileX || tileY != userTileY) {
return false;
}
final double avalDist = D - dist + currErr;
if (Math.abs(velX) <= currErr) {
if (avalDist >= .5 && Math.abs(.5 - localX) <= currErr) {
doUserHit();
return true;
}
return false;
}
if (Math.abs(velY) <= currErr) {
if (avalDist >= .5 && Math.abs(.5 - localY) <= currErr) {
doUserHit();
return true;
}
return false;
}
if (avalDist < Math.sqrt(Math.pow(localX - .5, 2) + Math.pow(localY - .5, 2))) {
return false;
}
final double a = velY / velX;
final double b = localY - a * localX;
if (Math.abs(a * .5 + b - .5) <= currErr || Math.abs((.5 - b) / a - .5) <= currErr) {
doUserHit();
return true;
}
// final double w = velY / velX;
// final double A = 1.0 / (w * localY - localX);
// final double B = - w * A;
// final double d = Math.abs((A + B) * .5 + 1) / Math.sqrt(A * A + B * B);
// if (d <= sensDist) {
// doUserHit();
// return true;
// }
return false;
}
private void doUserHit() {
if (! userHitLast) {
userHits++;
if (currentRay == 0) {
firstRayHit = true;
}
// logger.info(String.format("Hit! case=%d, ray=%d", currentCase, currentRay));
}
userHitLast = true;
}
private boolean performTileMove() {
localX = newLocalX;
localY = newLocalY;
switch (localMoveStatus) {
case TOPLEFT:
if (tiles[tileY - 1][tileX - 1] != TileType.MIRROR) {
tileX--;
tileY--;
localX = 1;
localY = 1;
return false;
} else if (tiles[tileY - 1][tileX] != TileType.MIRROR && tiles[tileY][tileX - 1] != TileType.MIRROR) {
return true;
}
break;
case TOPRIGHT:
if (tiles[tileY - 1][tileX + 1] != TileType.MIRROR) {
tileX++;
tileY--;
localX = 0;
localY = 1;
return false;
} else if (tiles[tileY - 1][tileX] != TileType.MIRROR && tiles[tileY][tileX + 1] != TileType.MIRROR) {
return true;
}
break;
case BOTTOMLEFT:
if (tiles[tileY + 1][tileX - 1] != TileType.MIRROR) {
tileX--;
tileY++;
localX = 1;
localY = 0;
return false;
} else if (tiles[tileY + 1][tileX] != TileType.MIRROR && tiles[tileY][tileX - 1] != TileType.MIRROR) {
return true;
}
break;
case BOTTOMRIGHT:
if (tiles[tileY + 1][tileX + 1] != TileType.MIRROR) {
tileX++;
tileY++;
localX = 0;
localY = 0;
return false;
} else if (tiles[tileY + 1][tileX] != TileType.MIRROR && tiles[tileY][tileX + 1] != TileType.MIRROR) {
return true;
}
break;
}
if (localMoveStatus.isTop) {
if (tiles[tileY - 1][tileX] == TileType.MIRROR) {
velY = -velY;
} else {
tileY--;
localY = 1;
}
}
if (localMoveStatus.isBottom) {
if (tiles[tileY + 1][tileX] == TileType.MIRROR) {
velY = -velY;
} else {
tileY++;
localY = 0;
}
}
if (localMoveStatus.isLeft) {
if (tiles[tileY][tileX - 1] == TileType.MIRROR) {
velX = -velX;
} else {
tileX--;
localX = 1;
}
}
if (localMoveStatus.isRight) {
if (tiles[tileY][tileX + 1] == TileType.MIRROR) {
velX = -velX;
} else {
tileX++;
localX = 0;
}
}
return false;
}
private enum TileType {
EMPTY, MIRROR, USER
}
private enum LocalMoveStatus {
TOP(true, false, false, false),
BOTTOM(false, true, false, false),
LEFT(false, false, true, false),
RIGHT(false, false, false, true),
TOPLEFT(true, false, true, false),
TOPRIGHT(true, false, false, true),
BOTTOMLEFT(false, true, true, false),
BOTTOMRIGHT(false, true, false, true);
public final boolean isTop;
public final boolean isBottom;
public final boolean isLeft;
public final boolean isRight;
private LocalMoveStatus(boolean top, boolean bottom, boolean left, boolean right) {
isTop = top;
isBottom = bottom;
isLeft = left;
isRight = right;
}
}
}
| 0 | 532 |
C20003 | C20055 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package jp.funnything.competition.util;
import java.util.Arrays;
import java.util.Iterator;
/**
* Do NOT change the element in iteration
*/
public class Permutation implements Iterable< int[] > , Iterator< int[] > {
public static int[] fromNumber( long value , final int n ) {
final int[] data = new int[ n ];
for ( int index = 0 ; index < n ; index++ ) {
data[ index ] = index;
}
for ( int index = 1 ; index < n ; index++ ) {
final int pos = ( int ) ( value % ( index + 1 ) );
value /= index + 1;
final int swap = data[ index ];
data[ index ] = data[ pos ];
data[ pos ] = swap;
}
return data;
}
public static long toNumber( final int[] perm ) {
final int[] data = Arrays.copyOf( perm , perm.length );
long c = 0;
for ( int index = data.length - 1 ; index > 0 ; index-- ) {
int pos = 0;
for ( int index_ = 1 ; index_ <= index ; index_++ ) {
if ( data[ index_ ] > data[ pos ] ) {
pos = index_;
}
}
final int t = data[ index ];
data[ index ] = data[ pos ];
data[ pos ] = t;
c = c * ( index + 1 ) + pos;
}
return c;
}
private final int _n;
private final int[] _data;
private final int[] _count;
int _k;
public Permutation( final int n ) {
_n = n;
_data = new int[ n ];
for ( int index = 0 ; index < n ; index++ ) {
_data[ index ] = index;
}
_count = new int[ n + 1 ];
for ( int index = 1 ; index <= n ; index++ ) {
_count[ index ] = index;
}
_k = 1;
}
@Override
public boolean hasNext() {
return _k < _n;
}
@Override
public Iterator< int[] > iterator() {
return this;
}
@Override
public int[] next() {
final int i = _k % 2 != 0 ? _count[ _k ] : 0;
final int t = _data[ _k ];
_data[ _k ] = _data[ i ];
_data[ i ] = t;
for ( _k = 1 ; _count[ _k ] == 0 ; _k++ ) {
_count[ _k ] = _k;
}
_count[ _k ]--;
return _data;
}
@Override
public void remove() {
}
}
| 0 | 533 |
C20003 | C20077 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| import java.awt.Point;
import java.util.Scanner;
public class Mirrors {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int cases = scan.nextInt();
for (int trial = 1; trial <= cases; trial++) {
System.out.print("Case #" + trial + ": ");
int height = scan.nextInt() * 2;
int width = scan.nextInt() * 2;
int d = scan.nextInt() * 2;
boolean[][] map = new boolean[height][width];
scan.nextLine();
Point myPos = null;
for (int row = 0; row < height; row += 2) {
char[] line = scan.nextLine().toCharArray();
for (int c = 0; c < line.length; c++) {
int col = c * 2;
map[row][col] = map[row + 1][col] = map[row][col + 1] = map[row + 1][col + 1] = (line[c] == '#');
if (line[c] == 'X') {
myPos = new Point(col + 1, row + 1);
}
}
}
int count = 0;
for (int dr = -d; dr <= d; dr++) {
for (int dc = -d; dc <= d; dc++) {
if (dr * dr + dc * dc >= d * d || !relPrime(dr, dc))
continue;
FracVec init = new FracVec(new Frac(dr), new Frac(dc));
FracVec dm = init;
FracVec next = dm.add(init);
while (next.compareLengthTo(d) <= 0) {
dm = next;
next = dm.add(init);
}
Frac t = Frac.ZERO;
FracVec pos = new FracVec(myPos);
do {
Frac nextRow = pos.row.roundAddSig(dm.row);
Frac nextCol = pos.col.roundAddSig(dm.col);
Frac addRowT = intersectRowTAdd(pos, dm, nextRow);
Frac addColT = intersectColTAdd(pos, dm, nextCol);
Frac addT;
if (dm.row.isZero())
addT = addColT;
else if (dm.col.isZero())
addT = addRowT;
else {
if (addRowT.compareTo(addColT) < 0)
addT = addRowT;
else
addT = addColT;
}
t = t.add(addT);
pos = pos.add(dm.multiply(addT));
if (pos.equals(myPos)) {
count++;
break;
}
if (pos.isCorner()) {
Point farCorn = getMidCorn(pos, dm);
boolean fc = map[farCorn.y][farCorn.x];
Point vertCorn = getVertCorn(pos, dm);
boolean vc = map[vertCorn.y][vertCorn.x];
Point horizCorn = getHorizCorn(pos, dm);
boolean hc = map[horizCorn.y][horizCorn.x];
if (fc) {
boolean found = false;
if (vc) {
dm = dm.flipHoriz();
found = true;
}
if (hc) {
dm = dm.flipVert();
found = true;
}
if (!found)
break;
}
} else {
Point cell = pos.getNextCell(dm);
boolean isWall = map[cell.y][cell.x];
if (isWall) {
if (pos.row.denom == 1) {
assert pos.col.denom > 1;
dm = dm.flipVert();
} else if (pos.col.denom == 1) {
assert pos.row.denom > 1;
dm = dm.flipHoriz();
} else
assert false;
}
}
} while (t.compareTo(Frac.ONE) < 0);
}
}
System.out.println(count);
}
}
private static Point getMidCorn(FracVec pos, FracVec dm) {
return pos.getNextCell(dm);
}
private static Point getVertCorn(FracVec pos, FracVec dm) {
return pos.getNextCell(dm.flipVert());
}
private static Point getHorizCorn(FracVec pos, FracVec dm) {
return pos.getNextCell(dm.flipHoriz());
}
private static boolean relPrime(int dr, int dc) {
int gcd = Frac.gcd(dr, dc);
return Math.abs(gcd) == 1;
}
private static Frac intersectColTAdd(FracVec pos, FracVec dm, Frac nextCol) {
if (nextCol == null)
return null;
else
return nextCol.sub(pos.col).div(dm.col);
}
private static Frac intersectRowTAdd(FracVec pos, FracVec dm, Frac nextRow) {
if (nextRow == null)
return null;
else
return nextRow.sub(pos.row).div(dm.row);
}
}
| 0 | 534 |
C20003 | C20024 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package pl.helman.codejam.Hall;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Hall {
private static int checkSpecialCases(boolean[][] map, int startX,
int startY, int d) {
int ret = 0;
// horizontal to left
int x = startX, y = startY;
// traveled distance
int a = 0;
while (!map[x][y]) {
x--;
a++;
}
if (2 * a <= d) {
ret++;
}
x = startX;
y = startY;
// traveled distance
a = 0;
while (!map[x + 1][y]) {
x++;
a++;
}
if (2 * a <= d) {
ret++;
}
x = startX;
y = startY;
// traveled distance
a = 0;
while (!map[x][y]) {
y--;
a++;
}
if (2 * a <= d) {
ret++;
}
x = startX;
y = startY;
// traveled distance
a = 0;
while (!map[x][y + 1]) {
y++;
a++;
}
if (2 * a <= d) {
ret++;
}
return ret;
}
private static boolean checkRay(boolean[][] map, int startX, int startY,
int d, int dirX, int dirY) {
int den = Math.abs(dirX * dirY);
int lx = startX * den;
int ly = startY * den;
int t = 0;
while (true) {
// seeking for next lines distance
int lineDistX = dirX < 0 ? (lx % den) : (den - lx % den);
if (lineDistX == 0) {
lineDistX = den;
}
lineDistX = Math.abs(lineDistX / dirX);
int lineDistY = dirY < 0 ? (ly % den) : (den - ly % den);
if (lineDistY == 0) {
lineDistY = den;
}
lineDistY = Math.abs(lineDistY / dirY);
// moving to closest line
if (lineDistX < lineDistY) {
lx += lineDistX * dirX;
ly += lineDistX * dirY;
} else {
lx += lineDistY * dirX;
ly += lineDistY * dirY;
}
if (lx % den == 0 && ly % den == 0) {
// full vector repetition
// increase traveled distance
t++;
if (t * t * (dirX * dirX + dirY * dirY) > d * d) {
return false;
}
// check if it's not starting point
if (lx == startX * den && ly == startY * den) {
return true;
}
// corners
int x = lx / den;
int y = ly / den;
boolean wall = false;
// vertical wall
if ((map[x][y] && map[x][y + 1])
|| (map[x + 1][y] && map[x + 1][y + 1])) {
dirX = -dirX;
wall = true;
}
// horizontal wall
if ((map[x][y] && map[x + 1][y])
|| (map[x][y + 1] && map[x + 1][y + 1])) {
dirY = -dirY;
wall = true;
}
if (!wall && map[dirX > 0 ? x + 1 : x][dirY > 0 ? y + 1 : y]) {
// dead corner
return false;
}
} else {
// possible mirror
if (lx % den == 0
&& map[dirX < 0 ? (lx / den) : ((lx / den) + 1)][(ly / den) + 1]) {
// vertical wall
dirX = -dirX;
} else if (ly % den == 0
&& map[(lx / den) + 1][dirY < 0 ? (ly / den)
: ((ly / den) + 1)]) {
// horizontal wall
dirY = -dirY;
}
}
}
}
public static int egcd(int a, int b) {
if (a == 0)
return b;
while (b != 0) {
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("d:\\hall.in");
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
FileWriter f0 = new FileWriter("d:\\hall.out");
int t = Integer.parseInt(s.trim());
for (int i = 1; i <= t; i++) {
s = br.readLine();
String[] elems = s.split(" ");
int h = Integer.parseInt(elems[0]);
int w = Integer.parseInt(elems[1]);
int d = Integer.parseInt(elems[2]) * 2;
int startX = -1;
int startY = -1;
int ret = 0;
boolean[][] map = new boolean[2 * w][2 * h];
// loading map
for (int y = 0; y < h; y++) {
s = br.readLine();
for (int x = 0; x < w; x++) {
if (s.charAt(x) == '#') {
map[2 * x][2 * y] = true;
map[2 * x + 1][2 * y] = true;
map[2 * x][2 * y + 1] = true;
map[2 * x + 1][2 * y + 1] = true;
} else {
map[2 * x][2 * y] = false;
map[2 * x + 1][2 * y] = false;
map[2 * x][2 * y + 1] = false;
map[2 * x + 1][2 * y + 1] = false;
}
if (s.charAt(x) == 'X') {
startX = 2 * x;
startY = 2 * y;
}
}
}
// special cases
ret = checkSpecialCases(map, startX, startY, d);
// probable light directions
for (int dirX = -d; dirX <= d; dirX++) {
if (dirX == 0) {
// special case
continue;
}
for (int dirY = -d; dirY <= d; dirY++) {
if (dirY == 0) {
// special case
continue;
}
if (dirX * dirX + dirY * dirY > d * d) {
// too long
continue;
}
if (egcd(Math.abs(dirX), Math.abs(dirY)) != 1) {
// there is shorter version
continue;
}
if (checkRay(map, startX, startY, d, dirX, dirY)) {
//System.out.println(" dirX:" + dirX + " dirY:" + dirY);
ret++;
}
}
}
System.out.println("Case #" + i + ": " + ret);
// System.out.println();
f0.write("Case #" + i + ": " + ret + "\r\n");
}
fr.close();
f0.close();
}
}
| 0 | 535 |
C20003 | C20001 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| import java.io.*;
import java.util.*;
class Mirrors {
static class Direction {
int x;
int y;
Direction(int a, int b){
x = a;
y = b;
}
}
static class Point {
double x;
double y;
Point(double a, double b){
x = a;
y = b;
}
}
static class Cell {
int x;
int y;
Cell(int a, int b){
x = a;
y = b;
}
}
public static void main(String [] args) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(in.readLine());
for(int i = 0; i < T; i++){
int ans = 0;
StringTokenizer st = new StringTokenizer(in.readLine());
int H = Integer.parseInt(st.nextToken());
int W = Integer.parseInt(st.nextToken());
int D = Integer.parseInt(st.nextToken());
char[][] map = new char[H][W];
Cell startcel = null;
for(int j = 0; j < H; j++){
String s = in.readLine();
for(int k = 0; k < s.length(); k++){
map[j][k] = s.charAt(k);
if(s.charAt(k)=='X'){
startcel = new Cell(j,k);
map[j][k]='.';
}
}
}
//list of diagonal directions
//hit corner at end
ArrayList<Direction> directions = new ArrayList<Direction>();
for(int j = 0; j <= D/2; j++){
for(int k = 0; k <= D/2; k++){
if(Math.sqrt((0.5+j)*(0.5+j)+(0.5+k)*(0.5+k))<=D/2+0.0001){
int x = 1+2*j;
int y = 1+2*k;
boolean exists = false;
for(int l = 0; l < directions.size(); l++){
int x2 = directions.get(l).x;
int y2 = directions.get(l).y;
if((double)x/y<=(double)x2/y2 + 0.0001 &&
(double)x/y>=(double)x2/y2 - 0.0001){
exists = true;
}
}
if(!exists){
directions.add(new Direction(x,y));
directions.add(new Direction(x,-y));
directions.add(new Direction(-x,y));
directions.add(new Direction(-x,-y));
}
}
}
}
//diagonal direction doesn't hit corner
for(int j = 1; j <= D; j++){
for(int k = 1; k <= D; k++){
if(Math.sqrt(j*j+k*k)<=D+0.0001){
int x = j;
int y = k;
boolean exists = false;
for(int l = 0; l < directions.size(); l++){
int x2 = directions.get(l).x;
int y2 = directions.get(l).y;
if((double)x/y<=(double)x2/y2 + 0.0001 &&
(double)x/y>=(double)x2/y2 - 0.0001){
exists = true;
}
}
if(!exists){
directions.add(new Direction(x,y));
directions.add(new Direction(x,-y));
directions.add(new Direction(-x,y));
directions.add(new Direction(-x,-y));
}
}
}
}
//straight directions
directions.add(new Direction(0,1));
directions.add(new Direction(0,-1));
directions.add(new Direction(1,0));
directions.add(new Direction(-1,0));
/*
for(int j = 0; j < directions.size(); j++){
System.out.println(directions.get(j).x + " " + directions.get(j).y);
}
*/
//for each direction see if it hits X again
for(Direction d : directions){
Point p = new Point(0.5,0.5);
double traveled = 0;
Direction dir = new Direction(d.x,d.y);
Cell cell = new Cell(startcel.x,startcel.y);
while (traveled<=D){
//assume next cell is vertical from this one
double ydir = 1 - p.y;
if(dir.y<0) ydir = -p.y;
if(dir.y==0) ydir = 0;
double xdir = (ydir==0) ? ((dir.x>0) ? 1 : -1 ): dir.x*ydir/dir.y;
//System.out.println(traveled + " (" + cell.x + ", " + cell.y + ") " + xdir + " " + ydir + " " + p.x + " " + p.y);
//check termination
if(cell.x==startcel.x&&cell.y==startcel.y&&(p.x!=0.5||p.y!=0.5)){
double x1 = 0.5;
double y1 = 0.5;
double x2 = p.x;
double y2 = p.y;
double x3 = p.x + xdir;
double y3 = p.y + ydir;
double area = x1*(y2-y3) + x2*(y3 - y1) + x3*(y1 - y2);
if(area < 0.0001 && area > -0.0001){ //colinear points
traveled += Math.sqrt((p.x-0.5)*(p.x-0.5) + (p.y-0.5)*(p.y-0.5));
p.x = 0.5;
p.y = 0.5;
break;
}
}
//corner case
if((p.x+xdir>0.9999&&p.x+xdir<1.0001)&&
(p.y+ydir>0.9999&&p.y+ydir<1.0001)){ //bottom right
traveled += Math.sqrt(xdir*xdir + ydir*ydir);
if(map[cell.x+1][cell.y+1]=='.'){
cell.x++;
cell.y++;
p.x = 0;
p.y = 0;
} else if(map[cell.x][cell.y+1]=='.'&&
map[cell.x+1][cell.y]=='.'){
traveled = 99999;
break;
} else if(map[cell.x][cell.y+1]=='#'&&
map[cell.x+1][cell.y]=='.'){
dir.x = -dir.x;
cell.x++;
p.x = 1;
p.y = 0;
} else if(map[cell.x][cell.y+1]=='.'&&
map[cell.x+1][cell.y]=='#'){
dir.y = -dir.y;
cell.y++;
p.x = 0;
p.y = 1;
} else if(map[cell.x][cell.y+1]=='#'&&
map[cell.x+1][cell.y]=='#'){
dir.y = -dir.y;
dir.x = -dir.x;
p.x = 1;
p.y = 1;
} else {
System.err.println("error br");
System.exit(-1);
}
} else if((p.x+xdir>-0.0001&&p.x+xdir<0.0001)&&
(p.y+ydir>0.9999&&p.y+ydir<1.0001)){ //bottom left
traveled += Math.sqrt(xdir*xdir + ydir*ydir);
if(map[cell.x+1][cell.y-1]=='.'){
cell.y--;
cell.x++;
p.x = 1;
p.y = 0;
} else if(map[cell.x][cell.y-1]=='.'&&
map[cell.x+1][cell.y]=='.'){
traveled = 99999;
break;
} else if(map[cell.x][cell.y-1]=='#'&&
map[cell.x+1][cell.y]=='.'){
dir.x = -dir.x;
cell.x++;
p.x = 0;
p.y = 0;
} else if(map[cell.x][cell.y-1]=='.'&&
map[cell.x+1][cell.y]=='#'){
dir.y = -dir.y;
cell.y--;
p.x = 1;
p.y = 1;
} else if(map[cell.x][cell.y-1]=='#'&&
map[cell.x+1][cell.y]=='#'){
dir.y = -dir.y;
dir.x = -dir.x;
p.x = 0;
p.y = 1;
} else {
System.err.println("error bl");
System.exit(-1);
}
} else if((p.x+xdir>-0.0001&&p.x+xdir<0.0001)&&
(p.y+ydir>-0.0001&&p.y+ydir<0.0001)){ //top left
traveled += Math.sqrt(xdir*xdir + ydir*ydir);
if(map[cell.x-1][cell.y-1]=='.'){
cell.y--;
cell.x--;
p.x = 1;
p.y = 1;
} else if(map[cell.x-1][cell.y]=='.'&&
map[cell.x][cell.y-1]=='.'){
traveled = 99999;
break;
} else if(map[cell.x-1][cell.y]=='.'&&
map[cell.x][cell.y-1]=='#'){
dir.x = -dir.x;
cell.x--;
p.x = 0;
p.y = 1;
} else if(map[cell.x-1][cell.y]=='#'&&
map[cell.x][cell.y-1]=='.'){
dir.y = -dir.y;
cell.y--;
p.x = 1;
p.y = 0;
} else if(map[cell.x-1][cell.y]=='#'&&
map[cell.x][cell.y-1]=='#'){
dir.y = -dir.y;
dir.x = -dir.x;
p.x = 0;
p.y = 0;
} else {
System.err.println("error tl");
System.exit(-1);
}
} else if((p.x+xdir>0.9999&&p.x+xdir<1.0001)&&
(p.y+ydir>-0.0001&&p.y+ydir<0.0001)){ //top right
traveled += Math.sqrt(xdir*xdir + ydir*ydir);
if(map[cell.x-1][cell.y+1]=='.'){
cell.y++;
cell.x--;
p.x = 0;
p.y = 1;
} else if(map[cell.x][cell.y+1]=='.'&&
map[cell.x-1][cell.y]=='.'){
traveled = 99999;
break;
} else if(map[cell.x][cell.y+1]=='#'&&
map[cell.x-1][cell.y]=='.'){
dir.x = -dir.x;
cell.x--;
p.x = 1;
p.y = 1;
} else if(map[cell.x][cell.y+1]=='.'&&
map[cell.x-1][cell.y]=='#'){
dir.y = -dir.y;
cell.y++;
p.x = 0;
p.y = 0;
} else if(map[cell.x][cell.y+1]=='#'&&
map[cell.x-1][cell.y]=='#'){
dir.y = -dir.y;
dir.x = -dir.x;
p.x = 1;
p.y = 0;
} else {
System.err.println("error tr");
System.exit(-1);
}
} else if(p.x + xdir>0.9999){
xdir = 1 - p.x;
ydir = dir.y*xdir/dir.x;
traveled += Math.sqrt(xdir*xdir + ydir*ydir);
if(map[cell.x][cell.y+1]=='#'){
dir.x = -dir.x;
p.x = 1;
p.y = p.y + ydir;
} else {
cell.y++;
p.x = 0;
p.y = p.y + ydir;
}
} else if(p.x + xdir<0.0001){ //go to next x cell
xdir = -p.x;
ydir = dir.y*xdir/dir.x;
traveled += Math.sqrt(xdir*xdir + ydir*ydir);
if(map[cell.x][cell.y-1]=='#'){
dir.x = -dir.x;
p.x = 0;
p.y = p.y + ydir;
} else {
cell.y--;
p.x = 1;
p.y = p.y + ydir;
}
} else if(p.y + ydir > 0.9999){ //go to next y cell
traveled += Math.sqrt(xdir*xdir + ydir*ydir);
if(map[cell.x+1][cell.y]=='#'){
dir.y = -dir.y;
p.y = 1;
p.x = p.x + xdir;
} else {
cell.x++;
p.y = 0;
p.x = p.x + xdir;
}
} else if(p.y + ydir < 0.0001){ //go to next y cell
traveled += Math.sqrt(xdir*xdir + ydir*ydir);
if(map[cell.x-1][cell.y]=='#'){
dir.y = -dir.y;
p.y = 0;
p.x = p.x + xdir;
} else {
cell.x--;
p.y = 1;
p.x = p.x + xdir;
}
} else {
System.err.println("error");
System.exit(-1);
}
}
// System.out.println(traveled + " " + d.x + " " + d.y);
if(traveled<=D+0.0001&&cell.x==startcel.x&&cell.y==startcel.y&&p.x==0.5&&p.y==0.5){
ans++;
}
}
System.out.println("Case #"+(i+1)+": " + ans);
}
}
} | 0 | 536 |
C20003 | C20049 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 537 |
C20003 | C20034 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import uk.co.epii.codejam.common.AbstractProcessor;
/**
*
* @author jim
*/
public class HallOfMirrorsProcessor extends AbstractProcessor<Hall> {
private static final HashMap<Integer, ArrayList<RayVector>> cachedRays =
new HashMap<>();
private static int ccase = 0;
@Override
public String processDatum(Hall datum) {
System.out.println("Case: " + (++ccase));
ArrayList<RayVector> vectors = getVectors(datum.D);
int count = 0;
for (RayVector rv : vectors) {
Ray r = new Ray(datum.D * datum.D, rv, datum.meLocation);
if (r.createsReflection(datum))
count++;
}
return count + "";
}
public static ArrayList<RayVector> getVectors(int maxDistance) {
ArrayList<RayVector> shortest = cachedRays.get(maxDistance);
if (shortest != null)
return shortest;
ArrayList<RayVector> rays = new ArrayList<>();
for (int x = -maxDistance; x <= maxDistance; x++) {
for (int y = -maxDistance; y <= maxDistance; y++) {
if (x == 0 && y == 0)
continue;
if (x * x + y * y > maxDistance * maxDistance)
continue;
if (Math.abs(Fraction.GCD(x, y)) == 1)
rays.add(new RayVector(x, y));
}
}
return rays;
}
}
| 0 | 538 |
C20003 | C20036 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Fraction implements Comparable<Fraction> {
public final int n;
public final int d;
public Fraction(int i) {
this(i, 1);
}
public Fraction(int n, int d) {
int gcd = GCD(n, d);
if (gcd != 1) {
n /= gcd;
d/= gcd;
}
this.n = n;
this.d = d;
}
public Fraction (int i, int n, int d) {
this(i * d + n, d);
}
public Fraction add(Fraction f) {
if (d == f.d)
return new Fraction(n + f.n, d);
else {
return new Fraction(n * f.d + d * f.n, d * f.d);
}
}
public Fraction substract(Fraction f) {
if (d == f.d)
return new Fraction(n - f.n, d);
else {
return new Fraction(n * f.d - d * f.n, d * f.d);
}
}
public Fraction multiply(Fraction f) {
return new Fraction(n * f.n, d * f.d);
}
public Fraction multiply(int i) {
return new Fraction(n * i, d);
}
public static int GCD ( int a , int b )
{
if (a == 0)
return b;
while ( b != 0 )
{
int t = b ;
b = a % b ;
a = t ;
}
return a ;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Fraction) {
return compareTo((Fraction)obj) == 0;
}
return false;
}
public int compareTo(Fraction f) {
return n * f.d - f.n * d;
}
public int compareTo(int i) {
return n - i * d;
}
public boolean isInt() {
return n % d == 0;
}
public int floor() {
if (n == 0 || (n > 0 == d > 0))
return n / d;
else
return n / d - 1;
}
public Fraction abs() {
return new Fraction(Math.abs(n), Math.abs(d));
}
public Fraction fractionalPart() {
int N = n % d;
int D = d;
if (D < 0) {
N = -N;
D = -D;
}
while (N < 0)
N += D;
return new Fraction(N, D);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(n);
sb.append("/");
sb.append(d);
return sb.toString();
}
public Fraction divide(int i) {
return new Fraction(n, d * i);
}
public Fraction divide(Fraction f) {
return new Fraction(n * f.d, d * f.n);
}
}
| 0 | 539 |
C20003 | C20081 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| import java.util.Scanner;
public class Program
{
static final int RANGE = 50;
static Board board;
static double dist;
public static void main(String[] args) throws Exception
{
Scanner in = new Scanner(new java.io.FileReader("in"));
int t = in.nextInt();
for (int test = 0; test < t; test++)
{
board = new Board(in.nextInt(), in.nextInt());
dist = in.nextInt();
for (int r = 0; r < board.height; r++)
{
String str = in.next();
for (int c = 0; c < board.width; c++)
{
char ch = str.charAt(c);
int tile;
if (ch == '.') tile = 0;
else if (ch == '#') tile = 1;
else tile = 2;
board.set(r, c, tile);
}
}
int count = 0;
for (int dR = -RANGE; dR <= RANGE; dR++)
{
for (int dC = -RANGE; dC <= RANGE; dC++)
{
if (Calc.gcd(dR, dC) == 1)
{
boolean hit = ray(dR, dC);
if (hit) count++;
}
}
}
System.out.println("Case #" + (test + 1) + ": " + count);
}
}
static boolean ray(int dR, int dC)
{
Point p = board.player.clone();
Point v = new Point(new Fraction(dR, 1), new Fraction(dC, 1));
double d = 0;
while (d <= dist)
{
Point nP = board.reflect(p, v);
if (nP == null) return false;
double addD = nP.distance(p);
if (board.hitsPlayer(p, nP) && d + nP.distance(board.player) <= dist)
{
return true;
}
p = nP;
d += addD;
}
return false;
}
}
| 0 | 540 |
C20003 | C20038 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| import java.io.*;
import java.util.*;
import static java.lang.System.*;
public class D {
public static void main (String [] args) throws IOException {new D().run();}
public void run() throws IOException{
Scanner file = new Scanner(new File("D-large.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D4.out")));
int times = Integer.parseInt(file.nextLine());
for(int asdf = 0; asdf<times; asdf++){
System.out.println (asdf);
int row = file.nextInt(), col = file.nextInt();
int d = file.nextInt(); file.nextLine();
double px=0,py=0;
for(int i = 0; i<row; i++){
String s = file.nextLine();
if( s.contains("X")){
py = i+.5-1; px = s.indexOf("X")+.5-1;
}
}
row-=2;col-=2;
int XLIM = 500;
int YLIM = 500;
HashSet<Double> set = new HashSet<Double>();
for(int x = -XLIM; x<=XLIM; x++)
for(int y = -YLIM; y<=YLIM; y++){
if( x==0&&y==0)continue;
double xx,yy;
if( x%2==0) xx = x*col+px;
else xx = (x+1)*col-px;
if( y%2==0) yy = y*row+py;
else yy = (y+1)*row-py;
if( Math.sqrt(Math.pow(xx-px,2)+Math.pow(yy-py,2)) <= d)
set.add(Math.atan2(yy-py,xx-px));
}
int count = 0;
for(double d1 : set)
for(double d2 : set)
if( Math.abs(d1-d2)<1e-5)count++;
if(count != set.size()) System.out.println ("FAIL");
out.println ("Case #"+(asdf+1)+": "+set.size());
//System.out.println (set);
}
out.close();
System.exit(0);
}
} | 0 | 541 |
C20003 | C20058 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 542 |
C20003 | C20011 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
|
public class CG {
/** get the intersection between a segment (p1, p2), and a project line from the origin
* with an angle.
* @return Point(INF, INF) if there is no intersection
* */
public static Point intersectSegmentOri(Point p1, Point p2, double angle)
{
Point ori = new Point(0.0, 0.0), out = new Point(Math.cos(angle),
Math.sin(angle));
Point intersect = new Point(0,0);
int stat = crossPointLL(p1, p2, ori, out, intersect);
// parallel
if(stat == 0)
return new Point(CGConst.INF, CGConst.INF);
// check the case of intersect at the wrong side of the project line
if(Math.cos(angle) * (intersect.x) < 0 || Math.sin(angle) * (intersect.y) < 0)
return new Point(CGConst.INF, CGConst.INF);
// check if the intersection is on the segment
if(intersectSP(p1, p2, intersect))
return intersect;
return new Point(CGConst.INF, CGConst.INF);
}
static boolean intersectSP(Point s0, Point s1, Point p) {
return s0.minus(p).abs() + s1.minus(p).abs() - s1.minus(s0).abs()< CGConst.EPS; // triangle inequality
}
private static int crossPointLL(Point m0, Point m1, Point n0, Point n1,
Point p) {
if (Math.abs (cross (m1.minus(m0), n1.minus(n0))) > CGConst.EPS) {
// non-parallel => intersect
double h = cross (m1 .minus( m0), n1.minus(n0));
double k = cross (m1.minus( m0), m1.minus(n0));
p.copy( n1.minus(n0).mul(k/h).add(n0));
return 1; // intersect at one point
}
if (Math.abs (cross (m1.minus(m0), n0.minus(m0))) < CGConst.EPS) { // area==0 => same line => intersect
p.copy( m0); // one of the intersection points, or m1, n0, n1, ...
return -1; // intersect at infinitely many points (when 2 parallel lines overlap)
}
return 0; // no intersection points (when two lines are parallel but does not overlap)
}
private static double cross(Point a, Point b) {
return a.x * b.y - a.y * b.x;
}
public static double distance(Point a, Point b)
{
return a.minus(b).abs();
}
public static double getAngle(Point a, Point b)
{
return Math.acos(point(a, b)/(a.abs()*b.abs()));
}
private static double point(Point a, Point b) {
return a.x*b.x + a.y*b.y;
}
static int dblcmp (double a, double b) {
if (Math.abs (a-b) < CGConst.EPS) return 0;
return a < b ? -1 : 1;
}
public static int ccw (Point a, Point b, Point c) { // short version
return dblcmp (cross (b.minus(a), c.minus(a)), 0);
}
// para: s0 and s1 form a segment s
// para: t0 and t1 form a segment t
// return: true if s and t appears like a 'X'
public static boolean properIntersectSS (Point s0, Point s1, Point t0, Point t1) {
return ccw (s0, s1, t0) * ccw (s0, s1, t1) < 0 &&
ccw (t0, t1, s0) * ccw (t0, t1, s1) < 0;
}
public static double triArea (Point a, Point b, Point c) {
// centroid = (a + b + c) / 3.0; // centroid of triangle
return Math.abs (cross (b.minus(a), c.minus(a))) * 0.5; // |cross product| / 2
}
/** check if two segments intersect and find the intersection point
para:
input s0 and s1 form a line s
input t0 and t1 form a line t
output p is the intersection point (if return value != 0)
return:
1: the segments intersect at exactly one point
-1: the segments intersect improperly, p is ONE OF the intersection points
0: no intersection points
note: If you are sure the segment intersect and want to find the intersection point,
you can include the statements in the first *if* block only.
*/
public static int crossPointSS( Point s0, Point s1, Point t0, Point t1, Point p) {
if (properIntersectSS (s0, s1, t0, t1)) {
double r = triArea (s0, t0, t1) / triArea (s1, t0, t1);
Point temp = s0.add( (s1.minus(s0)).mul((r / (1+r))));
p.x = temp.x;
p.y = temp.y;
return 1;
}
if (intersectSP (s0, s1, t0)) { p.copy(t0); return -1; }
if (intersectSP (s0, s1, t1)) { p .copy( t1); return -1; }
if (intersectSP (t0, t1, s0)) { p .copy(s0); return -1; }
if (intersectSP (t0, t1, s1)) { p .copy(s1); return -1; }
return 0;
}
}
| 0 | 543 |
C20003 | C20032 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
import uk.co.epii.codejam.common.AbstractMain;
/**
*
* @author jim
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.err.println("Hello");
new AbstractMain<Hall>(new HallFactory(),
new HallOfMirrorsProcessor()).main(args);
}
}
| 0 | 544 |
C20003 | C20033 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public enum RayStep {
EXPIRES,
REFLECTS_HORIZONTALLY,
REFLECTS_VERTICALLY,
INVERTS,
CONTINUES,
HITS;
}
| 0 | 545 |
C20003 | C20014 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
|
public class Point {
public double x, y;
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public Point() {
x=0; y=0;
}
@Override
public boolean equals(Object arg0) {
Point pt = (Point)arg0;
return CG.dblcmp(pt.x, x) == 0 && CG.dblcmp(pt.y, y) == 0;
}
public Point minus(Point p)
{
return new Point(x-p.x, y-p.y);
}
public Point add(Point p)
{
return new Point(x+p.x, y+p.y);
}
public Point mul(double n)
{
return new Point(n*x, n*y);
}
public double abs()
{
return Math.sqrt(x*x + y*y);
}
public void copy(Point p)
{
this.x = p.x;
this.y = p.y;
}
}
| 0 | 546 |
C20003 | C20053 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package jp.funnything.competition.util;
import java.util.Iterator;
/**
* Do NOT change the element in iteration
*/
public class Combination implements Iterable< int[] > , Iterator< int[] > {
private final int _n;
private final int _k;
private int[] _data;
public Combination( final int n , final int k ) {
if ( k < 0 || k > n ) {
throw new IllegalArgumentException();
}
_n = n;
_k = k;
}
@Override
public boolean hasNext() {
return _data == null || _data.length > 0 && _data[ 0 ] < _n - _k;
}
@Override
public Iterator< int[] > iterator() {
return this;
}
@Override
public int[] next() {
if ( _data == null ) {
_data = new int[ _k ];
for ( int index = 0 ; index < _k ; index++ ) {
_data[ index ] = index;
}
} else {
int i = 0;
while ( i < _k - 1 && _data[ i + 1 ] == _data[ i ] + 1 ) {
_data[ i ] = i++;
}
_data[ i ]++;
}
return _data;
}
@Override
public void remove() {
}
}
| 0 | 547 |
C20003 | C20046 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 548 |
C20003 | C20061 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package jp.funnything.competition.util;
import java.util.Iterator;
/**
* Do NOT change the element in iteration
*/
public class Combination implements Iterable< int[] > , Iterator< int[] > {
private final int _n;
private final int _k;
private int[] _data;
public Combination( final int n , final int k ) {
if ( k < 0 || k > n ) {
throw new IllegalArgumentException();
}
_n = n;
_k = k;
}
@Override
public boolean hasNext() {
return _data == null || _data.length > 0 && _data[ 0 ] < _n - _k;
}
@Override
public Iterator< int[] > iterator() {
return this;
}
@Override
public int[] next() {
if ( _data == null ) {
_data = new int[ _k ];
for ( int index = 0 ; index < _k ; index++ ) {
_data[ index ] = index;
}
} else {
int i = 0;
while ( i < _k - 1 && _data[ i + 1 ] == _data[ i ] + 1 ) {
_data[ i ] = i++;
}
_data[ i ]++;
}
return _data;
}
@Override
public void remove() {
}
}
| 0 | 549 |
C20003 | C20087 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
|
import java.io.*;
import java.util.*;
public class CodeJam2012_Q_D {
public int calc(int H, int W, int D, String[] mirror) {
int[][] seen = new int[D*2+1][D*2+1];
int sx=0,sy=0;
for(int i=0; i<D*2+1; i++)
Arrays.fill(seen[i], -1);
boolean[][] m = new boolean[H][W];
for(int i=0; i<H; i++) {
if(mirror[i].indexOf('X')>=0) {
sx = mirror[i].indexOf('X');
sy = i;
}
for(int j=0; j<W; j++) {
if(mirror[i].charAt(j)=='#') m[i][j] = true;
}
}
for(int y=-D; y<=D; y++) {
for(int x=-D; x<=D; x++) {
if(y*y+x*x>D*D || (x==0 && y==0)) {
seen[y+D][x+D] = 0;
}
if(seen[y+D][x+D]!=-1) continue;
int gcd = Math.abs(gcd(x, y));
int dx = x/gcd;
int dy = y/gcd;
int unit = dx*dy==0? 2:Math.abs(dx*dy)*2;
int cellx = sx;
int celly = sy;
int lx = unit/2;
int ly = unit/2;
int pathMax = x==0? y*unit/dy : x*unit/dx;
int pathCnt = 0;
while(pathCnt<pathMax) {
pathCnt++;
lx += dx;
ly += dy;
int nx, ny, ndx, ndy, nlx, nly;
if(celly==sy && cellx==sx && ly==unit/2 && lx==unit/2) {
//return to start point
seen[y+D][x+D]=1;
dx = x/gcd;
dy = y/gcd;
for(int d=1; -D<=y+d*dy && y+d*dy<=D && -D<=x+d*dx && x+d*dx<=D; d++) {
seen[y+d*dy+D][x+d*dx+D]=0;
}
break;
}
nx=cellx;
ny=celly;
ndx=dx;
ndy=dy;
nlx=lx;
nly=ly;
if(ly%unit==0 && lx%unit==0) {
if(m[celly + (dy>0?1:-1)][cellx + (dx>0?1:-1)]) {
//hit a corner
if(!m[celly + (dy>0?1:-1)][cellx] && !m[celly][cellx + (dx>0?1:-1)]) break;
//reflected with horizontal mirror
if(m[celly + (dy>0?1:-1)][cellx]) {
ndy = -dy;
} else {
ny = celly+(dy>0?1:-1);
nly = ly==0?unit:0;
}
//reflected with vertical mirror
if(m[celly][cellx + (dx>0?1:-1)]) {
ndx = -dx;
} else {
nx = cellx+(dx>0?1:-1);
nlx = lx==0?unit:0;
}
} else {
nx = cellx+(dx>0?1:-1);
ny = celly+(dy>0?1:-1);
nlx = lx==0?unit:0;
nly = ly==0?unit:0;
}
} else if(ly%unit==0) {
//reflected with horizontal mirror
if(m[celly + (dy>0?1:-1)][cellx]) {
ndy = -dy;
} else {
ny = celly+(dy>0?1:-1);
nly = ly==0?unit:0;
}
} else if(lx%unit==0) {
//reflected with vertical mirror
if(m[celly][cellx + (dx>0?1:-1)]) {
ndx = -dx;
} else {
nx = cellx+(dx>0?1:-1);
nlx = lx==0?unit:0;
}
}
cellx=nx;
celly=ny;
dx=ndx;
dy=ndy;
lx=nlx;
ly=nly;
}
if(seen[y+D][x+D]==-1) seen[y+D][x+D]=0;
}
}
int cnt=0;
for(int[] s1 : seen)
for(int s2 : s1)
cnt += s2;
return cnt;
}
public int gcd(int a, int b) {
return b==0? a : gcd(b, a%b);
}
public static void main(String[] args) {
try{
// (new CodeJam2012_Q_D()).exec("D-small-attempt0.in", "2012_Q_D-small.out");
(new CodeJam2012_Q_D()).exec("D-large.in", "2012_Q_D-large.out");
}catch(Exception ex) {
}
}
public final void exec(String inFileName, String outFileName) throws Exception{
BufferedReader inReader = new BufferedReader(new FileReader(inFileName));
PrintWriter outWriter = new PrintWriter(new BufferedWriter(new FileWriter(outFileName)));
int caseNums=0;
caseNums = Integer.parseInt(inReader.readLine());
for(int i=0; i<caseNums; i++) {
String[] input = inReader.readLine().split(" ");
int H = Integer.valueOf(input[0]);
int W = Integer.valueOf(input[1]);
int D = Integer.valueOf(input[2]);
String[] mirror = new String[H];
for(int j=0; j<H; j++) {
mirror[j] = inReader.readLine();
}
int outStr = calc(H, W, D, mirror);
String fmtOutStr="Case #" + (i+1) + ": " + outStr;
outWriter.println(fmtOutStr);
System.out.println(fmtOutStr);
}
System.out.println(caseNums + " cases complete");
outWriter.close();
inReader.close();
}
}
| 0 | 550 |
C20003 | C20079 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| public class Frac implements Comparable<Frac> {
public static final Frac ZERO = new Frac(0);
public static final Frac ONE = new Frac(1);
public final int num, denom;
public Frac(int num) {
this(num, 1);
}
public Frac(int num, int denom) {
if (denom == 0) {
if (num < 0)
this.num = -1;
else if (num > 0)
this.num = 1;
else
this.num = 0;
this.denom = 0;
} else {
int gcd;
if (denom < 0) {
gcd = -Math.abs(gcd(num, denom));
} else {
gcd = Math.abs(gcd(num, denom));
}
this.num = num / gcd;
this.denom = denom / gcd;
}
}
public Frac add(Frac other) {
return new Frac(num * other.denom + denom * other.num, denom
* other.denom);
}
public Frac mult(Frac other) {
return new Frac(num * other.num, denom * other.denom);
}
public Frac sub(Frac other) {
return new Frac(num * other.denom - denom * other.num, denom
* other.denom);
}
public Frac div(Frac other) {
return new Frac(num * other.denom, denom * other.num);
}
public Frac neg() {
return new Frac(-num, denom);
}
public Frac inv() {
return new Frac(denom, num);
}
@Override
public boolean equals(Object other) {
return other instanceof Frac && equals((Frac) other);
}
@Override
public int hashCode() {
return (31 + num) * 31 + denom;
}
public boolean equals(Frac other) {
return num == other.num && denom == other.denom;
}
public boolean equals(int o) {
return denom == 1 && num == o;
}
@Override
public String toString() {
if (denom == 1)
return Integer.toString(num);
else
return num + "/" + denom;
}
@Override
public int compareTo(Frac o) {
return num * o.denom - o.num * denom;
}
public static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
public Frac roundAddSig(Frac sgn) {
if (sgn.num > 0)
return roundAddUp();
else if (sgn.num < 0)
return roundAddDown();
else
return null;
}
public Frac roundAddUp() {
if (num < 0)
return new Frac((num + 1) / denom);
else
return new Frac(num / denom + 1);
}
public Frac roundAddDown() {
if (num > 0)
return new Frac((num - 1) / denom);
else
return new Frac(num / denom - 1);
}
public int roundDownAddSmallUp() {
if (num < 0)
return (num - denom + 1) / denom;
else
return num / denom;
}
public int roundDownAddSmallDown() {
if (num < 0)
return num / denom - 1;
else
return (num - 1) / denom;
}
public boolean isZero() {
return num == 0;
}
public int sgn() {
if (num < 0)
return -1;
else if (num > 0)
return 1;
else
return 0;
}
}
| 0 | 551 |
C20003 | C20047 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 552 |
C20003 | C20007 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package qualificationRound;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class P4 {
public static String[] map;
public static int x = 0, y = 0;
public static int calGCD(int n, int m) {
if (m == 0)
return n;
if (n == 0)
return m;
if (m < n) {
int tmp = m;
m = n;
n = tmp;
}
while (n != 0) {
int tmp = m % n;
m = n;
n = tmp;
}
return m;
}
public static boolean check(double unit_x, double unit_y, int step) {
boolean ret = false;
double start_x = x - 0.5, start_y = y - 0.5;
double tmp_x = start_x, tmp_y = start_y;
for (int i = 1; i <= step; ++i) {
tmp_x += unit_x;
tmp_y += unit_y;
if (Math.abs(tmp_x - start_x) < 1E-9
&& Math.abs(tmp_y - start_y) < 1E-9) {
if (i == step)
return true;
else
return false;
}
int int_x = (int) Math.round(tmp_x);
int int_y = (int) Math.round(tmp_y);
if (Math.abs(tmp_x - int_x) < 1E-9
&& Math.abs(tmp_y - int_y) < 1E-9) {
if (unit_x > 0)
int_x = int_x + 1;
if (unit_y > 0)
int_y = int_y + 1;
if (map[int_x].charAt(int_y) == '#') {
if (map[int_x - (int) Math.signum(unit_x)].charAt(int_y) == '#'
&& map[int_x].charAt(int_y
- (int) Math.signum(unit_y)) == '#') {
unit_x = -unit_x;
unit_y = -unit_y;
} else if (map[int_x - (int) Math.signum(unit_x)]
.charAt(int_y) == '#') {
unit_y = -unit_y;
} else if (map[int_x].charAt(int_y
- (int) Math.signum(unit_y)) == '#') {
unit_x = -unit_x;
} else {
return false;
}
}
} else if (Math.abs(tmp_x - int_x) < 1E-9) {
if (unit_x > 0)
int_x = int_x + 1;
int_y = (int) Math.ceil(tmp_y);
if (map[int_x].charAt(int_y) == '#')
unit_x = -unit_x;
} else if (Math.abs(tmp_y - int_y) < 1E-9) {
if (unit_y > 0)
int_y = int_y + 1;
int_x = (int) Math.ceil(tmp_x);
if (map[int_x].charAt(int_y) == '#')
unit_y = -unit_y;
}
}
return ret;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D-large.in"));
FileWriter fw = new FileWriter("out.txt");
int t = Integer.parseInt(br.readLine());
for (int c = 1; c <= t; ++c) {
args = br.readLine().split(" ");
int h = Integer.parseInt(args[0]);
int w = Integer.parseInt(args[1]);
int d = Integer.parseInt(args[2]);
int ans = 0;
map = new String[h];
for (int j = 0; j < h; ++j) {
map[j] = br.readLine();
if (map[j].indexOf('X') != -1) {
x = j;
y = map[j].indexOf('X');
}
}
for (int i = x - d; i <= x + d; ++i) {
for (int j = y - d; j <= y + d; ++j) {
int dx = x - i, dy = y - j;
if (dx * dx + dy * dy > d * d || (dx == 0 && dy == 0))
continue;
double unit_x, unit_y;
int gcd = calGCD(Math.abs(dx), Math.abs(dy));
int step;
if (dx != 0 && dy != 0)
step = 2 * Math.abs(dx) * Math.abs(dy) /gcd;
else if (dx == 0) {
step = 2 * Math.abs(dy);
} else {
step = 2 * Math.abs(dx);
}
unit_x = (double) dx / step;
unit_y = (double) dy / step;
if (check(unit_x, unit_y, step))
ans++;
}
}
fw.append("Case #" + c + ": " + ans + "\n");
System.out.println("Case #" + c + ": " + ans);
}
br.close();
fw.close();
}
}
| 0 | 553 |
C20003 | C20028 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
import java.awt.Point;
/**
*
* @author jim
*/
public class Hall {
public final int H;
public final int W;
public final int D;
public final FractionPoint meLocation;
private final Square[][] floor;
public Hall(int H, int W, int D, FractionPoint meLocation, Square[][] floor) {
this.H = H;
this.W = W;
this.D = D;
this.meLocation = meLocation;
this.floor = floor;
}
public RayVector processBoundary(FractionPoint fp, RayVector v) {
switch (getStep(fp, v)) {
case CONTINUES:
return v;
case EXPIRES:
return null;
case REFLECTS_HORIZONTALLY:
return new RayVector(-v.x, v.y);
case REFLECTS_VERTICALLY:
return new RayVector(v.x, -v.y);
case INVERTS:
return new RayVector(-v.x, -v.y);
default:
throw new IllegalArgumentException("The getStep has returned "
+ "an invalid result");
}
}
public RayStep getStep(FractionPoint fp, RayVector v) {
boolean xIsInt = fp.x.isInt();
boolean yIsInt = fp.y.isInt();
if (xIsInt && yIsInt) {
Surround surround = getSurround(new Point(fp.x.floor(), fp.y.floor()));
int xIndex = v.x > 0 ? 1 : 0;
int yIndex = v.y > 0 ? 1 : 0;
Square entering = surround.get(xIndex, yIndex);
if (entering != Square.MIRROR)
return RayStep.CONTINUES;
else if (surround.get(v.x > 0 ? 1 : 0, v.y > 0 ? 0 : 1) == Square.MIRROR &&
surround.get(v.x > 0 ? 0 : 1, v.y > 0 ? 1 : 0) == Square.MIRROR)
return RayStep.INVERTS;
else if (surround.get(v.x > 0 ? 1 : 0, v.y > 0 ? 0 : 1) != Square.MIRROR &&
surround.get(v.x > 0 ? 0 : 1, v.y > 0 ? 1 : 0) != Square.MIRROR)
return RayStep.EXPIRES;
else {
if ((surround.get(0, 0) == Square.MIRROR && surround.get(0, 1) == Square.MIRROR) ||
(surround.get(1, 0) == Square.MIRROR && surround.get(1, 1) == Square.MIRROR))
return RayStep.REFLECTS_HORIZONTALLY;
else
return RayStep.REFLECTS_VERTICALLY;
}
}
else if (xIsInt) {
Square entering = getSquare(
fp.x.floor() + (v.x > 0 ? 0 : -1), fp.y.floor());
if (entering == Square.MIRROR)
return RayStep.REFLECTS_HORIZONTALLY;
else
return RayStep.CONTINUES;
}
else if (yIsInt) {
int x = fp.x.floor();
int y = fp.y.floor() + (v.y > 0 ? 0 : -1);
Square entering = getSquare(x, y);
if (entering == Square.MIRROR)
return RayStep.REFLECTS_VERTICALLY;
else
return RayStep.CONTINUES;
}
else
throw new IllegalArgumentException("You are not at a boundary!");
}
public Surround getSurround(Point p) {
Square[][] surround = new Square[2][];
surround[0] = new Square[2];
surround[1] = new Square[2];
surround[0][0] = getSquare(p.x - 1, p.y - 1);
surround[1][1] = getSquare(p);
surround[1][0] = getSquare(p.x - 1, p.y);
surround[0][1] = getSquare(p.x, p.y - 1);
return new Surround(surround);
}
public Square getSquare(int x, int y) {
return floor[y][x];
}
public Square getSquare(Point p) {
return getSquare(p.x, p.y);
}
}
| 0 | 554 |
C20003 | C20022 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package template;
//standard libraries potentially used:
//Apache commons http://http://commons.apache.org/
//Google Guava http://code.google.com/p/guava-libraries/
import java.util.ArrayList;
public class Template {
public static void main(String[] args) {
//test();
//Utils.die("Done testing");
String folder = "C:\\Users\\Paul Thomson\\Documents\\CodeJam\\HallOfMirrors\\";
Utils.logfile = folder + "log.txt";
String infile = folder + "data.in";
String outfile = infile.substring(0, infile.lastIndexOf(".")) + ".out";
ArrayList<TestCase> tcList = TestCaseIO.loadFromFile(infile);
//ArrayList<TestCase> tcList = TestCaseIO.mockUp();
int numThreads = 1;
if (numThreads == 1) {
TestCaseSolver tcSolver = new TestCaseSolver(tcList, 1);
tcSolver.run();
} else {
//split into separate lists
ArrayList<ArrayList<TestCase>> tcSubLists = new ArrayList<>();
for (int i = 0; i < numThreads; i++) {
tcSubLists.add(new ArrayList<TestCase>());
}
int i = 0;
for (TestCase tc : tcList) {
tcSubLists.get(i).add(tc);
i++;
if (i == numThreads) {
i = 0;
}
}
//run each sublist in its own thread
ArrayList<Thread> threadList = new ArrayList<>();
int ref = 1;
for (ArrayList<TestCase> tcl : tcSubLists) {
TestCaseSolver tcs = new TestCaseSolver(tcl, ref);
Thread h = new Thread(tcs);
threadList.add(h);
h.start();
ref++;
}
//wait for completion
for (Thread h : threadList) {
try {
h.join();
} catch (InterruptedException ex) {
Utils.die("InterruptedException waiting for threads");
}
}
}
TestCaseIO.writeSolutions(tcList, outfile);
double totalTime = 0;
for (TestCase tc : tcList) {
totalTime += tc.getTime();
}
double avTime = totalTime / (double)tcList.size();
Utils.sout("Total compute time " + String.format("%.2f", totalTime) + " secs.");
Utils.sout("Average compute time " + String.format("%.2f", avTime) + " secs.");
Utils.sout("Done.");
}
public static void test() {
}
}
| 0 | 555 |
C20003 | C20016 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| // Author: Alejandro Sotelo Arevalo
package qualification;
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
public class D_HallOfMirrors {
//--------------------------------------------------------------------------------
private static String ID="D";
private static String NAME="large";
private static boolean STANDARD_OUTPUT=false;
//--------------------------------------------------------------------------------
public static void main(String[] args) throws Throwable {
BufferedReader reader=new BufferedReader(new FileReader(new File("data/"+ID+"-"+NAME+".in")));
if (!STANDARD_OUTPUT) System.setOut(new PrintStream(new File("data/"+ID+"-"+NAME+".out")));
for (int c=1,T=Integer.parseInt(reader.readLine()); c<=T; c++) {
String w[]=reader.readLine().trim().split(" +");
int H=Integer.parseInt(w[0]),W=Integer.parseInt(w[1]),D=Integer.parseInt(w[2]);
Point point=null;
Collection<Point> mirrors=new ArrayList<Point>();
char[][] hall=new char[H][];
for (int y=0; y<H; y++) {
hall[y]=reader.readLine().toCharArray();
for (int x=0; x<W; x++) {
if (Character.toUpperCase(hall[y][x])=='X') {
point=new Point(x,y);
}
else if (hall[y][x]=='#') {
mirrors.add(new Point(x,y));
}
}
}
int result=simulateAngles(point,mirrors,D);
System.out.println("Case #"+c+": "+result);
}
if (!STANDARD_OUTPUT) System.out.close();
reader.close();
}
private static int simulateAngles(Point point, Collection<Point> mirrors, int D) {
int count=0;
for (int i=0; i<=D; i++) for (int j=1; j<=D; j++) if (gcd(i,j)==1) {
for (int k=0; k<4; k++) {
double minimum=simulateAngle(new Particle(point.x,point.y,new Angle(k==0?i:(k==1?-j:(k==2?-i:j)),k==0?j:(k==1?i:(k==2?-j:-i)))),mirrors,D);
if (minimum<1E-12) count++;
}
}
return count++;
}
private static long gcd(long a, long b) {
return b==0?a:gcd(b,a%b);
}
private static double simulateAngle(Particle point, Collection<Point> mirrors, double distance) {
//System.out.println("Simulation with x="+point.x+",y="+point.y+",angle="+point.angle.getValue()+",distance="+distance);
Angle angle=point.angle;
double x=point.x,y=point.y,d=distance,walked=0;
double result=Double.POSITIVE_INFINITY;
while (d>0) {
double best=d;
double nextX=x+d*angle.cos(),nextY=y+d*angle.sin();
Angle nextAngle=angle;
Collection<Collision> collisions=new ArrayList<Collision>();
for (Point mirror:mirrors) {
Collision collision=collision(new Particle(x,y,angle),mirror);
if (collision!=null) {
double dist=collision.distance(x,y);
if (eq(dist,0)) {
continue;
}
if (le(dist,best)) {
collisions.clear();
}
if (leq(dist,best)) {
collisions.add(collision);
best=dist;
}
}
}
boolean destroyRay=collisions.isEmpty();
if (collisions.size()==1) {
Collision collision=collisions.iterator().next();
nextX=collision.x;
nextY=collision.y;
if (collision.isCorner()==-1) {
nextAngle=reflection(collision.side,angle);
}
else {
destroyRay=collision.insideMirror(angle);
}
}
else if (collisions.size()==2) {
Iterator<Collision> iterator=collisions.iterator();
Collision c1=iterator.next(),c2=iterator.next();
nextX=c1.x;
nextY=c1.y;
if (c1.mirror.x==c2.mirror.x) {
nextAngle=reflection(leq(x,c1.mirror.x)?3:1,angle);
}
else if (c1.mirror.y==c2.mirror.y) {
nextAngle=reflection(leq(y,c1.mirror.y)?4:2,angle);
}
else {
// The angle doesn't change
}
}
else if (collisions.size()==3) {
Collision collision=collisions.iterator().next();
nextX=collision.x;
nextY=collision.y;
nextAngle=new Angle(-angle.x,-angle.y);
}
if (ge(walked,0)) result=Math.min(result,Line2D.ptSegDistSq(x,y,nextX,nextY,point.x,point.y));
walked+=destroyRay?d:Point2D.distance(x,y,nextX,nextY);
d=destroyRay?0:d-Point2D.distance(x,y,nextX,nextY);
x=nextX;
y=nextY;
angle=nextAngle;
//System.out.println(" nextX="+x+"nextY="+y+"result="+result+";"+walked);
}
return result;
}
private static Angle reflection(int side, Angle angle) {
int x=angle.x,y=angle.y;
switch (side) {
case 1:
case 3:
return new Angle(-x,y);
case 2:
case 4:
return new Angle(x,-y);
}
return angle;
}
private static Collision collision(Particle point, Point mirror) {
Angle angle=point.angle;
double xp=point.getX(),yp=point.getY();
double xm=mirror.getX(),ym=mirror.getY();
Collision result=null;
if (angle.y==0&&angle.x>0) {
result=leq(xp,xm-0.5)&&geq(yp,ym-0.5)&&leq(yp,ym+0.5)?new Collision(xm-0.5,yp,mirror,3):null;
}
else if (angle.y==0&&angle.x<0) {
result=geq(xp,xm+0.5)&&geq(yp,ym-0.5)&&leq(yp,ym+0.5)?new Collision(xm+0.5,yp,mirror,1):null;
}
else if (angle.x==0&&angle.y>0) {
result=leq(yp,ym-0.5)&&geq(xp,xm-0.5)&&leq(xp,xm+0.5)?new Collision(xp,ym-0.5,mirror,4):null;
}
else if (angle.x==0&&angle.y<0) {
result=geq(yp,ym+0.5)&&geq(xp,xm-0.5)&&leq(xp,xm+0.5)?new Collision(xp,ym+0.5,mirror,2):null;
}
else {
double ma=angle.tan(),mb=angle.atan();
double xc=xp+angle.x,yc=yp+angle.y;
double x1=xm-0.5,y1=yp+ma*(x1-xp);
double x2=xm+0.5,y2=yp+ma*(x2-xp);
double y3=ym-0.5,x3=xp+mb*(y3-yp);
double y4=ym+0.5,x4=xp+mb*(y4-yp);
Collection<Collision> collection=new ArrayList<Collision>(4);
if (geq(y1,ym-0.5)&&leq(y1,ym+0.5)&&(xc>xp)==(x1>xp)) collection.add(new Collision(x1,y1,mirror,3));
if (geq(y2,ym-0.5)&&leq(y2,ym+0.5)&&(xc<xp)==(x2<xp)) collection.add(new Collision(x2,y2,mirror,1));
if (geq(x3,xm-0.5)&&leq(x3,xm+0.5)&&(yc>yp)==(y3>yp)) collection.add(new Collision(x3,y3,mirror,4));
if (geq(x4,xm-0.5)&&leq(x4,xm+0.5)&&(yc<yp)==(y4<yp)) collection.add(new Collision(x4,y4,mirror,2));
for (Collision c:collection) {
if (result==null||c.distance(point)<result.distance(point)) result=c;
}
}
return result!=null&&ge(result.distance(point),0)?result:null;
}
private static double EPSILON=1E-8;
private static boolean eq(double a, double b) {
return Math.abs(a-b)<EPSILON;
}
private static boolean leq(double a, double b) {
return a<=b+EPSILON;
}
private static boolean geq(double a, double b) {
return a>=b-EPSILON;
}
private static boolean le(double a, double b) {
return a<b-EPSILON;
}
private static boolean ge(double a, double b) {
return a>b+EPSILON;
}
private static class Particle extends Point2D {
private double x;
private double y;
private Angle angle;
public Particle(double x, double y, Angle angle) {
this.x=x;
this.y=y;
this.angle=angle;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setLocation(double x, double y) {
this.x=x;
this.y=y;
}
}
private static class Collision extends Point2D {
private double x;
private double y;
private Point mirror;
private int side;
public Collision(double x, double y, Point mirror, int side) {
this.x=x;
this.y=y;
this.mirror=mirror;
this.side=side;
}
public int isCorner() {
double xm=mirror.getX(),ym=mirror.getY();
if (eq(distance(xm+0.5,ym-0.5),0)) return 1;
if (eq(distance(xm+0.5,ym+0.5),0)) return 2;
if (eq(distance(xm-0.5,ym+0.5),0)) return 3;
if (eq(distance(xm-0.5,ym-0.5),0)) return 4;
return -1;
}
public boolean insideMirror(Angle angle) {
double xp=x+0.5*angle.cos(),yp=y+0.5*angle.sin();
double xm=mirror.getX(),ym=mirror.getY();
return geq(xp,xm-0.5)&&leq(xp,xm+0.5)&&geq(yp,ym-0.5)&&leq(yp,ym+0.5);
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setLocation(double x, double y) {
this.x=x;
this.y=y;
}
}
private static class Angle {
private int x;
private int y;
private double r;
public Angle(int x, int y) {
this.x=x;
this.y=y;
r=Math.sqrt(1.0*x*x+1.0*y*y);
}
public double tan() {
return 1.0*y/x;
}
public double atan() {
return 1.0*x/y;
}
public double cos() {
return 1.0*x/r;
}
public double sin() {
return 1.0*y/r;
}
@SuppressWarnings("unused")
public double getDegrees() {
return Math.toDegrees(Math.atan2(y,x));
}
@SuppressWarnings("unused")
public double getRadians() {
return Math.atan2(y,x);
}
}
}
| 0 | 556 |
C20003 | C20072 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* CodeJamTester1A.java
*
* Created on 28.07.2008, 14:20:39
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package qualification;
import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @author Besitzer
*/
public class CodeJamQuali {
int testcases;
String dict="yhesocvxduiglbkrztnwjpfmaq";
BufferedReader BR;
BigInteger ZERO =BigInteger.ZERO;
BigInteger ONE =BigInteger.ONE;
BigInteger TWO =new BigInteger("2");
BigInteger THREE =new BigInteger("3");
public void sort(long[] a){
for(int i=0;i<a.length;i++){
long max=a[i];
int minAt=-1;
for(int j=i+1;j<a.length;j++){
if(a[j]>max){max=a[j];minAt=j;}
}
//System.out.println("maxAT:"+minAt+" max: "+max);
if(minAt>-1){
long buf =a[i];
a[i]=a[minAt];
a[minAt]=buf;
}
}
}
public void fillDict(String E, String G, char[] C){
for(int i=0;i<E.length();i++){
char e= E.charAt(i);
char g =G.charAt(i);
int p = g-'a';
if(g!=' ')C[p]=e;
}
}
public String testCase1() throws IOException{
String read =BR.readLine();
//System.out.println(read);
for(char c='a';c<='z';c++)
{
read=read.replace(c,Character.toUpperCase(dict.charAt(c-'a')));
}
return read.toLowerCase();
}
public String testCase2() throws IOException{
String[] St= BR.readLine().split(" ");
int N = Integer.parseInt(St[0]);
int S = Integer.parseInt(St[1]);
int p = Integer.parseInt(St[2]);
int incable=0;
int ok=0;int cur;
int incbound = 3*p-4;
incbound =incbound >1 ? incbound :1;
for(int i =0;i<N;i++){
cur=Integer.parseInt(St[3+i]);
if(cur>=3*p-2){
ok++;
}else{
if(cur>=incbound)incable++;
}
}
S=S<incable ?S:incable;
ok=ok+S;
return ""+ok;
}
public void printArray(int[] a){
System.out.print(a[0]);
for(int i=1;i<a.length;i++)System.out.append(" "+a[i]);
System.out.println("");
}
public String testCase3() throws IOException{
long n= Long.parseLong(BR.readLine());
if(n==0)return "1";
if(n==1)return "6";
long cur=6;long prev=2;long buf;long n0=n-1;
while(n0>0){
buf= (6*cur-4*prev)%1000;
if(buf<0)buf=(10000+buf)%1000;
prev=cur;cur=buf;
n0--;
// abuse periority of the pairs
if((prev==144)&&(cur==752)&&(n-n0!=4)){
System.out.println(n-n0-4);
n0=n0%(n-n0-4);
}
}
String S= ""+(cur+999)%1000;
while(S.length()<3)S="0"+S;
return S;
}
@SuppressWarnings("fallthrough")
public String testCase4() throws IOException{
String[] SA= BR.readLine().split(" ");
int H = Integer.parseInt(SA[0]);
int W = Integer.parseInt(SA[1]);
int D = Integer.parseInt(SA[2]);
char[][] F = new char[H][];
int startx=-1,starty=-1;
int reflCount =0;
for(int i=0;i<H;i++){
F[i]=BR.readLine().toCharArray();
for(int j = 0;j<W;j++)if(F[i][j]=='X'){
startx =i;starty=j;
F[i][j]='.';
}
}
int iterations;
for(int a2 = 1;a2<=D;a2++)for(int b2=0;b2<D;b2++){
double dit= Math.sqrt(a2*a2+b2*b2);
int iterations2 = (int) Math.floor(((double)D) /dit);
for(int sx2 =-1;sx2<=1;sx2+=2)for(int sy2 =-1;sy2<=1;sy2+=2){
iterations =iterations2;
int gcd = new BigInteger(""+a2).gcd(new BigInteger(""+b2)).intValue();
if(gcd !=1) iterations =0;
//System.out.println("a "+a+" b "+b+" D "+D+" dit "+dit+" it "+iterations2);
int px=startx, py=starty;int sx=sx2;int sy=sy2;
boolean rayLives=true;
int a=a2;int b=b2;
if(sx*sy==-1){int c=a;a=b;b=c;}
//System.out.println("trying "+a*sx +" "+b*sy);
int abuf = -b;int bbuf =-a;
while((iterations>0)&&(rayLives)){
int diff = (abuf + 2*b)-(bbuf +2*a);
if (diff>=0){bbuf +=2*a;}
if (diff<=0){abuf +=2*b;}
diff=diff>0 ?1:(diff<0 ?-1:0);
switch(diff){
case -1:if(F[px+sx][py]=='#'){sx=-sx;}else{px+=sx;}break;
case 1:if(F[px][py+sy]=='#'){sy=-sy;}else{py+=sy;}break;
case 0:if(F[px+sx][py+sy]=='.')
{
px+=sx;py+=sy;
}else{
if((F[px+sx][py]=='.')&&((F[px][py+sy]=='.'))){rayLives=false;}
boolean xbuf =F[px+sx][py]=='#';
if(F[px][py+sy]=='#') {sy=-sy;}else{py+=sy;}
if(xbuf) {sx=-sx;}else{px+=sx;}
} break;
}
//System.out.println(a+" "+b+ " "+raylength+" ");
if((rayLives)&&(abuf ==(2*a-1)*b) && ((bbuf ==(2*b-1)*a)||(b==0))){
abuf=-b;bbuf=-a;
if((px==startx)&&(py==starty)){
reflCount++;rayLives=false;
//System.out.println("Found:" +a+" "+ b+" "+ sx2+" "+sy2);
}
iterations--;
}
}
}
}
return ""+reflCount;
}
public void go(String filename,int exerciseNr){
java.io.File F = new java.io.File(filename);
try{
BR = new BufferedReader(new FileReader(F));
BufferedWriter BW= new BufferedWriter(new FileWriter(new File("output.txt")));
int cases = Integer.parseInt(BR.readLine());
for(int i=0;i<cases;i++){
String S="";
switch(exerciseNr){
case 1: S="Case #"+(i+1)+": "+testCase1(); break;
case 2: S="Case #"+(i+1)+": "+testCase2(); break;
case 3: S="Case #"+(i+1)+": "+testCase3(); break;
case 4: S="Case #"+(i+1)+": "+testCase4(); break;
}
BW.write(S+"\n");
System.out.println(S);
}
BR.close();
BW.close();
}catch(FileNotFoundException e){System.err.println(e);}
catch(IOException e){System.err.println(e);}
}
}
| 0 | 557 |
C20003 | C20037 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class FractionPoint {
public final Fraction x;
public final Fraction y;
public FractionPoint(Fraction x, Fraction y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof FractionPoint) {
FractionPoint fp = (FractionPoint)obj;
return fp.x.equals(x) && fp.y.equals(y);
}
return false;
}
public FractionPoint add(FractionPoint f) {
return new FractionPoint(x.add(f.x), y.add(f.y));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(x);
sb.append(",");
sb.append(y);
sb.append(")");
return sb.toString();
}
}
| 0 | 558 |
C20003 | C20054 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| package jp.funnything.competition.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.apache.commons.io.IOUtils;
public class QuestionReader {
private final BufferedReader _reader;
public QuestionReader( final File input ) {
try {
_reader = new BufferedReader( new FileReader( input ) );
} catch ( final FileNotFoundException e ) {
throw new RuntimeException( e );
}
}
public void close() {
IOUtils.closeQuietly( _reader );
}
public String read() {
try {
return _reader.readLine();
} catch ( final IOException e ) {
throw new RuntimeException( e );
}
}
public BigDecimal[] readBigDecimals() {
return readBigDecimals( " " );
}
public BigDecimal[] readBigDecimals( final String separator ) {
final String[] tokens = readTokens( separator );
final BigDecimal[] values = new BigDecimal[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = new BigDecimal( tokens[ index ] );
}
return values;
}
public BigInteger[] readBigInts() {
return readBigInts( " " );
}
public BigInteger[] readBigInts( final String separator ) {
final String[] tokens = readTokens( separator );
final BigInteger[] values = new BigInteger[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = new BigInteger( tokens[ index ] );
}
return values;
}
public int readInt() {
final int[] values = readInts();
if ( values.length != 1 ) {
throw new IllegalStateException( "Try to read single interger, but the line contains zero or multiple values" );
}
return values[ 0 ];
}
public int[] readInts() {
return readInts( " " );
}
public int[] readInts( final String separator ) {
final String[] tokens = readTokens( separator );
final int[] values = new int[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = Integer.parseInt( tokens[ index ] );
}
return values;
}
public long readLong() {
final long[] values = readLongs();
if ( values.length != 1 ) {
throw new IllegalStateException( "Try to read single interger, but the line contains zero or multiple values" );
}
return values[ 0 ];
}
public long[] readLongs() {
return readLongs( " " );
}
public long[] readLongs( final String separator ) {
final String[] tokens = readTokens( separator );
final long[] values = new long[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = Long.parseLong( tokens[ index ] );
}
return values;
}
public String[] readTokens() {
return readTokens( " " );
}
public String[] readTokens( final String separator ) {
return read().split( separator );
}
}
| 0 | 559 |
C20003 | C20080 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Integer.signum;
import static java.lang.Long.parseLong;
import static java.lang.Math.abs;
import static java.lang.System.exit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class D {
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
static int test;
static void solve() throws Exception {
// System.err.println("TEST");
int n = nextInt();
int m = nextInt();
int d = nextInt();
boolean wall[][] = new boolean[n][m];
int ci = -1, cj = -1;
for (int i = 0; i < n; i++) {
String s = in.readLine();
for (int j = 0; j < m; j++) {
if (s.charAt(j) == 'X') {
ci = i;
cj = j;
}
wall[i][j] = s.charAt(j) == '#';
}
}
int ans = 0;
int djMax = 0;
for (int di = -d; di <= d; di++) {
if (di <= 0) {
while (di * di + djMax * djMax <= d * d) {
++djMax;
}
--djMax;
} else {
while (di * di + djMax * djMax > d * d) {
--djMax;
}
}
p: for (int dj = -djMax; dj <= djMax; dj++) {
if (di == 0 && dj == 0) {
continue;
}
// System.err.println("TRACE " + di + " " + dj);
int i = 0, j = 0;
int cci = ci, ccj = cj;
boolean fi = false, fj = false;
while (true) {
// System.err.print("AT (" + i + " " + j + ") REALLY (" + (fi ? cci - i : cci + i) + " " + (fj ? ccj - j : ccj + j) + ")");
int diff = abs((2 * i + signum(di)) * dj) - abs((2 * j + signum(dj)) * di);
if (diff > 0) {
int nj = j + signum(dj);
// System.err.println(" GO 0 " + signum(dj));
if (wall(wall, cci, ccj, fi, fj, i, nj)) {
ccj = flip(ccj, fj, j, nj);
fj = !fj;
// System.err.println("VERTICAL WALL");
}
j = nj;
} else if (diff < 0) {
int ni = i + signum(di);
// System.err.println(" GO " + signum(di) + " 0");
if (wall(wall, cci, ccj, fi, fj, ni, j)) {
cci = flip(cci, fi, i, ni);
fi = !fi;
// System.err.println("HORIZONTAL WALL");
}
i = ni;
} else {
int ni = i + signum(di);
int nj = j + signum(dj);
// System.err.println(" GO " + signum(di) + " " + signum(dj));
if (wall(wall, cci, ccj, fi, fj, ni, nj)) {
if (wall(wall, cci, ccj, fi, fj, ni, j)) {
if (wall(wall, cci, ccj, fi, fj, i, nj)) {
cci = flip(cci, fi, i, ni);
fi = !fi;
ccj = flip(ccj, fj, j, nj);
fj = !fj;
// System.err.println("GOOD CORNER");
} else {
cci = flip(cci, fi, i, ni);
fi = !fi;
// System.err.println("HORIZONTAL WALL");
}
} else {
if (wall(wall, cci, ccj, fi, fj, i, nj)) {
ccj = flip(ccj, fj, j, nj);
fj = !fj;
// System.err.println("VERTICAL WALL");
} else {
// System.err.println("BAD CORNER");
continue p;
}
}
}
i = ni;
j = nj;
}
int ri = fi ? cci - i : cci + i;
int rj = fj ? ccj - j : ccj + j;
if (i == di && j == dj) {
if (ri == ci && rj == cj) {
// System.err.println("IMAGE " + di + " " + dj);
++ans;
} else {
// System.err.println("NOTHING " + di + " " + dj);
}
break;
}
if (ri == ci && rj == cj && i * dj == j * di) {
// System.err.println("BAD PERSON");
continue p;
}
}
}
}
printCase();
out.println(ans);
}
static boolean wall(boolean wall[][], int ci, int cj, boolean fi, boolean fj, int i, int j) {
return wall[fi ? ci - i : ci + i][fj ? cj - j : cj + j];
}
static int flip(int c, boolean f, int x, int nx) {
return f ? c - x - nx : c + x + nx;
}
static void printCase() {
out.print("Case #" + test + ": ");
}
static void printlnCase() {
out.println("Case #" + test + ":");
}
static int nextInt() throws IOException {
return parseInt(next());
}
static long nextLong() throws IOException {
return parseLong(next());
}
static double nextDouble() throws IOException {
return parseDouble(next());
}
static String next() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
public static void main(String[] args) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
int tests = nextInt();
for (test = 1; test <= tests; test++) {
solve();
}
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
} | 0 | 560 |
C20003 | C20084 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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());
}
}
} | 0 | 561 |
C20003 | C20027 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* 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);
}
}
| 0 | 562 |
C20003 | C20009 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 563 |
C20003 | C20060 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 564 |
C20003 | C20004 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 565 |
C20003 | C20005 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 566 |
C20003 | C20065 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 567 |
C20003 | C20006 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 568 |
C20003 | C20067 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 569 |
C20003 | C20008 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 570 |
C20003 | C20063 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 571 |
C20003 | C20015 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 572 |
C20003 | C20050 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 573 |
C20003 | C20085 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 574 |
C20003 | C20020 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 575 |
C20003 | C20062 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 576 |
C20003 | C20059 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 577 |
C20003 | C20064 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 578 |
C20003 | C20076 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 579 |
C20003 | C20026 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 580 |
C20003 | C20078 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 581 |
C20003 | C20057 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 582 |
C20003 | C20030 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| /*
* 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 | 583 |
C20003 | C20002 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 584 |
C20003 | C20056 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 585 |
C20003 | C20018 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 586 |
C20003 | C20051 | import java.io.*;
import java.util.*;
public class D {
public static class Pair {
public int r;
public int c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair p = (Pair) o;
if (this.r == p.r && this.c == p.c)
return true;
else
return false;
}
}
static char[][] d = new char[30][];
static int H;
static int W;
static int D;
static int row;
static int column;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader("D.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D.out")));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; ++ i) {
String st = in.readLine();
String[] input = st.split(" ");
H = Integer.parseInt(input[0]);
W = Integer.parseInt(input[1]);
D = Integer.parseInt(input[2]);
for (int r = 0; r < H; ++ r)
d[r] = in.readLine().toCharArray();
for (int r = 0; r < H; ++ r)
for (int c = 0; c < W; ++ c)
if (d[r][c] == 'X') {
row = r;
column = c;
}
List<Pair> s = new ArrayList<Pair>();
int ans = 0;
for (int r = 1; r < D; ++ r) {
for (int c = 1; c < D; ++ c) {
int gcd = gcd(r, c);
int dr = r / gcd;
int dc = c / gcd;
Pair p = new Pair(dr, dc);
if (!s.contains(p)) {
s.add(p);
if (mapSearch(dr, dc, 1, 1))
++ ans;
if (mapSearch(dr, dc, 1, -1))
++ ans;
if (mapSearch(dr, dc, -1, 1))
++ ans;
if (mapSearch(dr, dc, -1, -1))
++ ans;
}
}
}
if (lineSearch(1, 0))
++ ans;
if (lineSearch(-1, 0))
++ ans;
if (lineSearch(0, 1))
++ ans;
if (lineSearch(0, -1))
++ ans;
out.println("Case #" + (i + 1) + ": " + ans);
}
in.close();
out.close();
}
private static boolean mapSearch(int dr, int dc, int dx, int dy) {
int x = 0;
int y = 0;
int posX = row;
int posY = column;
while(Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(D, 2)) {
if((x + 0.5) * dr < (y + 0.5) * dc) {
posX += dx;
x += 1;
if(d[posX][posY] == '#'){
dx = -dx;
posX += dx;
}
}
else if((x + 0.5) * dr > (y + 0.5) * dc) {
posY += dy;
y += 1;
if(d[posX][posY] == '#'){
dy = -dy;
posY += dy;
}
}
else {
x += 1;
y += 1;
posX += dx;
posY += dy;
if(d[posX][posY]=='#'){
if(d[posX - dx][posY] == '#' && d[posX][posY - dy] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
else if(d[posX - dx][posY] == '#'){
dy = -dy;
posY += dy;
}
else if(d[posX][posY - dy] == '#'){
dx = -dx;
posX += dx;
}
else{
return false;
}
}
}
if(d[posX][posY] == 'X' && y * dc == x * dr
&& Math.pow(x,2) + Math.pow(y,2) <= Math.pow(D,2)) {
return true;
}
}
return false;
}
public static boolean lineSearch(int dx, int dy){
int posX = row;
int posY = column;
for(int i = 0; i < D; ++ i){
posX += dx;
posY += dy;
if(d[posX][posY] == '#'){
dx = -dx;
dy = -dy;
posX += dx;
posY += dy;
}
if(d[posX][posY] == 'X'){
return true;
}
}
return false;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| 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 | 587 |
C20029 | C20040 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| package jp.funnything.competition.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.apache.commons.io.IOUtils;
public class QuestionReader {
private final BufferedReader _reader;
public QuestionReader( final File input ) {
try {
_reader = new BufferedReader( new FileReader( input ) );
} catch ( final FileNotFoundException e ) {
throw new RuntimeException( e );
}
}
public void close() {
IOUtils.closeQuietly( _reader );
}
public String read() {
try {
return _reader.readLine();
} catch ( final IOException e ) {
throw new RuntimeException( e );
}
}
public BigDecimal[] readBigDecimals() {
return readBigDecimals( " " );
}
public BigDecimal[] readBigDecimals( final String separator ) {
final String[] tokens = readTokens( separator );
final BigDecimal[] values = new BigDecimal[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = new BigDecimal( tokens[ index ] );
}
return values;
}
public BigInteger[] readBigInts() {
return readBigInts( " " );
}
public BigInteger[] readBigInts( final String separator ) {
final String[] tokens = readTokens( separator );
final BigInteger[] values = new BigInteger[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = new BigInteger( tokens[ index ] );
}
return values;
}
public int readInt() {
final int[] values = readInts();
if ( values.length != 1 ) {
throw new IllegalStateException( "Try to read single interger, but the line contains zero or multiple values" );
}
return values[ 0 ];
}
public int[] readInts() {
return readInts( " " );
}
public int[] readInts( final String separator ) {
final String[] tokens = readTokens( separator );
final int[] values = new int[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = Integer.parseInt( tokens[ index ] );
}
return values;
}
public long readLong() {
final long[] values = readLongs();
if ( values.length != 1 ) {
throw new IllegalStateException( "Try to read single interger, but the line contains zero or multiple values" );
}
return values[ 0 ];
}
public long[] readLongs() {
return readLongs( " " );
}
public long[] readLongs( final String separator ) {
final String[] tokens = readTokens( separator );
final long[] values = new long[ tokens.length ];
for ( int index = 0 ; index < tokens.length ; index++ ) {
values[ index ] = Long.parseLong( tokens[ index ] );
}
return values;
}
public String[] readTokens() {
return readTokens( " " );
}
public String[] readTokens( final String separator ) {
return read().split( separator );
}
}
| 0 | 588 |
C20029 | C20035 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Surround {
private Square[][] surround;
public Surround(Square[][] surround) {
this.surround = surround;
}
public Square get(int x, int y) {
return surround[y][x];
}
}
| 0 | 589 |
C20029 | C20045 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| 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 | 590 |
C20029 | C20052 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| 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 | 591 |
C20029 | C20073 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| /*
* Main.java
*
* Created on 14.04.2012, 10:03:46
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package codejam12;
import qualification.CodeJamQuali;
/**
*
* @author Besitzer
*/
public class Main {
/**
* @param args the command line arguments
*/
/*public static void main(String[] args) {
char[] C = new char[26];
CodeJamQuali CJQ =new CodeJamQuali();
CJQ.fillDict("our language is impossible to understand","ejp mysljylc kd kxveddknmc re jsicpdrysi",C);
CJQ.fillDict("there are twenty six factorial possibilities","rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd",C);
CJQ.fillDict("so it is okay if you want to just give up","de kr kd eoya kw aej tysr re ujdr lkgc jv",C);
C['z'-'a']='q';
C['q'-'a']='z';
System.out.println("abcdefghijklmnopqrstuvwxyz");
System.out.println(C);
for(int i =0;i<26;i++)if(C[i]=='z')System.out.println("found");
}*/
public static void main(String[] args) {
CodeJamQuali CJQ =new CodeJamQuali();
//CJQ.go("src/qualification/A-small-attempt0.in", 1);
CJQ.go("src/qualification/D-large.in", 4);
//System.out.println(new java.math.BigInteger("2").gcd(java.math.BigInteger.ZERO));
}
}
| 0 | 592 |
C20029 | C20043 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| package jp.funnything.competition.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
public class Packer {
private static void add( final ZipArchiveOutputStream out , final File file , final int pathPrefix ) {
if ( file.isDirectory() ) {
final File[] children = file.listFiles();
if ( children.length > 0 ) {
for ( final File child : children ) {
add( out , child , pathPrefix );
}
} else {
addEntry( out , file , pathPrefix , false );
}
} else {
addEntry( out , file , pathPrefix , true );
}
}
private static void addEntry( final ZipArchiveOutputStream out , final File file , final int pathPrefix , final boolean isFile ) {
try {
out.putArchiveEntry( new ZipArchiveEntry( file.getPath().substring( pathPrefix ) + ( isFile ? "" : "/" ) ) );
if ( isFile ) {
final FileInputStream in = FileUtils.openInputStream( file );
IOUtils.copy( in , out );
IOUtils.closeQuietly( in );
}
out.closeArchiveEntry();
} catch ( final IOException e ) {
throw new RuntimeException( e );
}
}
public static void pack( final File source , final File destination ) {
try {
final ZipArchiveOutputStream out = new ZipArchiveOutputStream( destination );
add( out , source , FilenameUtils.getPath( source.getPath() ).length() );
out.finish();
out.close();
} catch ( final IOException e ) {
throw new RuntimeException( e );
}
}
}
| 0 | 593 |
C20029 | C20083 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class QuestionD {
public static void doPuzzle() {
try {
File questionfile = new File("D.in");
BufferedReader questionreader = new BufferedReader(new FileReader(questionfile));
File answerfile = new File("D.out");
PrintWriter answerwriter = new PrintWriter(new BufferedWriter(new FileWriter(answerfile)));
String[] params = null;
String question = questionreader.readLine();
int T = Integer.parseInt(question);
int[] A = new int[T];
int[] B = new int[T];
for (int i = 0; i < T; i++) {
question = questionreader.readLine();
params = question.split(" ");
int H = Integer.parseInt(params[0]);
int W = Integer.parseInt(params[1]);
int D = Integer.parseInt(params[2]);
String[] M = new String[H];
for (int j = 0; j < H; j++) M[j] = questionreader.readLine();
answerwriter.println("Case #" + (i+1) + ": " + analyze(H, W, D, M));
}
answerwriter.close();
questionreader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
}
private static int analyze(int H, int W, int D, String[] M) {
int CX = 0;
int CY = 0;
char[][] realMap = new char[H][W];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char c = M[i].charAt(j);
realMap[i][j] = c;
if (c == 'X') {
CX = i;
CY = j;
}
}
}
boolean[][] virtualMap = new boolean[D*2+1][D*2+1];
int answer = 0;
for (int i = 0; i < D; i++) {
for (int j = 0; j <= D*2; j++) {
if (checkMirror(H, W, D, CX, CY, realMap, virtualMap, i, j)) answer++;
}
}
{
int i = D;
for (int j = 0; j < D; j++) {
if (checkMirror(H, W, D, CX, CY, realMap, virtualMap, i, j)) answer++;
}
for (int j = D*2; j > D; j--) {
if (checkMirror(H, W, D, CX, CY, realMap, virtualMap, i, j)) answer++;
}
}
for (int i = D*2; i > D; i--) {
for (int j = 0; j <= D*2; j++) {
if (checkMirror(H, W, D, CX, CY, realMap, virtualMap, i, j)) answer++;
}
}
return answer;
}
private static int[] delta = { 1, -1 };
private static int[] reverse = { 1, 0 };
private static boolean checkMirror(int H, int W, int D, int CX, int CY, char[][] realMap, boolean[][] virtualMap, int PX, int PY) {
if (virtualMap[PX][PY]) return false;
int dx = Math.abs(PX - D);
int dy = Math.abs(PY - D);
if ((D * D) < (dx * dx + dy * dy)) return false;
dx *= 2;
dy *= 2;
int rx = CX;
int ry = CY;
int drx = (PX > D) ? 0 : 1;
int dry = (PY > D) ? 0 : 1;
int dpx = drx;
int dpy = dry;
if (dx > dy) {
for (int x = 1, oy = 0; x <= dx; x++) {
int y = (dy * x) / dx;
if ((((dy * x) % dx) == 0) && ((x % 2) == 0) && ((y % 2) == 0)) {
//å°éãã©ã°
if (virtualMap[D+(x/2)*delta[dpx]][D+(y/2)*delta[dpy]]) return false;
virtualMap[D+(x/2)*delta[dpx]][D+(y/2)*delta[dpy]] = true;
//åå¤å®
if (realMap[rx][ry] == 'X') return true;
oy = y;
continue;
} else if (y > oy) {
oy = y;
if (((dy * x) % dx) == 0) {
if (((x % 2) == 1) && ((y % 2) == 1)) {
//è§åå°
if (realMap[rx+delta[drx]][ry+delta[dry]] != '#') {
rx += delta[drx];
ry += delta[dry];
} else {
if (realMap[rx+delta[drx]][ry] != '#') rx += delta[drx];
else drx = reverse[drx];
if (realMap[rx][ry+delta[dry]] != '#') ry += delta[dry];
else dry = reverse[dry];
}
continue;
}
}
if ((y % 2) == 1) {
//yåå°
if (realMap[rx][ry+delta[dry]] != '#') ry += delta[dry];
else dry = reverse[dry];
}
}
if ((x % 2) == 1) {
//xåå°
if (realMap[rx+delta[drx]][ry] != '#') rx += delta[drx];
else drx = reverse[drx];
}
}
} else {
for (int y = 1, ox = 0; y <= dy; y++) {
int x = (dx * y) / dy;
if ((((dx * y) % dy) == 0) && ((x % 2) == 0) && ((y % 2) == 0)) {
//å°éãã©ã°
if (virtualMap[D+(x/2)*delta[dpx]][D+(y/2)*delta[dpy]]) return false;
virtualMap[D+(x/2)*delta[dpx]][D+(y/2)*delta[dpy]] = true;
//åå¤å®
if (realMap[rx][ry] == 'X') return true;
ox = x;
continue;
} else if (x > ox) {
ox = x;
if (((dx * y) % dy) == 0) {
if (((x % 2) == 1) && ((y % 2) == 1)) {
//è§åå°
if (realMap[rx+delta[drx]][ry+delta[dry]] != '#') {
rx += delta[drx];
ry += delta[dry];
} else {
if (realMap[rx+delta[drx]][ry] != '#') rx += delta[drx];
else drx = reverse[drx];
if (realMap[rx][ry+delta[dry]] != '#') ry += delta[dry];
else dry = reverse[dry];
}
continue;
}
}
if ((x % 2) == 1) {
//xåå°
if (realMap[rx+delta[drx]][ry] != '#') rx += delta[drx];
else drx = reverse[drx];
}
}
if ((y % 2) == 1) {
//yåå°
if (realMap[rx][ry+delta[dry]] != '#') ry += delta[dry];
else dry = reverse[dry];
}
}
}
return false;
}
}
| 0 | 594 |
C20029 | C20023 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| package template;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class TestCase {
private boolean isSolved;
private Object solution;
private Map<String, Integer> intProperties;
private Map<String, ArrayList<Integer>> intArrayProperties;
private Map<String, ArrayList<ArrayList<Integer>>> intArray2DProperties;
private Map<String, Double> doubleProperties;
private Map<String, ArrayList<Double>> doubleArrayProperties;
private Map<String, ArrayList<ArrayList<Double>>> doubleArray2DProperties;
private Map<String, String> stringProperties;
private Map<String, ArrayList<String>> stringArrayProperties;
private Map<String, ArrayList<ArrayList<String>>> stringArray2DProperties;
private Map<String, Boolean> booleanProperties;
private Map<String, ArrayList<Boolean>> booleanArrayProperties;
private Map<String, ArrayList<ArrayList<Boolean>>> booleanArray2DProperties;
private Map<String, Long> longProperties;
private Map<String, ArrayList<Long>> longArrayProperties;
private Map<String, ArrayList<ArrayList<Long>>> longArray2DProperties;
private int ref;
private double time;
public TestCase() {
initialise();
}
private void initialise() {
isSolved = false;
intProperties = new HashMap<>();
intArrayProperties = new HashMap<>();
intArray2DProperties = new HashMap<>();
doubleProperties = new HashMap<>();
doubleArrayProperties = new HashMap<>();
doubleArray2DProperties = new HashMap<>();
stringProperties = new HashMap<>();
stringArrayProperties = new HashMap<>();
stringArray2DProperties = new HashMap<>();
booleanProperties = new HashMap<>();
booleanArrayProperties = new HashMap<>();
booleanArray2DProperties = new HashMap<>();
longProperties = new HashMap<>();
longArrayProperties = new HashMap<>();
longArray2DProperties = new HashMap<>();
ref = 0;
}
public void setSolution(Object o) {
solution = o;
isSolved = true;
}
public Object getSolution() {
if (!isSolved) {
Utils.die("getSolution on unsolved testcase");
}
return solution;
}
public void setRef(int i) {
ref = i;
}
public int getRef() {
return ref;
}
public void setTime(double d) {
time = d;
}
public double getTime() {
return time;
}
public void setInteger(String s, Integer i) {
intProperties.put(s, i);
}
public Integer getInteger(String s) {
return intProperties.get(s);
}
public void setIntegerList(String s, ArrayList<Integer> l) {
intArrayProperties.put(s, l);
}
public void setIntegerMatrix(String s, ArrayList<ArrayList<Integer>> l) {
intArray2DProperties.put(s, l);
}
public ArrayList<Integer> getIntegerList(String s) {
return intArrayProperties.get(s);
}
public Integer getIntegerListItem(String s, int i) {
return intArrayProperties.get(s).get(i);
}
public ArrayList<ArrayList<Integer>> getIntegerMatrix(String s) {
return intArray2DProperties.get(s);
}
public ArrayList<Integer> getIntegerMatrixRow(String s, int row) {
return intArray2DProperties.get(s).get(row);
}
public Integer getIntegerMatrixItem(String s, int row, int column) {
return intArray2DProperties.get(s).get(row).get(column);
}
public ArrayList<Integer> getIntegerMatrixColumn(String s, int column) {
ArrayList<Integer> out = new ArrayList();
for(ArrayList<Integer> row : intArray2DProperties.get(s)) {
out.add(row.get(column));
}
return out;
}
public void setDouble(String s, Double i) {
doubleProperties.put(s, i);
}
public Double getDouble(String s) {
return doubleProperties.get(s);
}
public void setDoubleList(String s, ArrayList<Double> l) {
doubleArrayProperties.put(s, l);
}
public void setDoubleMatrix(String s, ArrayList<ArrayList<Double>> l) {
doubleArray2DProperties.put(s, l);
}
public ArrayList<Double> getDoubleList(String s) {
return doubleArrayProperties.get(s);
}
public Double getDoubleListItem(String s, int i) {
return doubleArrayProperties.get(s).get(i);
}
public ArrayList<ArrayList<Double>> getDoubleMatrix(String s) {
return doubleArray2DProperties.get(s);
}
public ArrayList<Double> getDoubleMatrixRow(String s, int row) {
return doubleArray2DProperties.get(s).get(row);
}
public Double getDoubleMatrixItem(String s, int row, int column) {
return doubleArray2DProperties.get(s).get(row).get(column);
}
public ArrayList<Double> getDoubleMatrixColumn(String s, int column) {
ArrayList<Double> out = new ArrayList();
for(ArrayList<Double> row : doubleArray2DProperties.get(s)) {
out.add(row.get(column));
}
return out;
}
public void setString(String s, String t) {
stringProperties.put(s, t);
}
public String getString(String s) {
return stringProperties.get(s);
}
public void setStringList(String s, ArrayList<String> l) {
stringArrayProperties.put(s, l);
}
public void setStringMatrix(String s, ArrayList<ArrayList<String>> l) {
stringArray2DProperties.put(s, l);
}
public ArrayList<String> getStringList(String s) {
return stringArrayProperties.get(s);
}
public String getStringListItem(String s, int i) {
return stringArrayProperties.get(s).get(i);
}
public ArrayList<ArrayList<String>> getStringMatrix(String s) {
return stringArray2DProperties.get(s);
}
public ArrayList<String> getStringMatrixRow(String s, int row) {
return stringArray2DProperties.get(s).get(row);
}
public String getStringMatrixItem(String s, int row, int column) {
return stringArray2DProperties.get(s).get(row).get(column);
}
public ArrayList<String> getStringMatrixColumn(String s, int column) {
ArrayList<String> out = new ArrayList();
for(ArrayList<String> row : stringArray2DProperties.get(s)) {
out.add(row.get(column));
}
return out;
}
public void setBoolean(String s, Boolean b) {
booleanProperties.put(s, b);
}
public Boolean getBoolean(String s) {
return booleanProperties.get(s);
}
public void setBooleanList(String s, ArrayList<Boolean> l) {
booleanArrayProperties.put(s, l);
}
public void setBooleanMatrix(String s, ArrayList<ArrayList<Boolean>> l) {
booleanArray2DProperties.put(s, l);
}
public ArrayList<Boolean> getBooleanList(String s) {
return booleanArrayProperties.get(s);
}
public Boolean getBooleanListItem(String s, int i) {
return booleanArrayProperties.get(s).get(i);
}
public ArrayList<ArrayList<Boolean>> getBooleanMatrix(String s) {
return booleanArray2DProperties.get(s);
}
public ArrayList<Boolean> getBooleanMatrixRow(String s, int row) {
return booleanArray2DProperties.get(s).get(row);
}
public Boolean getBooleanMatrixItem(String s, int row, int column) {
return booleanArray2DProperties.get(s).get(row).get(column);
}
public ArrayList<Boolean> getBooleanMatrixColumn(String s, int column) {
ArrayList<Boolean> out = new ArrayList();
for(ArrayList<Boolean> row : booleanArray2DProperties.get(s)) {
out.add(row.get(column));
}
return out;
}
public void setLong(String s, Long b) {
longProperties.put(s, b);
}
public Long getLong(String s) {
return longProperties.get(s);
}
public void setLongList(String s, ArrayList<Long> l) {
longArrayProperties.put(s, l);
}
public void setLongMatrix(String s, ArrayList<ArrayList<Long>> l) {
longArray2DProperties.put(s, l);
}
public ArrayList<Long> getLongList(String s) {
return longArrayProperties.get(s);
}
public Long getLongListItem(String s, int i) {
return longArrayProperties.get(s).get(i);
}
public ArrayList<ArrayList<Long>> getLongMatrix(String s) {
return longArray2DProperties.get(s);
}
public ArrayList<Long> getLongMatrixRow(String s, int row) {
return longArray2DProperties.get(s).get(row);
}
public Long getLongMatrixItem(String s, int row, int column) {
return longArray2DProperties.get(s).get(row).get(column);
}
public ArrayList<Long> getLongMatrixColumn(String s, int column) {
ArrayList<Long> out = new ArrayList();
for(ArrayList<Long> row : longArray2DProperties.get(s)) {
out.add(row.get(column));
}
return out;
}
}
| 0 | 595 |
C20029 | C20086 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| //input file must be in.txt in this directory
//output file will be out.txt
import java.io.*;
import java.util.*;
public class D
{
public static Scanner in;
public static PrintStream out;
public static void main(String [] args) throws Throwable
{
in = new Scanner(new File("in.txt"));
int cases = in.nextInt();
in.nextLine();
out = new PrintStream(new File("out.txt"));
for (int i = 1; i <= cases; i++)
{
out.print("Case #" + i + ": ");
printResult();
out.println();
}
}
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static boolean canSeeImage(int dx, int dy, int lx, int ly, int d, boolean [][] house)
{
//System.out.println("Trying (" + dx + "," + dy + "):");
int cx = lx;
int cy = ly;
int sx, sy;
if (dx > 0)
sx = 1;
else
sx = -1;
if (dy > 0)
sy = 1;
else
sy = -1;
dx = Math.abs(dx);
dy = Math.abs(dy);
int i = 0;
int totx = 0;
int toty = 0;
boolean t;
boolean mx,my;
boolean nx = false,ny = false;
while (totx*totx + toty*toty <= d*d)
{
if (dx == 0)
{
my = true;
mx = false;
t = true;
}
else
{
if (dy == 0)
{
mx = true;
my = false;
t = true;
}
else
{
mx = i % dy == 0;
my = i % dx == 0;
t = mx && my && !nx && !ny;
if (mx)
{
mx = nx;
nx = !nx;
}
if (my)
{
my = ny;
ny = !ny;
}
}
}
if (t && totx+toty > 0 && cx == lx && cy == ly)
return true;
//if (mx || my)
// printHouse(lx,ly,cx,cy,house);
if (mx)
totx++;
if (my)
toty++;
if (mx && my)
{
if (house[cx + sx][cy + sy])
{
if (house[cx][cy + sy])
{
if (house[cx + sx][cy])
{
sx = -sx;
sy = -sy;
}
else
{
sy = -sy;
cx += sx;
}
}
else
{
if (house[cx + sx][cy])
{
sx = -sx;
cy += sy;
}
else
{
return false;
}
}
}
else
{
cx += sx;
cy += sy;
}
}
else
{
if (mx)
{
if (house[cx + sx][cy])
{
sx = -sx;
}
else
{
cx += sx;
}
}
if (my)
{
if (house[cx][cy + sy])
{
sy = -sy;
}
else
{
cy += sy;
}
}
}
i++;
}
return false;
}
public static void printHouse(int lx, int ly, int cx, int cy, boolean [][] house)
{
for (int y = 0; y < house[0].length; y++)
{
for (int x = 0; x < house.length; x++)
{
if (x == cx && y == cy)
{
System.out.print('*');
}
else if (x == lx && y == ly)
{
System.out.print('X');
}
else if (house[x][y])
{
System.out.print('#');
}
else
{
System.out.print(' ');
}
}
System.out.println();
}
}
public static int[][] getPossibilities(int d)
{
ArrayList<Integer> lx = new ArrayList<Integer>();
ArrayList<Integer> ly = new ArrayList<Integer>();
int h, v;
for (int x = -d; x <= d; x++)
{
for (int y = -d; y <= d; y++)
{
h = Math.abs(x);
v = Math.abs(y);
if (gcd(h,v) == 1 && h*h + v*v <= d*d)
{
lx.add(x);
ly.add(y);
}
}
}
int [][] possibilities = new int [lx.size()][2];
for (int i = 0; i < possibilities.length; i++)
{
possibilities[i][0] = lx.get(i);
possibilities[i][1] = ly.get(i);
}
return possibilities;
}
public static void printResult()
{
int h,w,d,lx = 0,ly = 0;
h = in.nextInt();
w = in.nextInt();
d = in.nextInt();
in.nextLine();
String s;
boolean [][] house = new boolean[w][h];
for (int y = 0; y < h; y++)
{
s = in.nextLine();
for (int x = 0; x < w; x++)
{
house[x][y] = s.charAt(x) == '#';
if (s.charAt(x) == 'X')
{
lx = x;
ly = y;
}
}
}
int [][] poss = getPossibilities(d);
int images = 0;
for (int i = 0; i < poss.length; i++)
{
if (canSeeImage(poss[i][0], poss[i][1], lx, ly, d, house))
{
images++;
//System.out.println("True");
}
//else
//System.out.println("False");
}
out.print(images);
}
}
| 0 | 596 |
C20029 | C20075 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashSet;
import java.util.Scanner;
public class CodeJamD
{
public static void main(String args[]) throws Exception
{
Scanner in = new Scanner(new File("in.txt"));
BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"));
int cases = in.nextInt();
for(int casenum = 1;casenum <= cases;casenum++)
{
int H = in.nextInt();
int W = in.nextInt();
int D = in.nextInt();
in.nextLine();
in.nextLine();
boolean x[][] = new boolean[2 * (H - 2)][2 * (W - 2)];
int R = 0,C = 0;
for(int n = 0;n < H - 2;n++)
{
String str = in.nextLine();
for(int i = 0;i < W - 2;i++)
{
boolean b = false;
if(str.charAt(i + 1) == 'X')
{
R = n;
C = i;
b = true;
}
x[n][i] = b;
x[x.length - 1 - n][i] = b;
x[n][x[0].length - 1 - i] = b;
x[x.length - 1 - n][x[0].length - 1 - i] = b;
}
}
in.nextLine();
int count = 0;
HashSet<String> set = new HashSet<String>();
for(int a = -D;a <= D;a++)
{
for(int b = -D;b <= D;b++)
{
if(a * a + b * b > D * D)
continue;
if(a == 0 && b == 0)
continue;
if(x[(R + a + D * (x.length)) % (x.length)][(C + b + D * (x[0].length)) % (x[0].length)])
{
int gcf = gcf(a,b);
int a2 = a/gcf;
int b2 = b/gcf;
String s = a2 + " " + b2;
if(!set.contains(s))
{
set.add(s);
count++;
}
}
}
}
out.write("Case #" + casenum + ": " + count + "\n");
}
in.close();
out.close();
}
public static int gcf(int a,int b)
{
if(a<0)
a = -a;
if(b<0)
b = -b;
if (b == 0) return a;
else return (gcf(b, a % b));
}
} | 0 | 597 |
C20029 | C20048 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| 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 | 598 |
C20029 | C20074 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class Ray {
private final Fraction expiryLengthSquared;
public RayVector vector;
private FractionPoint currentLocation;
private Fraction xTravelled;
private Fraction yTravelled;
public Ray(int expiryLengthSquared, RayVector vector,
FractionPoint startLocation) {
this.expiryLengthSquared = new Fraction(expiryLengthSquared);
this.vector = vector;
this.currentLocation = startLocation;
xTravelled = new Fraction(0, 1);
yTravelled = new Fraction(0, 1);
}
public static FractionPoint locationChangeInSq(FractionPoint enter, RayVector v) {
Fraction xFrac = enter.x.fractionalPart();
Fraction yFrac = enter.y.fractionalPart();
Fraction xLen;
Fraction yLen;
if (xFrac.n == 0) {
xLen = new Fraction(1);
yLen = yFrac.n == 0 ? new Fraction(1) :
(v.y < 0 ? yFrac : new Fraction(1).substract(yFrac));
}
else if (yFrac.n == 0) {
yLen = new Fraction(1);
xLen = xFrac.n == 0 ? new Fraction(1) :
(v.x < 0 ? xFrac : new Fraction(1).substract(xFrac));
}
else {
yLen = v.y < 0 ? yFrac: new Fraction(1).substract(yFrac);
xLen = v.x < 0 ? xFrac: new Fraction(1).substract(xFrac);
}
Fraction scalar;
if (xLen.multiply(v.getScalarGardient()).compareTo(yLen) > 0)
scalar = yLen.divide(Math.abs(v.y));
else
scalar = xLen.divide(Math.abs(v.x));
FractionPoint fp =
new FractionPoint(scalar.multiply(v.x), scalar.multiply(v.y));
return fp;
}
public FractionPoint locationChangeInSq() {
return locationChangeInSq(currentLocation, vector);
}
public boolean createsReflection(Hall h) {
// System.out.println("\nRay: " + vector.toString());
Boolean b = null;
while (b == null)
b = step(h);
// System.out.println(b);
return b;
}
public Boolean step(Hall h) {
FractionPoint locationChange = locationChangeInSq();
if (locationChange.x.isInt() || locationChange.y.isInt()) {
FractionPoint changeToMid = new FractionPoint(
locationChange.x.divide(2),
locationChange.y.divide(2));
FractionPoint midpoint = currentLocation.add(changeToMid);
if (midpoint.equals(h.meLocation)) {
xTravelled = xTravelled.add(changeToMid.x.abs());
yTravelled = yTravelled.add(changeToMid.y.abs());
return stillGoing();
}
}
currentLocation = currentLocation.add(locationChange);
vector = h.processBoundary(currentLocation, vector);
if (vector == null)
return false;
xTravelled = xTravelled.add(locationChange.x.abs());
yTravelled = yTravelled.add(locationChange.y.abs());
// System.out.println("Chng: " + locationChange);
// System.out.println("Loc: " + currentLocation);
// System.out.println("d: " + distanceTravelled());
if (!stillGoing())
return false;
else
return null;
}
private Fraction distanceTravelled() {
Fraction f = xTravelled.multiply(xTravelled).add(
yTravelled.multiply(yTravelled));
return f;
}
private boolean stillGoing() {
int compare = distanceTravelled().compareTo(
expiryLengthSquared);
return compare <= 0;
}
public FractionPoint getCurrentLocation() {
return currentLocation;
}
}
| package codejam2012.q;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
public class D
{
static int H, W, D;
static char[][] hall;
static int startx, starty;
static public int test(int dx, int dy, int dirx, int diry)
{
//System.out.println("test " + dx + "/" + dy + " " + dirx + "/" + diry);
int hx = startx, hy = starty;
int rx = dy, ry = dx;
int tx = 0, ty = 0;
while( tx*tx + ty*ty <= D*D ){
if( hx==startx && hy==starty && tx+ty>0 ){
if( rx==2*dy && ry==dx+dy )
return 1;
if( ry==2*dx && rx==dy+dx )
return 1;
}
if( rx < ry ){
ry -= rx;
rx = 2*dy;
if( hall[hx+dirx][hy]=='#' ){
dirx = -dirx;
} else {
hx += dirx;
}
tx++;
} else
if( rx > ry ){
rx -= ry;
ry = 2*dx;
if( hall[hx][hy+diry]=='#' ){
diry = -diry;
} else {
hy += diry;
}
ty++;
} else
if( rx==ry ){
rx = 2*dy;
ry = 2*dx;
if( hall[hx+dirx][hy+diry]!='#' ){
hx += dirx;
hy += diry;
} else
if( hall[hx+dirx][hy]!='#' && hall[hx][hy+diry]!='#' ){
return 0;
} else {
if( hall[hx+dirx][hy]=='#' ){
dirx = -dirx;
} else {
hx += dirx;
}
if( hall[hx][hy+diry]=='#' ){
diry = -diry;
} else {
hy += diry;
}
}
tx++;
ty++;
}
}
return 0;
}
static public int multiTest(int x0, int y0, int x1, int y1)
{
//System.out.println("multitest " + x0 + "/" + y0 + " " + x1 + "/" + y1) ;
int count = 0;
int x2 = x0+x1;
int y2 = y0+y1;
if( x2*x2 + y2*y2 <= D*D ){
count += multiTest(x0, y0, x2, y2);
count += test(x2, y2, +1, +1);
count += test(x2, y2, +1, -1);
count += test(x2, y2, -1, +1);
count += test(x2, y2, -1, -1);
count += multiTest(x2, y2, x1, y1);
}
return count;
}
static public void solveOne(int caseNo, Scanner in, PrintStream out)
{
H = in.nextInt();
W = in.nextInt();
D = in.nextInt();
in.nextLine();
hall = new char[W][H];
for( int y=0 ; y<H ; y++ ){
String line = in.nextLine();
for( int x=0 ; x<W ; x++ ){
hall[x][y] = line.charAt(x);
if( hall[x][y]=='X' ){
startx = x;
starty = y;
}
}
}
int count = 0;
count += test(1, 0, +1, 0);
count += test(1, 0, -1, 0);
count += test(0, 1, 0, +1);
count += test(0, 1, 0, -1);
count += multiTest(1,0,0,1);
String ans = "Case #" + caseNo + ": " + count;
System.out.println(ans);
if( out!=null ){
out.println(ans);
}
}
static public void solveAll(String infilename, String outfilename)
{
try {
FileInputStream stream = new FileInputStream(infilename);
Scanner in = new Scanner(stream);
PrintStream out = null;
if( outfilename!=null ){
out = new PrintStream(outfilename);
System.out.println("--- " + outfilename);
}
int nbcases = in.nextInt();
in.nextLine();
for( int n=1 ; n<=nbcases ; n++ ){
solveOne(n, in, out);
} // for n
} catch( IOException ex ){
ex.printStackTrace();
}
}
public static void main(String[] args)
{
if( args.length==0 ){
String cname = D.class.getName().replaceAll("\\.", "/");
String path = "src/" + cname + "-sample";
// String path = "src/" + cname + "-small-attempt0";
// String path = "src/" + cname + "-large";
solveAll(path + ".in.txt", path + ".out");
} else {
String dir = "src/" + D.class.getPackage().getName().replaceAll("\\.", "/") + "/";
String path = dir + args[0];
solveAll(path + ".in.txt", path + ".out");
}
}
}
| 0 | 599 |