Created
July 2, 2012 07:25
-
-
Save kunishi/3031665 to your computer and use it in GitHub Desktop.
ACM International Collegiate Programming Contest, Japan Domestic, 2004, Problem B
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.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.ListIterator; | |
/* | |
* B.java | |
* | |
* Created on 2005/06/10, 2:05 | |
* | |
* To change this template, choose Tools | Options and locate the template under | |
* the Source Creation and Management node. Right-click the template and choose | |
* Open. You can then make changes to the template in the Source Editor. | |
*/ | |
/** | |
* | |
* @author kunishi | |
*/ | |
public class B { | |
public class Tile { | |
public static final int RED = 0; | |
public static final int BLACK = 1; | |
public static final int OPEN = 1; | |
public static final int CLOSE = 0; | |
public int color; | |
public int status; | |
public int x, y; | |
public Tile(int x, int y) { | |
color = BLACK; | |
status = CLOSE; | |
this.x = x; | |
this.y = y; | |
} | |
public Tile(int c, int x, int y) { | |
color = c; | |
status = CLOSE; | |
this.x = x; | |
this.y = y; | |
} | |
public int calc(Tile[][] tiles) { | |
if (color == Tile.RED) { | |
return 0; | |
} else if (status == Tile.OPEN) { | |
return 0; | |
} else { | |
int sum = 1; | |
status = Tile.OPEN; | |
ArrayList al = new ArrayList(); | |
al.add(tiles[x-1][y]); | |
al.add(tiles[x][y+1]); | |
al.add(tiles[x+1][y]); | |
al.add(tiles[x][y-1]); | |
ListIterator li = al.listIterator(); | |
while (li.hasNext()) { | |
sum += ((Tile)li.next()).calc(tiles); | |
} | |
return sum; | |
} | |
} | |
} | |
/** Creates a new instance of B */ | |
public B() { | |
} | |
public static void main(String[] args) { | |
B b = new B(); | |
b.doCalculation(args[0]); | |
} | |
public void doCalculation(String file) { | |
int w, h; | |
Tile[][] tiles = null; | |
Tile mypos = null; | |
try { | |
BufferedReader br = new BufferedReader(new FileReader(file)); | |
String s = br.readLine(); | |
while (!s.equals("0 0")) { | |
w = Integer.parseInt(s.substring(0, s.indexOf(' '))); | |
h = Integer.parseInt(s.substring(s.indexOf(' ')+1, s.length())); | |
tiles = new Tile[w+2][h+2]; | |
for (int i = 0; i < h+2; i ++) { | |
tiles[0][i] = new Tile(Tile.RED, 0, i); | |
tiles[w+1][i] = new Tile(Tile.RED, w+1, i); | |
} | |
for (int i = 0; i < w+2; i ++) { | |
tiles[i][0] = new Tile(Tile.RED, i, 0); | |
tiles[i][h+1] = new Tile(Tile.RED, i, h+1); | |
} | |
for (int i = 0; i < h; i ++) { | |
s = br.readLine(); | |
for (int j = 0; j < s.length(); j ++) { | |
switch (s.charAt(j)) { | |
case '.': | |
tiles[j+1][i+1] = new Tile(Tile.BLACK, j+1, i+1); | |
break; | |
case '#': | |
tiles[j+1][i+1] = new Tile(Tile.RED, j+1, i+1); | |
break; | |
case '@': | |
tiles[j+1][i+1] = new Tile(Tile.BLACK, j+1, i+1); | |
mypos = tiles[j+1][i+1]; | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
System.out.println(mypos.calc(tiles)); | |
s = br.readLine(); | |
} | |
} catch (FileNotFoundException e) { | |
System.out.println(e); | |
} catch (IOException e) { | |
System.out.println(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment