POJ bfs+dfs之3083-Children of the Candy Corn
来源:互联网 发布:caffe model zoo 编辑:程序博客网 时间:2024/06/03 14:35
POJ3083-Children of the Candy Corn
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<vector>#include<algorithm>#include<queue>#define MAXNUM 10020using namespace std;int n, m;bool mp[50][50], mark[50][50];bool flag;char tmp[60];int LPath, RPath, SPath, Path;int Dir[4][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };struct Point{ int x, y;}start, en;void LDFS(int x, int y, int dir){ if (x == en.x && y == en.y) return; int a, b, i, j; for (i = dir - 1; i < dir + 3; i++) { j = (i + 4) % 4; a = x + Dir[j][0]; b = y + Dir[j][1]; if (a >= 0 && a < m&&b >= 0 && b < n) { if (mp[a][b]) break; } } LPath++; LDFS(a, b, j);}void RDFS(int x, int y, int dir){ if (x == en.x && y == en.y) return; int a, b, i, j; for (i = dir + 1; i > dir - 3; i--) { j = (4 + i) % 4; a = x + Dir[j][0]; b = y + Dir[j][1]; if (a >= 0 && a < m&&b >= 0 && b < n) { if (mp[a][b]) { break; } } } RPath++; RDFS(a, b, j);}void SDFS(int x, int y, int dir){ queue<Point> record; int i, j, k, t, a, b; Point tmp; tmp.x = x; tmp.y = y; record.push(tmp); SPath = 0; flag = 0; while (true) { t = record.size(); SPath++; while (t > 0) { i = record.front().x; j = record.front().y; record.pop(); if (i == en.x&&j == en.y) { flag = 1; break; } for (k = 0; k < 4; k++) { a = i + Dir[k][0]; b = j + Dir[k][1]; if (a >= 0 && a < m&&b >= 0 && b < n) { if (mp[a][b] && !mark[a][b]) { mark[a][b] = 1; Point tmp; tmp.x = a; tmp.y = b; record.push(tmp); } } } t--; } if (flag) break; }}int main(){ freopen("1.txt", "r", stdin); int i, j, k, t, a, b, cases, dir; bool flag; scanf("%d", &cases); while (cases > 0) { scanf("%d%d", &n, &m); memset(mp, 0, sizeof(mp)); for (i = 0; i < m; i++) { scanf("%s", tmp); for (j = 0; j < n; j++) { if (tmp[j] == '.') mp[i][j] = 1; else if (tmp[j] == 'S') { mp[i][j] = 1; start.x = i; start.y = j; if (i == 0) dir = 2; else if (i == (m - 1)) dir = 0; else if (j == 0) dir = 1; else if (j == (n - 1)) dir = 3; } else if (tmp[j] == 'E') { mp[i][j] = 1; en.x = i; en.y = j; } } } LPath = RPath = 0; LDFS(start.x, start.y, dir); RDFS(start.x, start.y, dir); memset(mark, 0, sizeof(mark)); SDFS(start.x, start.y, dir); printf("%d %d %d\n",LPath+1, RPath + 1, SPath); cases--; } return 0;}
不知道为什么原来的代码是错的:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<vector>#include<algorithm>#include<queue>#define MAXNUM 10020using namespace std;int n, m;bool mp[50][50], mark[50][50], flag;char tmp[60];int LPath, RPath, SPath, Path;int Dir[4][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };struct Point{ int x, y;}start, en;void LDFS(int x,int y,int dir){ if (flag) return; if (x == en.x && y == en.y) { flag = 1; return; } int a, b, i, j; for (i = dir-1; i < dir + 3; i++) { j = (i + 4) % 4; a = x + Dir[j][0]; b = y + Dir[j][1]; if (a >= 0 && a < m&&b >= 0 && b < n) { if (mp[a][b]) { LPath++; LDFS(a, b, j); if (flag) return; } } }}void RDFS(int x, int y, int dir){ if (flag) return; if (x == en.x && y == en.y) { flag = 1; return; } int a, b, i, j; for (i = dir + 1; i > dir - 3; i--) { j = (4 + i) % 4; a = x + Dir[j][0]; b = y + Dir[j][1]; if (a >= 0 && a < m&&b >= 0 && b < n) { if (mp[a][b]) { RPath++; RDFS(a, b, j); if (flag) return; } } }}void SDFS(int x, int y, int dir){ queue<Point> record; int i, j, k, t, a, b; Point tmp; tmp.x = x; tmp.y = y; record.push(tmp); SPath = 0; flag = 0; while (true) { t = record.size(); SPath++; while (t > 0) { i = record.front().x; j = record.front().y; record.pop(); if (i == en.x&&j == en.y) { flag = 1; break; } for (k = 0; k < 4; k++) { a = i + Dir[k][0]; b = j + Dir[k][1]; if (a >= 0 && a < m&&b >= 0 && b < n) { if (mp[a][b] && !mark[a][b]) { mark[a][b] = 1; Point tmp; tmp.x = a; tmp.y = b; record.push(tmp); } } } t--; } if (flag) break; }}int main(){ freopen("1.txt", "r", stdin); int i, j, k, t, a, b, cases, dir; bool flag; scanf("%d", &cases); while (cases > 0) { scanf("%d%d", &n, &m); memset(mp, 0, sizeof(mp)); for (i = 0; i < m; i++) { scanf("%s", tmp); for (j = 0; j < n; j++) { if (tmp[j] == '.') mp[i][j] = 1; else if (tmp[j] == 'S') { mp[i][j] = 1; start.x = i; start.y = j; if (i == 0) dir = 2; else if (i == (m - 1)) dir = 0; else if (j == 0) dir = 1; else if (j == (n - 1)) dir = 3; } else if (tmp[j] == 'E') { mp[i][j] = 1; en.x = i; en.y = j; } } } LPath = RPath = 0; flag = 0; //LDFS(start.x, start.y, dir); //flag = 0; RDFS(start.x, start.y, dir); memset(mark, 0, sizeof(mark)); SDFS(start.x, start.y, dir); printf("%d %d\n", RPath+1, SPath); cases--; } return 0;}
阅读全文
0 0
- POJ bfs+dfs之3083-Children of the Candy Corn
- POJ 3083 Children of the Candy Corn dfs+bfs
- poj 3083 Children of the Candy Corn(dfs+bfs)
- POJ 3083Children of the Candy Corn(DFS*2+BFS)
- POJ 3083 Children of the Candy Corn (bfs+dfs)
- POJ 3083:Children of the Candy Corn(DFS+BFS)
- POJ 3083 - Children of the Candy Corn(BFS+DFS)
- POJ 3083 Children of the Candy Corn (DFS + BFS)
- POJ 3083-Children of the Candy Corn(dfs+bfs)
- poj 3083 Children of the Candy Corn(bfs+dfs)
- poj 3083 Children of the Candy Corn (DFS+BFS)
- POJ 3083 Children of the Candy Corn(bfs+dfs)
- poj 3083 Children of the Candy Corn dfs+bfs
- poj-3083 Children of the Candy Corn-DFS+BFS
- Poj 3083 Children of the Candy Corn (DFS+BFS)
- POJ 3083 Children of the Candy Corn(BFS + DFS)
- [Poj 3083] Children of the Candy Corn bfs+dfs
- [POJ 3083]Children of the Candy Corn[DFS+BFS]
- LA 3905 Meteor [扫描线]
- Hadoop 集群搭建之一 CentOS7 安装
- 利用StringBuffer读写文件实例
- EA&UML日拱一卒-活动图::SendSignalAction
- DBA扁鹊
- POJ bfs+dfs之3083-Children of the Candy Corn
- python文件操作
- 【web前端自动化工作环境配置】6. 版本控制工具git与githup的区别与联系以及git的基本用法
- for循环遍历list集合组拼成字符串StringBuilder,及处理最后一个逗号
- 初步认识进程
- C++ Map
- Python爬虫基础
- VC6.0文件
- android系统信息