hdu 1180

题意:(http://acm.hdu.edu.cn/showproblem.php?pid=1180)

这道题刚开始没读懂题,以为不可以等待,然后就直接把需要等待的pass掉了,结果就wa了。后来找了点测试数据,才发现自己的错误,可是还是有错,就是需要重复更新,只要比原来距离小的都要更新。
题目重点在于判断奇偶性和更新距离数组。
先给两组测试数据吧!
3 4
S|.|
-T-.
.|..
7
20 20
S.|.|.|.|.|.|.|.|.|.
.|.|.|.|.|.|.|.|.|.|
|.|.|.|.|.|.|.|.|.|.
.|.|.|.|.|.|.|.|.|.|
|.|.|.|.|.|.|.|.|.|.
.|.|.|.|.|.|.|.|.|.|
|.|.|.|.|.|.|.|.|.|.
.|.|.|.|.|.|.|.|.|.|
|.|.|.|.|.|.|.|.|.|.
.|.|.|.|.|.|.|.|.|.|
|.|.|.|.|.|.|.|.|.|.
.|.|.|.|.|.|.|.|.|.|
|.|.|.|.|.|.|.|.|.|.
.|.|.|.|.|.|.|.|.|.|
|.|.|.|.|.|.|.|.|.|.
.|.|.|.|.|.|.|.|.|.|
|.|.|.|.|.|.|.|.|.|.
.|.|.|.|.|.|.|.|.|.|
|.|.|.|.|.|.|.|.|.|.
.|.|.|.|.|.|.|.|.|.T
20

代码如下:(有注释)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
//#include <map>
#define N 25
#define INF 1e9+7
#define ll long long
using namespace std;
typedef pair<int,int> PII;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; //方向数组
char map[N][N]; //地图
int dis[N][N];  //保存到当前位置的最短距离
int n,m,sx,sy,ex,ey;
queue<PII> que;

int min(int a,int b){
    return a < b ? a :b;
}

int bfs(int sx,int sy){
    //初始化距离数组
    for (int i = 0;i < m;i++){ 
        for (int j = 0;j < n;j++){
            dis[i][j] = INF;
        }
    }
    dis[sx][sy] = 0;
    //起点入队列
    que.push(PII(sx,sy));
    //直到队列为空才停止
    while(que.size()){
        PII p = que.front();que.pop();
        if (p.first == ex && p.second == ey) break; //找到退出
        for (int i = 0;i < 4;i++){
            int nx = p.first + dx[i],ny = p.second + dy[i];
            // 用flag标记直接过还是需要等待 。 false表示直接过。
            bool flag = false;
            // 如果下一个字符是‘|’。
            if (map[nx][ny] == '|'){
                // 当前步到达下一步方向是上或者下(仔细观察方向数组)
                if ((i == 1 || i == 3)){ 
                    // 如果当前距离是偶数则需要等待
                    if (!dis[p.first][p.second]%2) flag = true;
                    nx += dx[i],ny+=dy[i];
                }
                // 当前步到达下一步方向是左或者右
                else if ((i==0 ||i==2)){
                    if (dis[p.first][p.second]%2) flag = true;
                    nx += dx[i],ny += dy[i];
                }
            }
            // 道理同上
            if (map[nx][ny] == '-'){
                if ((i == 1 || i == 3)){
                    if (dis[p.first][p.second]%2) flag = true;
                    nx += dx[i],ny+=dy[i];
                }
                else if ((i==0 ||i==2)){
                    if (!dis[p.first][p.second]%2) flag = true;
                    nx += dx[i],ny += dy[i];
                }
            }

            if (nx >= 0 && nx < m && ny >= 0 && ny < n && map[nx][ny] != '*'){
                // 如果现在的下一步距离小于已经存在的下一步距离,则把它加入队列。
                if (flag && dis[p.first][p.second]+2 < dis[nx][ny]){
                    que.push(PII(nx,ny));
                }
                // 同理
                else if (dis[p.first][p.second]+2 < dis[nx][ny]) {
                    que.push(PII(nx,ny));
                }
                // 更新距离数组。
                if (flag) dis[nx][ny] = min(dis[p.first][p.second]+2,dis[nx][ny]);
                else dis[nx][ny] = min(dis[p.first][p.second]+1,dis[nx][ny]);
            }
        }
    }
    return dis[ex][ey];
}


int main(){
    while (cin >> m >> n){
        memset(map,0,sizeof(map));
        for (int i = 0;i < m;i++){
            for (int j = 0;j < n;j++){
                cin >> map[i][j];
                if (map[i][j] == 'S'){
                    sx = i,sy = j;
                }
                if (map[i][j] == 'T'){
                    ex = i,ey = j;
                }
            }
        }
        int ans = bfs(sx,sy);
        cout << ans << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值