hdu 1180

来源:互联网 发布:php sdk是什么 编辑:程序博客网 时间:2024/06/02 01:58
题意:(http://acm.hdu.edu.cn/showproblem.php?pid=1180)这道题刚开始没读懂题,以为不可以等待,然后就直接把需要等待的pass掉了,结果就wa了。后来找了点测试数据,才发现自己的错误,可是还是有错,就是需要重复更新,只要比原来距离小的都要更新。题目重点在于判断奇偶性和更新距离数组。先给两组测试数据吧!
3 4S|.|-T-..|..720 20S.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.T20

代码如下:(有注释)

#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 longusing 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
原创粉丝点击