走迷宫1

走迷宫1

Time Limit: 1000ms
Memory Limit: 65535KB
64-bit integer IO format:  %lld      Java class name:  Main
Prev  Submit  Status  Statistics  Discuss  Next
Font Size:  +   -
Type:  None Graph Theory     2-SAT     Articulation/Bridge/Biconnected Component     Cycles/Topological Sorting/Strongly Connected Component     Shortest Path         Bellman Ford         Dijkstra/Floyd Warshall     Euler Trail/Circuit     Heavy-Light Decomposition     Minimum Spanning Tree     Stable Marriage Problem     Trees     Directed Minimum Spanning Tree     Flow/Matching         Graph Matching             Bipartite Matching             Hopcroft–Karp Bipartite Matching             Weighted Bipartite Matching/Hungarian Algorithm         Flow             Max Flow/Min Cut             Min Cost Max Flow DFS-like     Backtracking with Pruning/Branch and Bound     Basic Recursion     IDA* Search     Parsing/Grammar     Breadth First Search/Depth First Search     Advanced Search Techniques         Binary Search/Bisection         Ternary Search Geometry     Basic Geometry     Computational Geometry     Convex Hull     Pick's Theorem Game Theory     Green Hackenbush/Colon Principle/Fusion Principle     Nim     Sprague-Grundy Number Matrix     Gaussian Elimination     Matrix Exponentiation Data Structures     Basic Data Structures     Binary Indexed Tree     Binary Search Tree     Hashing     Orthogonal Range Search     Range Minimum Query/Lowest Common Ancestor     Segment Tree/Interval Tree     Trie Tree     Sorting     Disjoint Set String     Aho Corasick     Knuth-Morris-Pratt     Suffix Array/Suffix Tree Math     Basic Math     Big Integer Arithmetic     Number Theory         Chinese Remainder Theorem         Extended Euclid         Inclusion/Exclusion         Modular Arithmetic     Combinatorics         Group Theory/Burnside's lemma         Counting     Probability/Expected Value Others     Tricky     Hardest     Unusual     Brute Force     Implementation     Constructive Algorithms     Two Pointer     Bitmask     Beginner     Discrete Logarithm/Shank's Baby-step Giant-step Algorithm     Greedy     Divide and Conquer Dynamic Programming     Tag it!

走迷宫是很有趣的一种游戏,能够锻炼人的记忆力和思维.现在,HK被困在一个迷宫里面了,请你帮助他找到一条最短的路径,能够让他走出迷宫.

迷宫使用一个N*M的矩阵来描述,矩阵中用'.'代表空格可以通行,用'*'代表障碍物,用'S'代表出发点,用'T'代表出口.例如下面的一个矩阵就描述了一个8*8的迷宫

.....T..
..*****.
......*.
*.***.*.
......*.
.****.*.
S..*....
........

每个字符代表1个格子,HK只能在格子间按上下左右的方向移动

Input

每个输入文件只包含一组输入数据.
每组数据第一行是两个正整数N和M(N,M<=100).
接着是一个N*M的矩阵.

Output

如果HK能够走出迷宫,输出最少需要的步数;否则输出-1.

Sample Input

8 8
.....T..
..*****.
......*.
*.***.*.
......*.
.****.*.
S..*....
........

Sample Output

11

Source

第七届北京师范大学程序设计竞赛热身赛第五场

Author

HK@Sphinx

Tags ( Click to see )

Prev  Submit  Status  Statistics  Discuss  Next


裸BFS

#include"queue"
#include"cstdio"
#include"cstring"
#include"iostream"
#include"algorithm"

using namespace std;

char maze[105][105];

int N,M;
int move[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
bool vis[105][105];

bool is_block(int x,int y)
{
    if(x<0 || x>=N || y<0 || y>=M)
    {
        return true;
    }
    if(maze[x][y] == '*')
    {
        return true;
    }
    return false;
}

struct node
{
    int x,y,step;
};

queue  q;

int bfs(int x,int y)
{
    while(!q.empty())
    {
        q.pop();
    }
    node now;
    now.x = x;
    now.y = y;
    now.step = 0;
    vis[x][y] = true;
    q.push(now);
    while(!q.empty())
    {
        node nnew = q.front();
        q.pop();
        if(maze[nnew.x][nnew.y] == 'T')
        {
            return nnew.step;
        }
        for(int i = 0;i < 4;i++)
        {
            int xx = nnew.x + move[i][0];
            int yy = nnew.y + move[i][1];
            if(!is_block(xx,yy) && !vis[xx][yy])
            {
                node n;
                n.x = xx;
                n.y = yy;
                n.step = nnew.step + 1;
                vis[xx][yy] = true;
                q.push(n);
            }
        }
    }
    return -1;
}

int main()
{
    while(~scanf("%d%d",&N,&M))
    {
        for(int i = 0;i < N;i++)
        {
            scanf("%s",maze[i]);
        }
        memset(vis,false,sizeof(vis));
        for(int i = 0;i < N;i++)
        {
            for(int j = 0;j < M;j++)
            {
                if(maze[i][j] == 'S')
                {
                    printf("%d\n",bfs(i,j));
                    break;
                }
            }
        }
    }
    return 0;
}



你可能感兴趣的:(BFS)