zoukankan      html  css  js  c++  java
  • hdu 1180(诡异的楼梯)

    这题还真是诡异,明明思路很清晰,BFS也很熟练 的敲出来了,可就是改了半天,原来问题在这里,“Harry从来不在楼梯上停留”,这里提示的很明显了,他除了不再楼梯上停留,其他地方是可以停留的,当然,也就只有在遇到楼梯,又暂时过不去时,才有必要在原地停留,郁闷呀,疏忽了这里,wa了好几次……

    hdu1180 代码
    #include <iostream>
    #include
    <queue>
    using namespace std;
    struct Node
    {
    int x, y, time;
    Node(
    int _x=0,int _y=0,int _time=0):x(_x),y(_y),time(_time){};
    };

    Node first;
    int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
    int n, m, a, b;
    char map[21][21];
    bool vis[21][21];
    queue
    <Node> Q;

    int bfs()
    {
    int fx, fy;
    first.x
    = a;
    first.y
    = b;
    first.time
    = 0;
    Q.push(first);
    while (!Q.empty())
    {
    first
    = Q.front();
    Q.pop();

    if (map[first.x][first.y] == 'T')
    {
    return first.time;
    }

    int i;
    for (i = 0; i < 4; ++i)
    {
    fx
    = first.x + dir[i][0];
    fy
    = first.y + dir[i][1];

    if (fx >= 0 && fx < n && fy >= 0 && fy < m && map[fx][fy] != '*' && !vis[fx][fy])
    {
    if (map[fx][fy] == '.' || map[fx][fy] == 'T')
    {
    vis[fx][fy]
    = 1;
    Q.push(Node(fx,fy,first.time
    +1));
    }
    else if (map[fx][fy] == '-')
    {
    if (first.time % 2 == 0)
    {
    if (fx == first.x)
    {
    fy
    = 2*fy - first.y;
    if (fy >= 0 && fy < m && map[fx][fy] != '*' && !vis[fx][fy])
    {
    vis[fx][fy]
    = 1;
    Q.push(Node(fx,fy,first.time
    +1));
    }
    }
    else
    Q.push(Node(first.x,first.y,first.time
    +1));//遇到楼梯,但方向不对,原地停留
    }
    else
    {
    if (fy == first.y)
    {
    fx
    = 2*fx - first.x;
    if (fx >= 0 && fx < n && map[fx][fy] != '*' && !vis[fx][fy])
    {
    vis[fx][fy]
    = 1;
    Q.push(Node(fx,fy,first.time
    +1));
    }
    }
    else
    Q.push(Node(first.x,first.y,first.time
    +1));//同样,原地停留
    }
    }
    else if (map[fx][fy] == '|')
    {
    if (first.time % 2 == 0)
    {
    if (fy == first.y)
    {
    fx
    = 2*fx - first.x;
    if (fx >= 0 && fx < n && map[fx][fy] != '*' && !vis[fx][fy])
    {
    vis[fx][fy]
    = 1;
    Q.push(Node(fx,fy,first.time
    +1));
    }
    }
    else
    Q.push(Node(first.x,first.y,first.time
    +1));
    }
    else
    {
    if (fx == first.x)
    {
    fy
    = 2*fy - first.y;
    if (fy >= 0 && fy < m && map[fx][fy] != '*' && !vis[fx][fy])
    {
    vis[fx][fy]
    = 1;
    Q.push(Node(fx,fy,first.time
    +1));
    }
    }
    else
    Q.push(Node(first.x,first.y,first.time
    +1));
    }
    }
    }
    }
    }
    return -1;
    }


    int main()
    {
    while (scanf("%d %d", &n, &m) != EOF)
    {
    int i, j;
    for (i = 0; i < n; ++i)
    {
    getchar();
    for (j = 0; j < m; ++j)
    {
    scanf(
    "%c", &map[i][j]);
    if (map[i][j] == 'S')
    {
    a
    = i;
    b
    = j;
    }
    vis[i][j]
    =0;
    }
    }
    vis[a][b]
    = 1;
    while (!Q.empty())
    {
    Q.pop();
    }
    printf(
    "%d\n",bfs());
    }
    return 0;
    }
  • 相关阅读:
    html5 保存图片到服务器本地
    html5 canvas分层
    webstorm配置scss自动编译路径
    微信开发测试号配置
    html5手机网站需要加的那些meta/link标签,html5 meta全解
    css去掉iPhone、iPad默认按钮样式
    如何激活webstorm 11
    min-height在安卓下不起作用
    基于字符串模式的路由路径的一些示例。
    nodeJS搭建本地服务器
  • 原文地址:https://www.cnblogs.com/nanke/p/2126020.html
Copyright © 2011-2022 走看看