UVA 10047 The Monocycle
来源:互联网 发布:java中的try catch 编辑:程序博客网 时间:2024/04/29 04:16
以前漏下的题目,现在重新做发现很水。
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=988
独轮车问题,注意的就是vis数组的使用,裸的BFS就可以。vis数组是对当前坐标,颜色,方向的记录。为了熟悉一下优先队列,程序中使用了优先队列代替普通的队列,程序中可以在获得下一个状态时加入旋转180°的,此时step+=2就可以了。
#include <algorithm>#include <iostream>#include <string.h>#include <cstdlib>#include <iomanip>#include <sstream>#include <fstream>#include <climits>#include <cstdio>#include <vector>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>using namespace std;const int INF = 0x7fffffff;const int MAXL = 30;const int MAXC = 5;const int MAXD = 4;typedef struct _Node{ int x,y,color,step,dir;}Node;struct Cmp{ bool operator ()(const Node a,const Node b) { return a.step > b.step; }};char maze[MAXL][MAXL];bool vis[MAXL][MAXL][MAXC][MAXD];int dc[] = {0,1,2,3,4};//0--Greenint dd[] = {0,1,2,3};//0--N,1--E,2--S,3--Wint dx[] = {-1,0,1,0};int dy[] = {0,1,0,-1};int L,R,Sx,Sy,Tx,Ty;priority_queue < Node ,vector <Node>, Cmp> Q;void Print_Node(Node cur){ printf("x = %d, y = %d, color = %d,step = %d,dir = %d\n",cur.x,cur.y,cur.color,cur.step,cur.dir);}bool IsOver(Node cur){ return cur.x == Tx && cur.y == Ty && cur.color == 0;}bool IsInMaze(Node cur){ return cur.x >= 0 && cur.x < L && cur.y >= 0 && cur.y < R;}bool IsBlock(Node cur){ return maze[cur.x][cur.y] != '#';}bool IsVisit(Node cur){ return vis[cur.x][cur.y][cur.color][cur.dir];}Node GetNextNode(Node cur){ Node newnode; newnode.x = cur.x + dx[ cur.dir ]; newnode.y = cur.y + dy[ cur.dir ]; newnode.color = (cur.color + 1) % MAXC; newnode.dir = cur.dir; newnode.step = cur.step + 1; return newnode;}Node TurnLeft90(Node cur){ Node newnode; newnode.x = cur.x; newnode.y = cur.y; newnode.color = cur.color; newnode.dir = (((cur.dir)+MAXD) - 1) % MAXD; newnode.step = cur.step + 1; return newnode;}Node TurnRight90(Node cur){ Node newnode; newnode.x = cur.x; newnode.y = cur.y; newnode.color = cur.color; newnode.dir = (cur.dir + 1) % MAXD; newnode.step = cur.step + 1; return newnode;}int BFS(){ while(!Q.empty()) Q.pop(); memset(vis,false,sizeof(vis)); Node cur,newnode; cur.x = Sx,cur.y = Sy,cur.color = dc[0],cur.step = 0,cur.dir = dd[0]; Q.push(cur); vis[cur.x][cur.y][cur.color][cur.dir] = true; while(!Q.empty()) { cur = Q.top(); Q.pop(); if(IsOver(cur)) return cur.step; newnode = GetNextNode(cur); if(IsInMaze(newnode) && IsBlock(newnode) && !IsVisit(newnode)) { Q.push(newnode); vis[newnode.x][newnode.y][newnode.color][newnode.dir] = true; } newnode = TurnLeft90(cur); if(IsInMaze(newnode) && IsBlock(newnode) && !IsVisit(newnode)) { Q.push(newnode); vis[newnode.x][newnode.y][newnode.color][newnode.dir] = true; } newnode = TurnRight90(cur); if(IsInMaze(newnode) && IsBlock(newnode) && !IsVisit(newnode)) { Q.push(newnode); vis[newnode.x][newnode.y][newnode.color][newnode.dir] = true; } } return -1;}int main(){#ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout);#endif int cases = 0; while(~scanf("%d %d",&L,&R) && L+R) { gets(maze[0]); for(int i = 0;i < L;i++) { gets(maze[i]); for(int j = 0;j < R;j++) { if(maze[i][j] == 'S') Sx = i,Sy = j; else if(maze[i][j] == 'T') Tx = i,Ty = j; } } if(cases) puts(""); printf("Case #%d\n",++cases); int ans = BFS(); if(ans == -1) printf("destination not reachable\n"); else printf("minimum time = %d sec\n",ans); } return 0;}
。
- UVA 10047 The Monocycle
- uva 10047 - The Monocycle
- uva 10047 - The Monocycle
- UVA 10047 - The Monocycle
- uva 10047 - The Monocycle
- uva 10047 - The Monocycle
- UVa 10047 - The Monocycle
- uva 10047 The Monocycle
- Uva 10047 The Monocycle
- uva 10047 The Monocycle
- UVa 10047 The Monocycle
- UVa:10047 The Monocycle
- UVa 10047 - The Monocycle
- UVa 10047 - The Monocycle
- UVA 10047 - The Monocycle
- UVA - 10047 The Monocycle
- UVA - 10047 The Monocycle
- UVA 10047 The Monocycle
- 浏览器中查看
- Game 迷城的国度 Next(类似暗黑的游戏)
- 理解WebKit和Chromium: Chromium的GPU硬件加速
- java中字符串与16进制,byte之间的转换
- 配置Samba
- UVA 10047 The Monocycle
- 网络编程学习
- 店铺支付宝接口申请方法
- 简单排序算法
- 使用Handler实现多线程
- Vim实战手册(1)初级个性化配置你的Vim
- Apache ODE流程编译过程解析
- PostgresQL数据库的参数需要KeepAlive和防火墙清理检查的时间
- Vim实战手册(2)常用的状态切换按键