提出詳細


ソースコード

import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Pandemic {

	public static boolean inField(int x, int y, int width, int height) {
		return x >= 0 && x <= width - 1 && y >= 0 && y <= height - 1;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File inFile = new File("F.txt");
		BufferedReader reader = null;
		FileReader fr = null;
		File outFile = new File("result.txt");
		BufferedWriter writer = null;
		FileWriter fw = null;
		try {
			// リーダ生成
			fr = new FileReader(inFile);
			reader = new BufferedReader(fr);
			// ライター生成
			fw = new FileWriter(outFile);
			writer = new BufferedWriter(fw);

			// テストケース数を読み込み
			int testCaseNum = Integer.parseInt(reader.readLine());

			// テストケース毎に計算、出力
			for (int i = 1; i <= testCaseNum; i++) {
				String caseNumStr = "Case #" + i + ":";

				String[] size = reader.readLine().split(" ");

				int width = Integer.parseInt(size[0]);
				int height = Integer.parseInt(size[1]);

				FieldObject[][] field = new FieldObject[height][width];
				List<FieldObject> nonInfectedPersons = new ArrayList<FieldObject>();
				List<FieldObject> infectedPersons = new ArrayList<FieldObject>();

				for (int y = 0; y < height; y++) {
					String[] values = reader.readLine().split(" ");
					for (int x = 0; x < width; x++) {
						int state = Integer.parseInt(values[x]);
						FieldObject obj = new FieldObject(state, x, y);
						field[y][x] = obj;

						switch (state) {
						case 1:
							nonInfectedPersons.add(obj);
							break;
						case 2:
							infectedPersons.add(obj);
							break;
						default:
						}
					}
				}

				Integer passageDay = 0;
				boolean increased = false;

				while (true) {
					boolean alive = passageDay != 0 && !increased;
					
					if (nonInfectedPersons.isEmpty() || alive)
						break;
					
					increased = false;

					FieldObject[] ipArray = infectedPersons.toArray(new FieldObject[0]);
					for (FieldObject obj : ipArray) {
						Point p = obj.getPoint();
						for (int dy = -1; dy <= 1; dy++) {
							for (int dx = -1; dx <= 1; dx++) {
								int targetX = p.x + dx;
								int targetY = p.y + dy;
								
								if ((dx != 0 || dy != 0) && inField(targetX, targetY, width, height)) {

									FieldObject targetObj = field[targetY][targetX];
									if (targetObj.state == 0)
										continue;

									boolean wasInfected = targetObj.isInfected();

									targetObj.infect();

									boolean isInfected = targetObj.isInfected();

									boolean increaseInfected = !(wasInfected && isInfected);
									if (increaseInfected) {
										nonInfectedPersons.remove(targetObj);
										infectedPersons.add(targetObj);
										increased = true;
									}
								}
							}
						}
					}

					passageDay++;
				}
				
				String result = nonInfectedPersons.isEmpty() ? passageDay.toString() : "ALIVE";
					
				// 標準出力
				System.out.println(caseNumStr);
				System.out.println(result);

				// テキストファイルとして書き込み処理
				writer.write(caseNumStr);
				writer.newLine();
				writer.write(result);
				writer.newLine();
			}
			// テキストファイルをフラッシュ
			writer.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 各IOインスタンスのクローズ処理
			try {
				if (reader != null)
					reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (reader != null)
					writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (fr != null)
					fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (reader != null)
					fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

class FieldObject {
	// 0なら壁、1なら非感染者、2なら感染者
	public int state;
	Point p;

	FieldObject(int state, int x, int y) {
		this.state = state;
		p = new Point(x,y);
	}

	public Point getPoint() {
		return new Point(p);
	}

	public void infect() {
		if (state == 1)
			state = 2;
	}

	public boolean isInfected() {
		return state == 2;
	}
}

提出情報

提出時間 2018-07-02 02:31:39
問題 F - Pandemic!!!
ユーザ名 soutarou1210
状態 正解
正解率 50/50
提出出力結果

テストケース情報

# 状態 詳細情報
テストケース 1 正解 詳細を見る
テストケース 2 正解 詳細を見る
テストケース 3 正解 詳細を見る
テストケース 4 正解 詳細を見る
テストケース 5 正解 詳細を見る
テストケース 6 正解 詳細を見る
テストケース 7 正解 詳細を見る
テストケース 8 正解 詳細を見る
テストケース 9 正解 詳細を見る
テストケース 10 正解 詳細を見る
テストケース 11 正解 詳細を見る
テストケース 12 正解 詳細を見る
テストケース 13 正解 詳細を見る
テストケース 14 正解 詳細を見る
テストケース 15 正解 詳細を見る
テストケース 16 正解 詳細を見る
テストケース 17 正解 詳細を見る
テストケース 18 正解 詳細を見る
テストケース 19 正解 詳細を見る
テストケース 20 正解 詳細を見る
テストケース 21 正解 詳細を見る
テストケース 22 正解 詳細を見る
テストケース 23 正解 詳細を見る
テストケース 24 正解 詳細を見る
テストケース 25 正解 詳細を見る
テストケース 26 正解 詳細を見る
テストケース 27 正解 詳細を見る
テストケース 28 正解 詳細を見る
テストケース 29 正解 詳細を見る
テストケース 30 正解 詳細を見る
テストケース 31 正解 詳細を見る
テストケース 32 正解 詳細を見る
テストケース 33 正解 詳細を見る
テストケース 34 正解 詳細を見る
テストケース 35 正解 詳細を見る
テストケース 36 正解 詳細を見る
テストケース 37 正解 詳細を見る
テストケース 38 正解 詳細を見る
テストケース 39 正解 詳細を見る
テストケース 40 正解 詳細を見る
テストケース 41 正解 詳細を見る
テストケース 42 正解 詳細を見る
テストケース 43 正解 詳細を見る
テストケース 44 正解 詳細を見る
テストケース 45 正解 詳細を見る
テストケース 46 正解 詳細を見る
テストケース 47 正解 詳細を見る
テストケース 48 正解 詳細を見る
テストケース 49 正解 詳細を見る
テストケース 50 正解 詳細を見る