11573 - Ocean Currents
来源:互联网 发布:淘宝的优化建议 编辑:程序博客网 时间:2024/06/06 05:41
这题必须要加优先队列优化 不然会超时
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int maxn = 1000 + 7;const int INF = ~0U >> 1;const int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};const int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};char str[maxn][maxn];struct node { int x, y; int val; node(int a, int b, int c) : x(a), y(b), val(c) { } bool operator<(const node &rhs) const { return val > rhs.val; }};int m, n;int q, sx, sy, ex, ey;int vis[maxn][maxn];int BFS() { int ans = INF; priority_queue<node> que; memset(vis, -1, sizeof(vis)); vis[sx][sy] = 0; que.push(node(sx, sy, 0)); while(!que.empty()) { node u = que.top(); que.pop(); if(u.x == ex && u.y == ey) { return u.val; } for(int i = 0; i < 8; ++i) { int nx = dx[i] + u.x, ny = dy[i] + u.y; if(nx >= 1 && nx <= m && ny >= 1 && ny <= n) { int d = u.val; if(str[u.x][u.y] - '0' != i) d += 1; if(vis[nx][ny] == -1 || vis[nx][ny] > d) { vis[nx][ny] = d; que.push(node(nx, ny, d)); } } } } return ans;}int main() { while(scanf("%d%d", &m, &n) != EOF) { for(int i = 1; i <= m; ++i) scanf("%s", str[i] + 1); scanf("%d", &q); for(int i = 0; i < q; ++i) { scanf("%d%d%d%d", &sx, &sy, &ex, &ey); int ans = BFS(); printf("%d\n", ans); } } return 0;}
0 0
- 11573 - Ocean Currents
- 11573 - Ocean Currents
- 11573 - Ocean Currents DFS变形
- UVA 11573 Ocean Currents --BFS+优先队列
- UVA 11573 - Ocean Currents(BFS+优先队列)
- UVA 11573 - Ocean Currents【BFS+优先队列】
- uva 11573 - Ocean Currents(bfs)
- Hdu 2757 Ocean Currents
- hdu(2575)Ocean Currents
- HDU 2757 Ocean Currents
- hdu 2757 Ocean Currents
- uva 11573 - Ocean Currents(bfs+优先队列)
- hdu 2757 Ocean Currents【广度优先搜索】
- hdu 2757 Ocean Currents (一搜到底)
- JOJ 2558 / HDU 2757 (Ocean Currents) 解题纠错
- hdu 2757 Ocean Currents(BFS 比较简单的)
- hdu 2757 Ocean Currents(优先队列+bfs)
- HDU - 2757 Ocean Currents (BFS+优先队列)
- Eclipse启动时报需要安装"Java SE 6 Runtime"致无法启动解决方案
- 导入jar包到web项目中
- 视差特效
- idea gradle开发java工程
- unityTaidou(三)unity模型材质比较暗的解决方法
- 11573 - Ocean Currents
- bzoj2186: [Sdoi2008]沙拉公主的困惑
- HDU——1003Max Sum(子序列最大和)
- Codeforces Round #342 (Div. 2)总结
- 程序员进阶书籍介绍推荐
- HDU 3555 Bomb(数位DP)
- spring(AOP)Aop的概念
- 分隔符匹配
- 欢迎使用CSDN-markdown编辑器