• HDOJ_ACM_Escape


    Problem Description
    You find yourself trapped in a large rectangular room, made up of large square tiles; some are accessible, others are blocked by obstacles or walls. With a single step, you can move from one tile to another tile if it is horizontally or vertically adjacent (i.e. you cannot move diagonally).
    To shake off any people following you, you do not want to move in a straight line. In fact, you want to take a turn at every opportunity, never moving in any single direction longer than strictly necessary. This means that if, for example, you enter a tile from the south, you will turn either left or right, leaving to the west or the east. Only if both directions are blocked, will you move on straight ahead. You never turn around and go back!
    Given a map of the room and your starting location, figure out how long it will take you to escape (that is: reach the edge of the room).

     
    Input
    On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:

    a line with two integers separated by a space, h and w (1 <= h, w <= 80), the height and width of the room;

    then h lines, each containing w characters, describing the room. Each character is one of . (period; an accessible space), # (a blocked space) or @ (your starting location).
    There will be exactly one @ character in each room description.
     
    Output
    For each test case:

    A line with an integer: the minimal number of steps necessary to reach the edge of the room, or -1 if no escape is possible.
     
    Sample Input
    2
    9 13
    #############
    #@..........#
    #####.#.#.#.#
    #...........#
    #.#.#.#.#.#.#
    #.#.......#.#
    #.#.#.#.#.#.#
    #...........#
    #####.#######
    4 6
    #.####
    #.#.##
    #...@#
    ######
     
    Sample Output
    31
    -1
     
     
    Code
    View Code
      1 #include <stdio.h>
      2 #include <queue>
      3 using namespace std;
      4 #define N 100
      5 #define M 100
      6 struct State
      7 {
      8     int x;
      9     int y;
     10     int step; 
     11     int dir;
     12 };
     13 int dx[5] = {0, 1, 0, -1};
     14 int dy[5] = {1, 0, -1, 0};
     15 int flags[N + 5][M + 5][4];
     16 char map[N + 5][M + 5];
     17 int n, m;
     18 
     19 int BFS(State source)
     20 {
     21     queue<State> q;
     22     State cur, pre;
     23     int i, j, k, dir, flag;
     24     q.push(source);
     25     while (!q.empty())
     26     {
     27         pre = q.front();
     28         q.pop();
     29         if (pre.x == 0 || pre.x == n - 1 || pre.y == 0 || pre.y == m - 1)
     30             return pre.step;
     31         cur.step = pre.step + 1;
     32         flag = 0;
     33         //using for circulation
     34         for (i = 0; i < 4; i++)
     35         {
     36             if (pre.dir == -1)
     37                 cur.dir = i;
     38             else
     39             {
     40                 switch(i)
     41                 {
     42                     case 1: 
     43                         if (pre.dir % 2)
     44                             dir = 0;
     45                         else
     46                             dir = 1;
     47                         break;
     48                     case 2:
     49                         dir = dir + 2;
     50                         break;
     51                     case 3:
     52                         dir = pre.dir;
     53                         break;
     54                     default:
     55                         continue;
     56                 }
     57                 cur.dir = dir;
     58             }
     59             cur.x = pre.x + dx[cur.dir];
     60             cur.y = pre.y + dy[cur.dir];
     61             //if it have turn left or right, it couldn't go straight
     62             if (cur.dir == pre.dir && flag == 1)
     63                 continue;
     64             //out of index
     65             if (cur.x < 0 || cur.x >= n || cur.y < 0 || cur.y >= m)
     66                 continue;
     67             if (map[cur.x][cur.y]=='#') 
     68                 continue;
     69             flag=1;
     70             if (flags[cur.x][cur.y][cur.dir] == 0)
     71             {
     72                 flags[cur.x][cur.y][cur.dir]=1;
     73                 q.push(cur);
     74             }
     75         }
     76     }
     77     return -1;
     78 }
     79 int main()
     80 {
     81     int t, i, j, result;
     82     State source, des;
     83     scanf("%d", &t);
     84     while (t--)
     85     {
     86         scanf("%d %d", &n, &m);
     87         for (i = 0; i < n; i++)
     88         {
     89             scanf("%s", map[i]);
     90             for (j = 0; j < m; j++)
     91             {
     92                 if (map[i][j] == '@')
     93                 {
     94                     source.x = i;
     95                     source.y = j;
     96                     //printf("source: %d %d\n", source.x, source.y);
     97                 }
     98             }
     99         }
    100         source.step = 0;
    101         source.dir = -1;
    102         memset(flags, 0, sizeof(flags));
    103         result = BFS(source);
    104         printf("%d\n", result);
    105     }
    106     return 0;
    107 }
     
    Idea
    Well, yesterday I debug the program so much time, as a result, I only find a little logical error. So, don't run the program and look the result when u don't know the logical clearly. It's really dangerous. u should understand clearly every sentence's meaning and know the order of the sentence is really crucial.
     if (map[cur.x][cur.y]=='#') 
        continue;
    flag=1;
    if (flags[cur.x][cur.y][cur.dir] == 0)
    {
        flags[cur.x][cur.y][cur.dir]=1;
        q.push(cur);
    }

     Besides, for this question, I use a switch to translate the dir, it's really convenient for me.

     
     
  • 相关阅读:
    解决spring boot JavaMailSender部分收件人错误导致发送失败的问题
    Linux设备驱动开发基础--内核定时器
    Linux中断分层--工作队列
    Linux中断分层--软中断和tasklet
    深入理解函数线程安全与可重入
    Linux中断处理流程
    Linux混杂设备驱动--按键设备驱动
    Linux字符设备驱动--Led设备驱动
    Linux字符设备简单示例
    Linux内核硬件访问技术
  • 原文地址:https://www.cnblogs.com/chuanlong/p/3029254.html
Copyright © 2020-2023  润新知