zoukankan      html  css  js  c++  java
  • SDNU 1220.Look for homework(BFS路径标记)

    Description

    (the picture has no relation with this problem..I just want to add a picture. emmm..)
     
    Super scholar robs the all homework of others. The monitor decides to combat with the super scholar so as to help students to get back the homework. But super scholar lives in a castle because he doesn't want to be disturbed. The outside of the castle is a maze with two dimension grids. Entering the castle must pass the maze. The monitor needs to save time because of accompanying his girlfriend, so he wants to make a student named Huachao Wei help him who hasn't a girlfriend. Now he has the map of the maze and needs to calculate the shortest path. 

    Input

    The input consists several cases.For each case starts with two integers n,m(),symbolizing the length and width of the maze.The next n lines contain m numbers with no space and value for only 0 and 1.The essence is that 0 can pass ,1 can't pass. Now you are in the place of (1,1) refering to the top left corner.the export is in the place of (n,m).Each step can only walk with up,down,left and right.

    Output

    For each case,the first line prints the least number of steps to arrive the castle.The second line prints k characters for U,D,L,R,representing up,down,left,right.if there are many paths with the same length,please print the path with Minimum dictionary.It is guarantees that there must have a path for arriving the castle.

    Sample Input

    3 3
    001
    100
    110
    3 3
    000
    000
    000

    Sample Output

    4
    RDRD
    4
    DDRR

    Source

    Unknown
    思路:真是道神奇的题啊。读入数据不可以直接用int型;在走上下左右的时候,题目说要按字典序最小输出,于是对于走的先后就有了区分:要下、左、右、上依次走,才能符合题目的要求。
    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <map>
    using namespace std;
    #define ll long long
    
    int n, m, position[18][18];
    int path[4][2] = {1,0, 0,-1, 0,1, -1,0};
    struct node
    {
        int x, y;
    };
    
    struct edge
    {
        int x, y, s = 0;
        char step;
    } e[100+8][100+8];
    
    void bfs()
    {
        queue<node>q;
        node p;
        p.x = 0;
        p.y = 0;
        position[0][0] = 1;
        q.push(p);
        while(!q.empty())
        {
            node l = q.front();
            q.pop();
            if(l.x == n-1 && l.y == m-1)return;
            for(int i = 0; i<4; i++)
            {
                node w;
                w.x = l.x+path[i][0];
                w.y = l.y+path[i][1];
                if(w.x>=0 && w.y>=0 && w.x < n && w.y < m && !position[w.x][w.y])
                {
                    position[w.x][w.y] = 1;
                    e[w.x][w.y].s = e[l.x][l.y].s+1;
                    e[w.x][w.y].x = l.x;//记录该点的上一个点,因为上一个点是确认的
                    e[w.x][w.y].y = l.y;//记录该点的上一个点,因为上一个点是确认的
                    if(i == 0)e[w.x][w.y].step = 'D';
                    else if(i == 1)e[w.x][w.y].step = 'L';
                    else if(i == 2)e[w.x][w.y].step = 'R';
                    else if(i == 3)e[w.x][w.y].step = 'U';
                    q.push(w);
                }
            }
        }
    }
    
    void print(int a, int b)
    {
        if(a == 0 && b == 0)return;
        print(e[a][b].x, e[a][b].y);
        printf("%c", e[a][b].step);
    }
    
    int main()
    {
        while(~scanf("%d%d", &n, &m))
        {
            char s[18][18];
            for(int i = 0; i < n; i++)scanf("%s", s[i]);
            for(int i = 0; i < n; i++)
                for(int j = 0; j < m; j++)
                {
                    if(s[i][j] == '0')position[i][j] = 0;
                    else position[i][j] = 1;
                }
            bfs();
            printf("%d
    ", e[n-1][m-1].s);
            print(n-1, m-1);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    树套树+【UVALive】6709 Mosaic 二维线段树
    汇编实验1. 计算1+2+3+…+10,将结果显示在屏幕上。4
    Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2) D. Destruction of a Tree
    HDU 4417 Super Mario主席树
    spoj+B
    2018-2019赛季多校联合新生训练赛第五场(2018/12/14)补题题解
    迷宫问题 POJ
    浅谈二分搜索与二分查找
    Moving Tables POJ
    Humidex POJ
  • 原文地址:https://www.cnblogs.com/RootVount/p/10991679.html
Copyright © 2011-2022 走看看