博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
「日常训练」 Fire!(UVA-11624)
阅读量:4550 次
发布时间:2019-06-08

本文共 2430 字,大约阅读时间需要 8 分钟。

与其说是训练不如说是重温。重新写了Java版本的代码。

import java.util.*;import java.math.*;import java.io.BufferedInputStream;public class Main{    static class Node    {    int r, c, t;    public Node(int _r, int _c)    {        this(_r, _c, 0);    }    public Node(int _r, int _c, int _t)    {        r=_r; c=_c; t=_t;    }    }    static int[][] fireTime = new int[1005][1005];    static int[][] maze = new int[1005][1005];    static Queue
q = new LinkedList<>(); static final int[] dx = {0,1,0,-1}; static final int[] dy = {1,0,-1,0}; static int r, c; static int startR, startC; static void getFireTime() { for(int i=0;i<=r;++i) for(int j=0;j<=c;++j) fireTime[i][j]=0x3f3f3f3f; while(!q.isEmpty()) { Node node = q.poll(); fireTime[node.r][node.c] = node.t; for(int i=0; i!=4; ++i) { int tr = node.r + dx[i], tc = node.c + dy[i]; if(tr>=0 && tr
=0 && tc
node.t+1 && maze[tr][tc]==1) { fireTime[tr][tc] = node.t+1; q.offer(new Node(tr, tc, node.t+1)); } } } } static String solve() { Queue
sq = new LinkedList<>(); boolean[][] vis = new boolean[1005][1005]; sq.offer(new Node(startR, startC)); vis[startR][startC]=true; while(!sq.isEmpty()) { Node now = sq.poll(); if(now.t>=fireTime[now.r][now.c]) continue; if(now.r==0 || now.r == r-1 || now.c == 0 || now.c == c-1) return String.valueOf(now.t+1); for(int i=0;i!=4;++i) { int tr = now.r + dx[i], tc = now.c + dy[i]; if(tr>=0 && tr
=0 && tc
now.t+1 && (!vis[tr][tc]) && maze[tr][tc]==1) { vis[tr][tc]=true; sq.offer(new Node(tr, tc, now.t+1)); } } } return "IMPOSSIBLE"; } public static void main(String args[]) { Scanner cin = new Scanner(new BufferedInputStream(System.in)); int T = cin.nextInt(); while(T-- != 0) { q.clear(); r = cin.nextInt(); c = cin.nextInt(); for(int i=0; i!=r; ++i) { String str = cin.next(); for(int j=0;j!=c;++j) { char chr = str.charAt(j); if(chr == '#') maze[i][j] = 0; else if(chr == '.') maze[i][j] = 1; else if(chr == 'J') { maze[i][j] = 1; startR=i; startC=j; } else if(chr == 'F') { maze[i][j] = 1; q.offer(new Node(i,j)); } } } getFireTime(); System.out.println(solve()); } }}

转载于:https://www.cnblogs.com/samhx/p/UVa-11624.html

你可能感兴趣的文章
第一个Azure应用
查看>>
Java 读写锁的实现
查看>>
分享、收藏、打印页面操作
查看>>
Vim 编辑器
查看>>
js跳转页面方法大全
查看>>
别名节点aliases
查看>>
BZOJ-10-1176: [Balkan2007]Mokia-CDQ第二类应用
查看>>
[C++]线性链表之顺序表<一>
查看>>
操作系统学习
查看>>
常用free文献数据库
查看>>
题目2
查看>>
js创建对象的方式 三种
查看>>
elementUI vue v-model的修饰符
查看>>
数组复制:关于java中引用传递的一个例子
查看>>
第九周PSP
查看>>
红帽linux忘记root密码的配置
查看>>
JS十进制转二进制(控制位数)
查看>>
Spark源码分析 – SparkContext
查看>>
tabBar选择不同item设置标题不同颜色
查看>>
机器学习算法一览图
查看>>