SlideShare a Scribd company logo
1 of 8
+
AtCoderRegularContest31B
埋め立て
ICPC AOJ Meeting 2014/1/19
+
Problem
 とある所に島国がありました。島国にはいくつかの島がありま
す。このたび、この島国で埋め立て計画が立案されたのですが、
どこを埋め立てるか決まっていません。
 できることなら埋め立てによって島を繋いで、1 つの島にして
しまいたいのですが、たくさん埋め立てるわけにもいきません。
10 マス × 10 マスのこの島国の地図が与えられるので、1 マ
スを埋め立てた時に 1 つの島にできるか判定してください。
 ただし、地図で陸地を表すマスが上下左右につながっている領
域のことを島と呼びます。
+
Sample
赤く囲ったマスを埋め立てることで 1 つの島にできます。
xxxxxxxxxx
xoooooooxx
xxoooooxxx
xxxoooxxxx
xxxxoxxxxx
xxxxxxxxxx
xxxxoxxxxx
xxxoooxxxx
xxoooooxxx
xxxxxxxxxx
+
SourceCode
http://ideone.com/wDhohJ
+
SourceCode
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String[] data = new String[10];
for(int i=0;i<data.length;i++){
data[i]=sc.nextLine();
}
System.out.println(new Execution(data));
}
}
class Execution{
private Island island;
private boolean result;
public Execution(String[] data){
island = new Island(data);
result = calculation();
}
private boolean calculation(){
for(int i=0;i<island.getBoolPool().length;i++){
for(int j=0;j<island.getBoolPool()[i].length;j++){
Island target = island.getClone();
dfs(target,new Point(j,i));
if(check(target)){
return true;
}
}
}
return false;
}
private boolean check(Island is){
for(int i = 0;i < is.getBoolPool().length;i++){
for(int j = 0;j < is.getBoolPool()[i].length;j++){
if(is.getBoolPool()[i][j]){
return false;
}
}
}
return true;
}
+private void dfs(Island is,Point point){
is.getBoolPool()[point.y][point.x]=false;
for(Point p:point.getFourWay()){
if(validRange(p)){
continue;
}
if(!is.getBoolPool()[p.y][p.x]){
continue;
}
dfs(is,p);
}
}
private boolean validRange(Point point){
return point.x<0||point.y<0||point.x>=10||point.y>=10;
}
public String toString(){
return result?"YES":"NO";
}
}
+
class Island{
private boolean[][] boolPool;
public Island(String[] data){
boolPool=makePool(data);
}
public Island(boolean[][] boolPool){
this.boolPool=boolPool;
}
private boolean[][] makePool(String[] data){
boolean[][] tmp = new
boolean[data.length][data[0].length()];
for(int i=0;i<tmp.length;i++){
for(int j=0;j<tmp[i].length;j++){
tmp[i][j]=data[i].charAt(j)=='o';
}
}
return tmp;
}
+
public Island getClone(){
boolean[][] tmp = new boolean[boolPool.length][boolPool[0].length];
for(int i=0;i<tmp.length;i++){
for(int j=0;j<tmp[i].length;j++){
tmp[i][j]=boolPool[i][j];
}
}
return new Island(tmp);
}
public boolean[][] getBoolPool(){
return boolPool;
}
}
class Point{
public int x;
public int y;
public Point(int x,int y){
this.x=x;
this.y=y;
}
public Point[] getFourWay(){
Point[] points = new Point[4];
points[0]=new Point(x,y+1);
points[1]=new Point(x,y-1);
points[2]=new Point(x+1,y);
points[3]=new Point(x-1,y);
return points;
}
}

More Related Content

More from nullzine

第二回ミーティングスライド
第二回ミーティングスライド第二回ミーティングスライド
第二回ミーティングスライドnullzine
 

More from nullzine (6)

Meeting7
Meeting7Meeting7
Meeting7
 
Meeting6
Meeting6Meeting6
Meeting6
 
Meeting5
Meeting5Meeting5
Meeting5
 
Meeting4
Meeting4Meeting4
Meeting4
 
第二回ミーティングスライド
第二回ミーティングスライド第二回ミーティングスライド
第二回ミーティングスライド
 
Meeting1
Meeting1Meeting1
Meeting1
 

Meeting19

Editor's Notes

  1. AOJ0222