hdu1180诡异的楼梯(bfs)
来源:互联网 发布:电脑下载软件哪个好 编辑:程序博客网 时间:2024/06/10 02:49
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1180
这个题真的是坑啊。。 等楼梯的时候要看看对面是否已经访问过了,如果访问过了,就没有必要过楼梯了。。。。 这点很重要。。。。
代码:
#include <cstdio>#include <queue>#include <cstring>using namespace std;char s[30][30];int v[30][30];int n,m;int stx,sty,endx,endy;int ans ;int dx[] = {-1,0,1,0};int dy[] = {0,1,0,-1};struct Node{ int x,y; int time;};bool isOk(int x,int y,Node t,Node &r){ int dir; for(int i = 0; i < 4; ++i) { int tx = t.x + dx[i]; int ty = t.y + dy[i]; if(tx == x && ty == y) { dir = i; break; } } int nextx = x + dx[dir]; int nexty = y + dy[dir]; if(nextx >= 0 && nextx < n && nexty >= 0 && nexty < m && s[nextx][nexty] != '*' && !v[nextx][nexty]) { if(dir % 2 == 0) { if((s[x][y] == '|' && t.time % 2 == 0) || (s[x][y] == '-' && t.time % 2)) { r.x = nextx; r.y = nexty; r.time = t.time + 1; return true; } else { r.x = nextx; r.y = nexty; r.time = t.time + 2; //printf("flag = %d\n",flag); return true; } } else if(dir % 2) { if((s[x][y] == '|' && t.time % 2) || (s[x][y] == '-' && t.time % 2 == 0)) { r.x = nextx; r.y = nexty; r.time = t.time + 1; return true; } else { r.x = nextx; r.y = nexty; r.time = t.time + 2; // printf("flag = %d\n",flag); return true; } } } return false;}void bfs(int x,int y){ queue<Node> q; Node first; first.x = x; first.y = y; first.time = 0; q.push(first); v[x][y] = 1; while(!q.empty()) { Node head = q.front(); q.pop(); for(int i = 0; i < 4; ++i) { int tx = head.x + dx[i]; int ty = head.y + dy[i]; if(tx == endx && ty == endy) { ans = head.time + 1; return ; } if(tx >= 0 && tx < n && ty >= 0 && ty < m && !v[tx][ty]) { if(s[tx][ty] == '.') { Node t; t.x = tx; t.y = ty; t.time = head.time + 1; q.push(t); v[tx][ty] = 1; } else if(s[tx][ty] == '|' || s[tx][ty] =='-') { Node t; if(isOk(tx,ty,head,t)) { //printf("%d %d\n",t.x,t.y); if(t.x == endx && t.y == endy) { ans = t.time; return ; } q.push(t); v[t.x][t.y] = 1; } } } } }}int main(){ while(~scanf("%d%d",&n,&m)) { if(!m || !n) { printf("0\n"); continue; } memset(v,0,sizeof v); for(int i = 0; i < n; ++i) scanf("%s",s[i]); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) if(s[i][j] == 'S') { stx = i; sty = j; } else if(s[i][j] == 'T') { endx = i; endy = j; } } ans = 0; bfs(stx,sty); printf("%d\n",ans); } return 0;}
0 0
- HDU1180 诡异的楼梯--BFS
- hdu1180诡异的楼梯 bfs
- hdu1180诡异的楼梯(bfs)
- hdu1180 诡异的楼梯--BFS
- HDU1180-诡异的楼梯(bfs)
- HDU1180 诡异的楼梯(BFS)
- hdu1180诡异的楼梯(BFS+优先队列)
- 【搜索-BFS】HDU1180诡异的楼梯
- hdu1180-诡异的楼梯(bfs+优先队列)
- hdu1180诡异的楼梯(bfs)
- hdu1180 诡异的楼梯 --bfs+优先级队列
- hdu1180 诡异的楼梯
- HDU1180 诡异的楼梯
- hdu1180 诡异的楼梯
- hdu1180诡异的楼梯
- HDU1180 诡异的楼梯
- HDU1180-诡异的楼梯
- ACM-BFS之诡异的楼梯——hdu1180
- image2lcd的使用
- Java强引用、软引用、弱引用和虚引用
- http_load使用
- Objective-C之run loop详解
- HDOJ(HDU) 2162 Add ‘em(求和)
- hdu1180诡异的楼梯(bfs)
- C++第五次实验-项目1:数组分离
- 给定一个字符串,找出其所有不重复的排列组合
- 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 现场赛 WHU oj Problem 1613 - Count in Sama’s triangle 扩展欧几里得
- 《程序员自我修养》第十章读书笔记
- 归并排序
- 蓄水池算法
- Asp.net相对路径连接Access数据库的方法
- 数的全排列