poj3083 dfs+bfs一次ac
来源:互联网 发布:买p2p问题平台数据 编辑:程序博客网 时间:2024/05/17 01:04
两个dfs一个bfs写的有点乱了…关键就是往哪走的两个数组
#include <iostream>#include <queue>using namespace std;const int INF = 100000;const int MAXNN = 41;char maze[MAXNN][MAXNN];bool check[MAXNN][MAXNN];int n, w, h,leftNum, rightNum, minNUM;int startX, startY;int dfsLeftStep, dfsRightStep;int dx[] = {1,0,-1,0};int dy[] = {0,1,0,-1};enum dir{up,rights,down,lefts};struct node{ int x,y,num; node(int i, int j,int k):x(i),y(j),num(k){}; node(){};};class go{ public: go(int i, int j,int d):x(i),y(j),dire(d){}; int x; int y; int dire;};go shang(0,-1,up), xia(0, 1,down), zuo(-1,0,lefts),you(1,0, rights);go dfsDirLeft[4][4] = { {zuo, shang, you, xia}, {shang, you, xia, zuo}, {you, xia,zuo,shang}, {xia,zuo, shang,you}};go dfsDirRight[4][4] = { {you, shang, zuo, xia}, {xia, you, shang, zuo}, {zuo, xia,you,shang}, {shang, zuo, xia,you}};bool BFSjudge(int x,int y){ return (x < w) && (0 <= x) && (0 <= y) && (y < h) && (!check[x][y]) && (maze[x][y] != '#');}bool DFSjudge(int x,int y){ return (x < w) && (0 <= x) && (0 <= y) && (y < h) && (maze[x][y] != '#') ;}void dfsLeft(int startX, int startY,int direct){ if (maze[startX][startY] == 'E') { return; } for (int i = 0;i < 4; i++) { int tmpX = startX + dfsDirLeft[direct][i].x; int tmpY = startY + dfsDirLeft[direct][i].y; if (DFSjudge(tmpX, tmpY)) { dfsLeftStep++; dfsLeft(tmpX, tmpY,dfsDirLeft[direct][i].dire ); return ; } }}void dfsRight(int startX, int startY,int direct){ if (maze[startX][startY] == 'E') { return; } for (int i = 0;i < 4; i++) { int tmpX = startX + dfsDirRight[direct][i].x; int tmpY = startY + dfsDirRight[direct][i].y; if (DFSjudge(tmpX, tmpY)) { dfsRightStep++; dfsRight(tmpX, tmpY,dfsDirRight[direct][i].dire ); return; } }}int bfs(int startX, int startY){ queue<node> que; que.push(node(startX,startY,0)); while (que.size()) { node head = que.front(); que.pop(); for (int i = 0; i < 4; i++) { node tmp(head.x + dx[i],head.y + dy[i],head.num + 1) ; if (maze[tmp.x][tmp.y] == 'E') { return tmp.num; } if (BFSjudge(tmp.x,tmp.y)) { check[tmp.x][tmp.y] = true; que.push(tmp); } } }}int main(){ cin >> n; while(n--){ cin >> w >> h; minNUM = 0; dfsLeftStep = dfsRightStep = 0; memset(check,false,sizeof(check)); memset(maze,0,sizeof(maze)); for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { cin >> maze[i][j]; if (maze[i][j] == 'S') { startX = i; startY = j; } } } dfsLeft(startX, startY,0); dfsRight(startX,startY,0); cout << dfsLeftStep + 1<< " "<<dfsRightStep + 1 << " " << bfs(startX, startY) + 1 << endl; } return 0;}
0 0
- poj3083 dfs+bfs一次ac
- poj3083 dfs + bfs
- bfs+dfs(poj3083)
- [DFS && BFS]poj3083
- POJ3083 -- BFS和DFS
- POJ3083 DFS&BFS
- 基础BFS+DFS poj3083
- poj3083(dfs/bfs混合)
- poj3083 DFS靠墙走+BFS
- POJ3083 DFS+BFS
- poj3083 dfs+bfs搜索迷宫
- poj3083 Children of the Candy Corn dfs 和 bfs 的综合,终于AC了
- poj3083 非常好 的入门级dfs+bfs
- POJ3083《Children of the Candy Corn》方法:DFS+BFS
- POJ3083 Children of the Candy Corn dfs+bfs
- POJ3083 Children of the Candy Corn(BFS+DFS水题)
- POJ3083 Children of the Candy Corn BFS+DFS
- POJ3083 Children of the Candy Corn(DFS + BFS)
- 单例模式
- 消息推送机制
- spring 支持文件上传配置
- Java异常处理手册和最佳实践
- Oracle 12c 手工创建CDB数据库
- poj3083 dfs+bfs一次ac
- Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Wi
- AFNetWorking出现code=-1016错误解决办法
- 关于ant自动化打包
- matlab2014a + win764bit + vs2013混合编程(.m转成dll供C++调用)
- mysql 多实例案例实战
- 如何将Birt源码添加到Eclipse中
- PHP获取QQ音乐的API类,可以集成到各种音乐网站上使用
- springmvc配置详解2大集合转载