J - Fire! UVA - 11624 JAVA

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main
{
	public static char[][] arr;
	public static int[][] fire;
	public static int[][] move =
	{
			{ 0, 1 },
			{ 1, 0 },
			{ -1, 0 },
			{ 0, -1 } };

	public static void main(String args[])
	{
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader sc = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		int p = sc.nextInt();
		while (p-- > 0)
		{
			int a = sc.nextInt();
			int b = sc.nextInt();
			arr = new char[a][b];
			fire = new int[a][b];
			boolean pd[][] = new boolean[a][b];
			Queue<change> joe = new LinkedList<change>();
			for (int i = 0; i < fire.length; i++)
			{
				for (int j = 0; j < fire[i].length; j++)
				{
					fire[i][j] = Integer.MAX_VALUE;
				}
			}
			Queue<change> x = new LinkedList<change>();

			for (int i = 0; i < arr.length; i++)
			{

				arr[i] = sc.next().toCharArray();
				for (int j = 0; j < b; j++)
				{
					if (arr[i][j] == 'F')
					{
						x.add(new change(i, j, 1));

					}
					if (arr[i][j] == 'J')
					{

						joe.add(new change(i, j, 1));

					}
				}
			}
			// read in
			while (!x.isEmpty())
			{
				change ok = x.poll();
				fire[ok.x][ok.y] = ok.step;
				for (int i = 0; i < move.length; i++)
				{
					int o = ok.x + move[i][0];
					int k = ok.y + move[i][1];
					if (o >= 0 && o < a && k >= 0 && k < b && arr[o][k] != '#' && ok.step + 1 < fire[o][k])
					{
						x.add(new change(o, k, ok.step + 1));
						fire[o][k] = ok.step + 1;
					}
				}
			}

			/// burn it
			boolean gg = false;
			while (!joe.isEmpty())
			{

				change ok = joe.poll();
				if (ok.step >= fire[ok.x][ok.y])
				{
					continue;
				}
				if (ok.x == 0 || ok.x == a - 1 || ok.y == 0 || ok.y == b - 1)
				{
					gg = true;
					System.out.println(ok.step);
					break;
				}

				for (int i = 0; i < 4; i++)
				{
					int o = ok.x + move[i][0];
					int k = ok.y + move[i][1];
					if (o >= 0 && o < a && k >= 0 && k < b && arr[o][k] != '#' && ok.step + 1 < fire[o][k]
							&& pd[o][k] == false)
					{
						pd[o][k] = true;
						joe.add(new change(o, k, ok.step + 1));
					}
				}

			}
			if (gg == false)
			{
				System.out.println("IMPOSSIBLE");
			}
		
		}
		out.close();

	}

	static class InputReader
	{
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public InputReader(InputStream stream)
		{
			reader = new BufferedReader(new InputStreamReader(stream), 32768);
			tokenizer = null;
		}

		public String next()
		{
			while (tokenizer == null || !tokenizer.hasMoreTokens())
			{
				try
				{
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e)
				{
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

		public int nextInt()
		{
			return Integer.parseInt(next());
		}
	}

}

class change
{
	int x, y, step;

	public change(int x, int y, int step)
	{
		this.x = x;
		this.y = y;
		this.step = step;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值