zoukankan      html  css  js  c++  java
  • UVA 11642 Fire!

    Fire!

    Time Limit: 1000ms
    Memory Limit: 131072KB
    This problem will be judged on UVA. Original ID: 11624
    64-bit integer IO format: %lld      Java class name: Main
     
    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze.

    Given Joe's location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.

    Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.

    Input Specification

    The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 <= R,C <= 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of:
    • #, a wall
    • ., a passable square
    • J, Joe's initial position in the maze, which is a passable square
    • F, a square that is on fire
    There will be exactly one J in each test case.

    Sample Input

    2
    4 4
    ####
    #JF#
    #..#
    #..#
    3 3
    ###
    #J.
    #.F
    

    Output Specification

    For each test case, output a single line containing IMPOSSIBLE if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

    Output for Sample Input

    3
    IMPOSSIBLE

    解题:bfs...让火先走,再人走。。判断走出去的是人还是火
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 using namespace std;
     6 const int maxn = 1010;
     7 char mp[maxn][maxn];
     8 bool vis[maxn][maxn];
     9 int n,m;
    10 struct node{
    11     int x,y,t;
    12     bool isFire;
    13     node(int a = 0,int b = 0,int c = 0,bool isfire = true){
    14         x = a;
    15         y = b;
    16         t = c;
    17         isFire = isfire;
    18     }
    19 };
    20 queue<node>q;
    21 bool isIn(int x,int y){
    22     return x < n && x >= 0 && y < m && y >= 0;
    23 }
    24 int bfs(){
    25     static const int dir[4][2] = {-1,0,1,0,0,-1,0,1};
    26     while(!q.empty()){
    27         node now = q.front();
    28         q.pop();
    29         for(int i = 0; i < 4; ++i){
    30             int tx = now.x + dir[i][0];
    31             int ty = now.y + dir[i][1];
    32             if(isIn(tx,ty)){
    33                 if(!vis[tx][ty]){
    34                     vis[tx][ty] = true;
    35                     q.push(node(tx,ty,now.t+1,now.isFire));
    36                 }
    37             }else if(!now.isFire) return now.t+1;
    38         }
    39     }
    40     return -1;
    41 }
    42 int main(){
    43     int kase,px,py;
    44     scanf("%d",&kase);
    45     while(kase--){
    46         scanf("%d %d",&n,&m);
    47         memset(vis,false,sizeof(vis));
    48         while(!q.empty()) q.pop();
    49         for(int i = 0; i < n; ++i){
    50             scanf("%s",mp[i]);
    51             for(int j = 0; j < m; ++j){
    52                 if(mp[i][j] == 'F'){
    53                     vis[i][j] = true;
    54                     q.push(node(i,j,0,true));
    55                 }else if(mp[i][j] == '#') vis[i][j] = true;
    56                 else if(mp[i][j] == 'J') vis[px = i][py = j] = true;
    57             }
    58         }
    59         q.push(node(px,py,0,false));
    60         int ans = bfs();
    61         if(ans == -1) puts("IMPOSSIBLE");
    62         else printf("%d
    ",ans);
    63     }
    64     return 0;
    65 }
    66 /*
    67 2
    68 4 4
    69 ####
    70 #JF#
    71 #..#
    72 #..#
    73 3 3
    74 ###
    75 #J.
    76 #.F
    77 
    78 */
    View Code

  • 相关阅读:
    第一天,用诗遇见
    13计本班人工智能第二次作业
    第一次人工智能作业
    陈林 130702010048
    人工智能第一次作业
    第二次作业
    人工智能第一次作业
    软件工程(2019)结对编程第二次作业
    软件工程(2019)结对编程第一次作业
    软件工程(2019)第二次作业
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4330943.html
Copyright © 2011-2022 走看看