Last active
April 12, 2017 05:14
-
-
Save vishwakarma/f7b1fb11001bae1e2e3d11a6dc900355 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.StringTokenizer; | |
public class TaskA { | |
private static class InputReader { | |
public BufferedReader reader; | |
public StringTokenizer tokenizer; | |
public InputReader(InputStream stream) { | |
reader = new BufferedReader(new InputStreamReader(stream), 32768); | |
tokenizer = null; | |
} | |
public String next() { | |
while (tokenizer == null || !tokenizer.hasMoreTokens()) { | |
try { | |
tokenizer = new StringTokenizer(reader.readLine()); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
return tokenizer.nextToken(); | |
} | |
public int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
public double nextDouble() { | |
return Double.parseDouble(next()); | |
} | |
} | |
public static void main(String args[]) { | |
InputStream inputStream = System.in; | |
OutputStream outputStream = System.out; | |
TaskA.InputReader in = new InputReader(inputStream); | |
PrintWriter out = new PrintWriter(outputStream); | |
solve(in, out); | |
out.close(); | |
} | |
private static void solve(InputReader in, PrintWriter out) { | |
double xc = in.nextDouble(); | |
double yc = in.nextDouble(); | |
double rc = in.nextDouble(); | |
int numSquares = countSquaresInCircle(xc, yc, rc); | |
out.println(numSquares); | |
} | |
/** | |
runtime complexity is : O(r*r) | |
**/ | |
private static int countSquaresInCircle(double xc, double yc, double rc) { | |
int answer = 0; | |
long leftX = (long) (xc - rc); | |
long topY = (long) (yc + rc); | |
long bottomY = (long) (yc - rc); | |
long rightX = (long) (xc + rc); | |
for (long y = topY; y >= bottomY; y--) { | |
for (long x = leftX; x <= rightX; x++) { | |
if (isValidSquare(x, y, xc, yc, rc)) { | |
answer++; | |
} | |
} | |
} | |
return answer; | |
} | |
private static boolean isValidSquare(long bottomLeftX, long bottomLeftY, double centreX, double centreY, double radius) { | |
double sqrtTwo = Math.sqrt(2); | |
long sqX1 = bottomLeftX; | |
long sqY1 = bottomLeftY; | |
long sqX2 = bottomLeftX; | |
long sqY2 = bottomLeftY + 1; | |
long sqX3 = bottomLeftX + 1; | |
long sqY3 = bottomLeftY; | |
long sqX4 = (long) (bottomLeftX + sqrtTwo); | |
long sqY4 = (long) (bottomLeftY + sqrtTwo); | |
return Math.sqrt(Math.pow(centreX - sqX1, 2) + Math.pow(centreY - sqY1, 2)) <= radius | |
&& Math.sqrt(Math.pow(centreX - sqX2, 2) + Math.pow(centreY - sqY2, 2)) <= radius | |
&& Math.sqrt(Math.pow(centreX - sqX3, 2) + Math.pow(centreY - sqY3, 2)) <= radius | |
&& Math.sqrt(Math.pow(centreX - sqX4, 2) + Math.pow(centreY - sqY4, 2)) <= radius; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment