hdu 2822 Dogs

来源:互联网 发布:漂流教室 知乎 编辑:程序博客网 时间:2024/06/05 02:15

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2822
bfs+优先队列。。。
X不用花时间,走.时间为1

#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<vector>#include<queue>#include<map>using std::cin;using std::cout;using std::endl;using std::find;using std::sort;using std::map;using std::pair;using std::vector;using std::multimap;using std::priority_queue;#define pb(e) push_back(e)#define sz(c) (int)(c).size()#define mp(a, b) make_pair(a, b)#define all(c) (c).begin(), (c).end()#define iter(c) decltype((c).begin())#define cls(arr,val) memset(arr,val,sizeof(arr))#define cpresent(c, e) (find(all(c), (e)) != (c).end())#define rep(i, n) for (int i = 0; i < (int)(n); i++)#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)const int N = 1010;typedef unsigned long long ull;const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };bool vis[N][N];char rec[N][N];int m, n, Sx, Sy, Dx, Dy;struct Node {    int x, y, s;    Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {}    bool operator<(const Node &a) const {        return s > a.s;    }};void bfs() {    cls(vis, false);    priority_queue<Node> que;    que.push(Node(Sx, Sy, 0));    vis[Sx][Sy] = true;    while (!que.empty()) {        Node tmp = que.top(); que.pop();        if (tmp.x == Dx && tmp.y == Dy) { printf("%d\n", tmp.s); return; }        rep(i, 4) {            int nx = tmp.x + dx[i], ny = tmp.y + dy[i];            if (nx < 0 || nx >= m || ny < 0 || ny >= n || vis[nx][ny]) continue;            if (rec[nx][ny] == 'X') que.push(Node(nx, ny, tmp.s));            else que.push(Node(nx, ny, tmp.s + 1));            vis[nx][ny] = true;        }    }}int main() {#ifdef LOCAL    freopen("in.txt", "r", stdin);    freopen("out.txt", "w+", stdout);#endif    while (~scanf("%d %d", &m, &n) && m + n) {        rep(i, m) scanf("%s", rec[i]);        scanf("%d %d %d %d", &Sx, &Sy, &Dx, &Dy);        Sx--, Sy--, Dx--, Dy--;        bfs();    }    return 0;}
0 0
原创粉丝点击