hdu 5040 Instrusive 最短路
来源:互联网 发布:店铺怎么用淘宝客 编辑:程序博客网 时间:2024/06/05 21:11
题意:
潜入军事基地。
在图中有多个刚开始时有多个不同朝向的监视器,其视程为2(包括监视器本身)。每隔1秒,监视器就会顺时针转90度。
1.你可以躲进box里逃避监视器的监察,躲进box后若移动一格需要3秒,否则只需1秒。或者待在原地不动。
2.你一开始可能就在box里。
3.若打算进入当前已经被监控的区域,或者离开你现在所在的位置(已被监控),你必须要躲进box里才可以进入或者离开。
4.N,E,S,W这些格子是可以进入的。
让你求出到达目的地的最短时间。不能到达,输出-1
思路:
监视器4秒一个周期,因此有:
dis[x][y][t]:到达点(x,y),t = dis[x][y][t]%4 的最短时间。(其实t就是表示对于每个监视器所过去的时间是多少)
预处理每个格子需要躲进box才能进入的时间。跑个最短路。
*另:这题顶卡时间,我用SPFA写的差点超时。铭神用的bfs才500ms
AC代码:
/* ********************************************** Created Time: 2014-9-25 16:36:11 File Name : hdu5040.cpp *********************************************** *///#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <fstream>#include <cstring>#include <climits>#include <deque>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <utility>#include <sstream>#include <complex>#include <string>#include <vector>#include <cstdio>#include <bitset>#include <functional>#include <algorithm>using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int MAXN = 505;int dir[5][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};char gra[MAXN][MAXN];int n;struct Point{ int x, y, t;}m, t;int dis[MAXN][MAXN][5];bool inq[MAXN][MAXN][5];bool canin[MAXN][MAXN][5]; //0: can move in ; 1: can't move invoid SPFA(int x, int y){ queue <Point> q; q.push((Point){x, y, 0}); inq[x][y][0] = true, dis[x][y][0] = 0; int dd; while(!q.empty()) { Point e = q.front(); q.pop(), inq[e.x][e.y][e.t] = false; //check whether in the cardbox; bool inbox = canin[e.x][e.y][e.t]; //move need time if(inbox) dd = 3; else dd = 1; Point t; //don't move; t = e; t.t = (dis[e.x][e.y][e.t]+1)%4; if(dis[t.x][t.y][t.t] > dis[e.x][e.y][e.t]+1) { dis[t.x][t.y][t.t] = dis[e.x][e.y][e.t]+1; if(!inq[t.x][t.y][t.t]) { inq[t.x][t.y][t.t] = true; q.push(t); } } for(int i = 0;i < 4; i++) { t.x = e.x + dir[i][0], t.y = e.y + dir[i][1], t.t = dis[e.x][e.y][e.t]%4; if(t.x >= 0 && t.y >= 0 && t.x < n && t.y < n && gra[t.x][t.y] != '#') { //check current time, the next grad whether is insight of cameras; bool insight = canin[t.x][t.y][t.t]; int ddd; if(insight) ddd = 3; //the next grad is insight ,so daitao; else ddd = dd; t.t = (dis[e.x][e.y][e.t]+ddd)%4; if(dis[t.x][t.y][t.t] > dis[e.x][e.y][e.t]+ddd) { dis[t.x][t.y][t.t] = dis[e.x][e.y][e.t]+ddd; if(!inq[t.x][t.y][t.t]) { inq[t.x][t.y][t.t] = true; q.push(t); } } } } }}char fx[5] = "NESW";void preDeal(int x, int y, char ch) //NESW{ int i; for(i = 0;i < 4; i++) if(ch == fx[i]) break; int tx, ty, cot = 0; for(int j = 0;j < 4; j++) canin[x][y][j] = true; while(cot < 4) { tx = x+dir[i][0], ty = y + dir[i][1]; if(tx >= 0 && ty >= 0 && tx < n && ty < n) canin[tx][ty][cot] = true; i = (i+1)%4; cot++; }}int main(){ int T, cas = 0; scanf("%d", &T); while(T--) { //input scanf("%d", &n); for(int i = 0;i < n; i++) for(int j = 0;j < n; j++) for(int z = 0;z < 4; z++) dis[i][j][z] = INF, canin[i][j][z] = false; getchar(); for(int i = 0;i < n; i++) { scanf("%s", gra[i]); for(int j = 0;j < n; j++) { if(gra[i][j] == '.' || gra[i][j] == '#') continue; if(gra[i][j] == 'M') m.x = i, m.y = j; else if(gra[i][j] == 'T') t.x = i, t.y = j; else preDeal(i, j, gra[i][j]); //= = } } // SPFA(m.x, m.y); int res = INF; for(int i = 0;i < 4; i++) res = min(res, dis[t.x][t.y][i]); if(res == INF) printf("Case #%d: -1\n", ++cas); else printf("Case #%d: %d\n", ++cas, res); } return 0;}
0 0
- HDU 5040 Instrusive 最短路
- hdu 5040 Instrusive 最短路
- HDU 5040 Instrusive 搜索
- hdu 5040 Instrusive(BFS)
- 【HDU 5040】Instrusive
- HDU 5040 Instrusive
- HDU 5040 Instrusive
- hdu 5040 Instrusive (bfs)
- HDU 5040 Instrusive
- hdu 5040 Instrusive(BFS)
- 网络赛 Instrusive (HDU-5040)
- hdu 5040——Instrusive
- HDU-5040-Instrusive(BFS+优先队列)
- HDU 5040 Instrusive BFS 性质优化
- hdu 5040 Instrusive【BFS+优先队列】
- hdu 5040 Instrusive(堆优化+dij)
- Instrusive hdu 5040 优先队列+BFS
- HDU 5040 Instrusive(北京网络赛I题)
- Centos及linux系统安装VirtualBox虚拟机增强包
- Mac OS X中Apache开启ssl
- Objective-C相关Category的收集
- 两个子窗体间的传值和函数调用
- nginx相关书籍
- hdu 5040 Instrusive 最短路
- oracle中flashback闪回区常存储的内容
- 生活丰富便捷
- 黑马程序员——多态
- 【面向对象程序设计之CRC】CRC卡及其应用
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
- android--广播、服务、数据库、通知、包原理图
- qt 5.3 安装调试
- 循环队列的实现