HDU 4528 (BFS)
来源:互联网 发布:知乎中国出口构成 编辑:程序博客网 时间:2024/05/23 11:41
题目链接:点击这里
题意:给一个地图,要从S出发看到D,E,如果两个点在同一行并且中间没有X或者人就可以看到。
比较坑的是不能走有人的格子。所以直接给两个人一个权值多开一维记录拿到的权值就好了。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#include <queue>using namespace std;#define maxn 111int n, m, t;char mp[maxn][maxn];bool legal (int x, int y) { if (x < 0 || y < 0 || x >= n || y >= m) return 0; if (mp[x][y] == 'X' || mp[x][y] == 'E' || mp[x][y] == 'D') return 0; return 1;}struct node { int x, y; int num, step; node (int _x = 0, int _y = 0, int _n = 0, int _s = 0):x(_x), y(_y), num(_n), step(_s) {}}s, e, d;queue <node> gg;int val[maxn][maxn];void go (node s, int num) { int x, y; x = s.x, y = s.y; for (y--; legal (x, y); y--) {val[x][y] |= num;} y = s.y; for (y++; legal (x, y); y++) {val[x][y] |= num;} y = s.y; for (x--; legal (x, y); x--) {val[x][y] |= num;} x = s.x; for (x++; legal (x, y); x++) {val[x][y] |= num;} x = s.x;}bool vis[maxn][maxn][11];const int M[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};int bfs () { while (!gg.empty ()) gg.pop (); memset (val, 0, sizeof val); go (e, 1); go (d, 2); s.num += val[s.x][s.y]; memset (vis, 0, sizeof vis); vis[s.x][s.y][s.num] = 1; gg.push (s); while (!gg.empty ()) { node now = gg.front (); gg.pop (); if (now.num == 3) { return now.step; } for (int i = 0; i < 4; i++) { node next = now; next.x += M[i][0], next.y += M[i][1]; if (!legal (next.x, next.y)) continue; next.step++; next.num |= val[next.x][next.y]; if (vis[next.x][next.y][next.num] || next.step > t) continue; gg.push (next); vis[next.x][next.y][next.num] = 1; } } return -1;}int main () { int T, kase = 0; scanf ("%d", &T); while (T--) { printf ("Case %d:\n", ++kase); scanf ("%d%d%d", &n, &m, &t); for (int i = 0; i < n; i++) { scanf ("%s", mp[i]); for (int j = 0; j < m; j++) { if (mp[i][j] == 'S') s = node (i, j, 0); else if (mp[i][j] == 'D') d = node (i, j, 0); else if (mp[i][j] == 'E') e = node (i, j, 0); } } int ans = bfs (); printf ("%d\n", ans); } return 0;}
0 0
- hdu 4528(bfs)
- HDU 4528 (BFS)
- HDU bfs
- hdu 1175 bfs
- HDU 1072(BFS)
- HDU 1429(BFS )
- hdu 1026(BFS)
- HDU 4039 BFS
- hdu 1312 bfs
- hdu 1253 三维bfs
- hdu 1242 bfs
- BFS hdu 1072 Nightmare
- HDU 1072 Nightmare BFS
- bfs hdu 1026
- hdu-2757 BFS
- HDU-1495 BFS
- hdu 1401 bfs
- HDU-2952 BFS
- digester使用
- Android Annotations 4.0.0 配置Rest API
- [58] Length of Last Word
- UVa1601 万圣节后的早晨(The Morning after Halloween)
- 静态存储区的内存四区
- HDU 4528 (BFS)
- Oracle查询昨天的记录
- leetcode 357. Count Numbers with Unique Digits 解题报告
- codeforces round 359# div.2 B. Little Robber Girl's Zoo
- soapui笔记3:创建一个新的项目
- 编写redhat7.2内核,使其支持nfts格式
- 文章标题
- android 编译等各种坑……
- java 标准IO、文件中读取数据(上)