- 追加された行はこのように表示されます。
- 削除された行は
このように表示されます。
import java.util.*; // StringTokenizer 文字で切り分け
import java.io.*; // BufferedReader ファイル入出力
public class randb {
public static void main(String[] args) {
try {
// キーボードから入力用オブジェクト用意
BufferedReader f0 = new BufferedReader(new InputStreamReader(System.in));
// キーボードから1行文文字列に入力
System.out.print("入力ファイル名:");
String file1=f0.readLine();
//ファイルからの入力用オブジェクト用意
BufferedReader fr = new BufferedReader(new FileReader(file1));
// キーボードから1行文文字列に入力
System.out.print("出力ファイル名:");
String file2=f0.readLine();
//ファイルへの出力用オブジェクト用意
PrintWriter fw = new PrintWriter(new FileWriter(file2));
int[][] room = new int[22][22];
int[] dat = new int[2];;
while(true){
// 1行読み込み
String s = fr.readLine();
split(s, dat); // 文字列から空白を境に2整数(配列[0],[1])に分割
int w=dat[0];
int h=dat[1];
if(w==0 && h==0) break;
System.out.println("*" + w + " " + h);
// room set
int sx,sy;
for(int j=0; j<h; j++){
s = fr.readLine();
//System.out.println("--" +s + " " + s.length());
for(int i=0; i<s.length(); i++){
//System.out.print(i);
String ss=s.substring(i,i+1);
if(ss.equals(".")){
room[i+1][j+1]=0;
}else if(ss.equals("#")){
room[i+1][j+1]=1;
}else{
room[i+1][j+1]=2;
sx=i;
sy=j;
}
}
}
for(int i=0; i<w+2; i++){
room[i][0]=1;
room[i][h+1]=1;
}
for(int i=0; i<h+2; i++){
room[0][i]=1;
room[w+1][i]=1;
}
print(room, w+2, h+2);
int sum=1, ct;
while(true){
ct=0;
for(int y=1; y<h+1; y++){
for(int x=1; x<w+1; x++){
if(room[x][y]==0 && (room[x-1][y]==2 || room[x+1][y]==2 || room[x][y-1]==2 || room[x][y+1]==2) ){
room[x][y]=2;
ct++;
}
}
}
if(ct==0) break;
sum+=ct;
}
fw.println(sum);
}
fr.close();
fw.close();
} catch (FileNotFoundException e) {
System.out.println("ファイルがないよ");
} catch (IOException w) {
System.out.println("IOエラーだな");
}
}
static void split(String s, int[] a) {
StringTokenizer as = new StringTokenizer(s," ");
if (as.countTokens() != 2) {
System.out.print("Input Error\n");
System.exit(1);
}
a[0]=Integer.parseInt(as.nextToken());
a[1]=Integer.parseInt(as.nextToken());
}
static void print(int[][] a, int w, int h) {
for(int j=0; j<h; j++){
for(int i=0; i<w; i++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}