/************************************************ Grundlegende Algorithmen mit Java, http://algorithmen-und-problemloesungen.de/ Copyright @2007-2008 by Doina Logofatu in C#: Michael Gärtner ************************************************/ using System; using System.IO; namespace Logofatu { class P9QuadratRek { private static String FileInputName = "quadrate.in"; private static String FileOutputName = "quadrate.out"; private int step = 0; private bool inSquare(int x0, int y0, int cx, int cy, int k) { int x1 = cx - k; int x2 = cx + k; int y1 = cy - k; int y2 = cy + k; return (x1 <= x0 && x0 <= x2 && y1 <= y0 && y0 <= y2); } public void count(int x0, int y0, int cx, int cy, int k) { int x1, x2, y1, y2; if ( inSquare(x0,y0,cx,cy,k) ) step++; if ( k > 1 ) { x1 = cx - k; x2 = cx + k; y1 = cy - k; y2 = cy + k; count(x0, y0, x1, y1, k / 2); count(x0, y0, x1, y2, k / 2); count(x0, y0, x2, y1, k / 2); count(x0, y0, x2, y2, k / 2); } } static void Main(string[] args) { StreamReader sr = null; StreamWriter sw = null; try { int k, x0, y0; sr = new StreamReader(FileInputName); sw = new StreamWriter(FileOutputName); while ( !sr.EndOfStream ) { string[] sArray = sr.ReadLine().Split(' '); k = int.Parse(sArray[0]); x0 = int.Parse(sArray[1]); y0 = int.Parse(sArray[2]); P9QuadratRek q = new P9QuadratRek(); q.count(x0, y0, 1024, 1024, k); sw.WriteLine(q.step); } } catch ( IOException ex ) { Console.WriteLine("Fehler bei der Dateiverarbeitung!\n" + ex); } finally { if ( sr != null ) { sr.Close(); } if ( sw != null ) { sw.Close(); } } } } }