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
C20082
C20025
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
package j2012qualifier; import java.awt.Point; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class D { public static String inputDirectory="src/j2012qualifier/"; public static String inputFile="D.in"; public static String outputFile="D.out"; public static PrintWriter output; public static char[][] room; public static void main(String[] args) throws FileNotFoundException{ Scanner s=new Scanner(new File(inputDirectory + inputFile)); output=new PrintWriter(new File(inputDirectory + outputFile)); int cases = s.nextInt(); //size of each square s.nextLine(); for(int Case=1;Case<=cases;Case++){ int H = s.nextInt(); int W = s.nextInt(); int D = s.nextInt(); s.nextLine(); room = new char[H][W]; int x=0, y=0; for(int i=0;i<H;i++){ String line = s.nextLine(); for(int j=0;j<W;j++){ room[i][j] = line.charAt(j); if (room[i][j] == 'X') { x = j; y = i; } } } int count = 0; Set<String> used = new HashSet<String>(); for(int i=-D; i<=D ;i++) { for(int j=-D; j<=D; j++) { int dx = j; int dy = i; if (dx == 0 && dy == 0) continue; if (dx == 0) dy = (int)(dy / Math.sqrt(dy * dy)); if (dy == 0) dx = (int)(dx / Math.sqrt(dx * dx)); if(i != 0 && j!=0) { int gcd = GCD(i, j); gcd = gcd < 0 ? -gcd : gcd; dx = j / gcd; dy = i / gcd; } String key = dx+","+dy; if (used.contains(key))continue; used.add(key); int lcm = dx*dy; if ( dx == 0) { lcm = dy; } else if(dy == 0){ lcm = dx; } lcm = lcm < 0 ? -lcm : lcm; int startX = (2*x+1)*lcm; int startY = (2*y+1)*lcm; if(castRay(lcm, D * lcm * 2, startX, startY, dx, dy, startX, startY)) { count++; } } } output.printf("Case #%d: %d\n", Case, count); } output.flush(); } public static int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } public static boolean castRay(int lcm, double D, int x, int y, int dx, int dy, int gx, int gy) { //out(D+ " : ("+x+","+y+") <"+dx+","+dy+">"); if (D<=0) { return false; } int xSteps=1000, ySteps=1000; if (dx > 0) { xSteps = ((x / lcm) * lcm + lcm - x) / dx; } else if(dx < 0) { xSteps = (((x - 1) / lcm) * lcm - x) / dx; } if (dy > 0) { ySteps = ((y / lcm) * lcm + lcm - y) / dy; } else if(dy < 0) { ySteps = (((y - 1) / lcm) * lcm - y) / dy; } int steps = Math.min(xSteps, ySteps); double distance = steps * Math.sqrt(dx*dx + dy*dy); if (distance > D) { return false; } int newX = x+dx*steps; int newY = y+dy*steps; if (newX == gx && newY == gy) { return true; } //we are on a vertical line boolean onVertical = (newX %(2*lcm) == 0); boolean onHorizontal = (newY %(2*lcm) == 0); int gridY = newY / (2 * lcm); int gridX = newX / (2 * lcm); int newDx = dx; int newDy = dy; if (onVertical && onHorizontal) { int tX = (dx < 0) ? gridX - 1 : gridX; int tY = (dy < 0) ? gridY - 1 : gridY; int cX = (tX == gridX) ? gridX - 1 : gridX; int cY = (tY == gridY) ? gridY - 1 : gridY; if(room[tY][tX] == '#') { if(room[tY][cX] != '#' && room[cY][tX] != '#') { //light destroyed return false; } if (room[tY][cX] == '#'){ newDy = -dy; } if (room[cY][tX] == '#'){ newDx = -dx; } } } else if(onVertical) { gridX += (dx < 0) ? -1 : 0; if(room[gridY][gridX] == '#') { newDx = -dx; } } else if(onHorizontal) { gridY += (dy < 0) ? -1 : 0; if(room[gridY][gridX] == '#') { newDy = -dy; } } return castRay(lcm, D - distance, newX, newY, newDx, newDy, gx, gy); } public static void out(String s){ output.println(s); System.out.println(s); } }
0
0
C20082
C20012
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
public class CGConst { public static final int INF = 99999999; public static final double EPS = 1e-9; public static final double pi = Math.PI; }
0
1
C20082
C20069
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
package com.brootdev.gcj2012.common; import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; public class DataUtils { public static int readIntLine(BufferedReader in) throws IOException { return Integer.valueOf(in.readLine()); } public static long readLongLine(BufferedReader in) throws IOException { return Long.valueOf(in.readLine()); } public static int[] readIntsArrayLine(BufferedReader in) throws IOException { String[] numsS = in.readLine().split("\\s+"); int[] nums = new int[numsS.length]; for (int i = 0; i < nums.length; i++) { nums[i] = Integer.valueOf(numsS[i]); } return nums; } public static long[] readLongsArrayLine(BufferedReader in) throws IOException { String[] numsS = in.readLine().split("\\s+"); long[] nums = new long[numsS.length]; for (int i = 0; i < nums.length; i++) { nums[i] = Long.valueOf(numsS[i]); } return nums; } public static void writeCaseHeader(PrintWriter out, long case_) { out.print("Case #"); out.print(case_ + 1); out.print(": "); } }
0
2
C20082
C20039
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
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
3
C20082
C20031
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
/* * 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; } }
0
4
C20082
C20003
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
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); } }
0
5
C20082
C20029
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
/* * 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
6
C20082
C20040
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
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
7
C20082
C20035
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
/* * 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
8
C20082
C20045
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
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
9
C20082
C20052
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
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
10
C20082
C20073
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
/* * 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
11
C20082
C20043
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
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
12
C20082
C20083
import java.util.*; import java.io.*; class Frac { public static int gcd(int u, int v) { while (v != 0) { int t = v; v = u % v; u = t; } return Math.abs(u); } public int n; public int d; public Frac(int n, int d) { int dd = gcd(n, d); this.n = n / dd; this.d = d / dd; } public Frac add(Frac other) { int tempn = this.n * other.d + other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac sub(Frac other) { int tempn = this.n * other.d - other.n * this.d; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac mul(Frac other) { int tempn = this.n * other.n; int tempd = this.d * other.d; return new Frac(tempn, tempd); } public Frac div(Frac other) { int tempn = this.n * other.d; int tempd = this.d * other.n; return new Frac(tempn, tempd); } public double doubl() { return ((double) this.n) / this.d; } public boolean eq(Frac other) { return this.n == other.n && this.d == other.d; } public String toString() { return String.format("%d/%d", n, d); } } class Grid { private int[] grid; public int xsize; public int ysize; public int xstart = 0; public int ystart = 0; public Grid(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; grid = new int[xsize * ysize]; } public int get(int x, int y) { return grid[x + y * xsize]; } public void set(int x, int y, int v) { grid[x + y * xsize] = v; } public void write() { for (int y = 0; y < ysize; y++) { for (int x = 0; x < xsize; x++) { System.out.print("" + get(x,y)); } System.out.println(); } } public Grid rotate() { Grid newg = new Grid(ysize, xsize); for (int x = 0; x < xsize; x++) { for (int y = 0; y < ysize; y++) { int v = get(x,y); int newx = ysize - y - 1; int newy = x; newg.set(newx, newy, v); if (v == 2 && (newx % 2) == 1 && (newy % 2) == 1) { newg.xstart = newx; newg.ystart = newy; } } } return newg; } } public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int ncases = sc.nextInt(); for (int caseno = 0; caseno < ncases; caseno++) { int ysize = sc.nextInt() * 2; int xsize = sc.nextInt() * 2; int maxdist = sc.nextInt() * 2; Grid g = new Grid(xsize, ysize); for (int y = 0; y < (ysize / 2); y++) { String row = sc.next(); for (int x = 0; x < (xsize / 2); x++) { if (row.charAt(x) == '#') { g.set(x*2+0,y*2+0,1); g.set(x*2+1,y*2+0,1); g.set(x*2+1,y*2+1,1); g.set(x*2+0,y*2+1,1); } else if (row.charAt(x) == 'X') { g.set(x*2+0,y*2+0,2); g.set(x*2+1,y*2+0,2); g.set(x*2+1,y*2+1,2); g.set(x*2+0,y*2+1,2); g.xstart = x * 2 + 1; g.ystart = y * 2 + 1; } } } int count = 0; for (int i = 0; i < 4; i++) { // System.out.println("" + g.xstart); // System.out.println("" + g.ystart); // g.write(); for (int xdiff = 0; xdiff < maxdist+2; xdiff += 2) { for (int ydiff = 2; ydiff < maxdist+2; ydiff += 2) { if (xdiff * xdiff + ydiff * ydiff <= maxdist * maxdist) { boolean res = testray(g.xstart, g.ystart, xdiff, ydiff, g); if (res) count += 1; } } } g = g.rotate(); } System.out.printf("Case #%d: %d\n", caseno+1, count); } } public static boolean testray(int xstartt, int ystartt, int xdifff, int ydifff, Grid g) { //System.out.printf("%d %d %d %d\n", xstartt, ystartt, xdifff, ydifff); int xmirror = 1; int ymirror = 1; int xgrid = xstartt; int ygrid = ystartt; Frac xend = new Frac(xstartt + xdifff, 1); Frac yend = new Frac(ystartt + ydifff, 1); Frac xstart = new Frac(xstartt, 1); Frac ystart = new Frac(ystartt, 1); //System.out.println("" + xstart); Frac xdiff = xend.sub(xstart); Frac ydiff = yend.sub(ystart); Frac xslope = xdiff.div(ydiff); Frac yslope = ydiff.div(xdiff); Frac xpos = xstart; Frac ypos = ystart; while (true) { if (xpos.eq(xend) && ypos.eq(yend)) { break; } int xcorner = xpos.n / xpos.d; int ycorner = ypos.n / ypos.d; Frac yedge = new Frac(ycorner + 1, 1); Frac xres = xpos.add(xslope.mul(yedge.sub(ypos))); Frac xedge = new Frac(xcorner + 1, 1); Frac yres = ypos.add(yslope.mul(xedge.sub(xpos))); double h = (xres.sub(xpos)).doubl(); double v = (xedge.sub(xpos)).doubl(); if (h < v) { xpos = xres; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; if (xmod == 0) { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } else if (g.get(xgrid, ygrid) == 2 && (ycorner % 2) == 1) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } } else { ygrid += ymirror; if (g.get(xgrid, ygrid) == 1) { ymirror *= -1; ygrid += ymirror; } } } else if (v < h) { xpos = xedge; ypos = yres; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; xgrid += xmirror; if (g.get(xgrid, ygrid) == 1) { xmirror *= -1; xgrid += xmirror; } } else { xpos = xedge; ypos = yedge; xcorner = xpos.n / xpos.d; int xmod = xpos.n % xpos.d; ycorner = ypos.n / ypos.d; int ymod = ypos.n % ypos.d; int blockE = g.get(xgrid + xmirror, ygrid); int blockSE = g.get(xgrid + xmirror, ygrid + ymirror); int blockS = g.get(xgrid, ygrid + ymirror); if (blockE == 2 && blockSE == 2 && blockS == 2) { if (xpos.eq(xend) && ypos.eq(yend)) { return true; } else { return false; } } if (blockE == 2) blockE = 0; if (blockSE == 2) blockSE = 0; if (blockS == 2) blockS = 0; if (blockE == 0 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 1 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 0 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; } else if (blockE == 0 && blockSE == 1 && blockS == 0) { return false; } else if (blockE == 1 && blockSE == 1 && blockS == 0) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; xgrid += xmirror; } else if (blockE == 0 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; ymirror *= -1; ygrid += ymirror; } else if (blockE == 1 && blockSE == 1 && blockS == 1) { xgrid += xmirror; ygrid += ymirror; xmirror *= -1; ymirror *= -1; xgrid += xmirror; ygrid += ymirror; } } } return false; } }
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
13

No dataset card yet

New: Create and edit this dataset card directly on the website!

Contribute a Dataset Card
Downloads last month
0
Add dataset card