poj 3083 DFS
poj3083
用DFS和BFS。通过这题,我对DFS和BFS再次有了初步的认识!!但是方向问题真的好绕啊。。真心没搞懂,还是看了别人的才做出来的。下来我计划再做几道DFS和BFS的题。然后,再去独立把上学期数据结构书上的迷宫那题给搞出来,用两种方法,这也许就我暂时的目的吧。
再接再励。
#include <iostream>#include <cstdio>#include <queue>#include <fstream>#include <memory.h> #define MAX 45using namespace std; struct node{int x,y;int step;}; int flag[MAX][MAX];char map[MAX][MAX];int dir1[4][2]={{0,-1},{1,0},{0,1},{-1,0}}; // Left firstint dir2[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; //Right firstint w,h;int d1,d2;int start[2],end[2]; void getmap(){ memset(flag,0,sizeof(flag));for(int i=0; i<h; i++) { cin>>map[i];for(int j=0; j<w; j++) {if(map[i][j]=='#') flag[i][j]=1;else if(map[i][j]=='S') { start[0]=i; start[1]=j; flag[i][j]=1; }else if(map[i][j]=='E') { end[0]=i; end[1]=j; } } }if(start[0]==0) { // S on the down-side d1=1; d2=1; }else if(start[0]==h-1) { //upside d1=3; d2=3; }else if(start[1]==w-1) { //right side d1=2; d2=0; }else { //left side d1=0; d2=2; }} int dfs(int x,int y,int d,int dir[][2]){int step,sx,sy,temp;if(x==end[0] && y==end[1])return 1;for(int i=0; i<4; i++) { temp=(d+i)%4; sx=x+dir[temp][1]; sy=y+dir[temp][0];if(sx>=0 && sx<h && sy>=0 && sy<w && flag[sx][sy]==0)break; } step=dfs(sx,sy,(temp+3)%4,dir)+1;return step;} int bfs(){ queue<node> q; node p; p.x=start[0]; p.y=start[1]; p.step=1; q.push(p);while(!q.empty()) { p=q.front(); q.pop();if(p.x==end[0] && p.y==end[1]) //End return p.step;for(int i=0; i<4; i++){ node temp; temp.x=p.x+dir1[i][1]; temp.y=p.y+dir1[i][0];if(temp.x>=0 && temp.x<h && temp.y>=0 && temp.y<w && !flag[temp.x][temp.y]){ flag[temp.x][temp.y]=1; temp.step=p.step+1; q.push(temp); } } }return 0;} int main(){int n; freopen("acm.txt","r",stdin); scanf("%d",&n);while(n--){ scanf("%d%d",&w,&h); getmap(); printf("%d ",dfs(start[0],start[1],d1,dir1)); //Left printf("%d ",dfs(start[0],start[1],d2,dir2)); //Right printf("%d\n",bfs()); }return 0;}
转载于:https://www.cnblogs.com/Jason-Damon/archive/2012/03/11/2390703.html
poj 3083 DFS相关推荐
- A - 棋盘问题 POJ - 1321(dfs)
A - 棋盘问题 POJ - 1321 dfs 复杂度计算: dfs共进行a步,每步需要循环n次,复杂度为 a^n次方 首先是暴力 an*n,果然炸了 #include<cstdio> # ...
- poj 3083 Children of the Candy Corn(bfs+dfs 数组模拟方向)
好纠结啊,方向转晕了~~~~~先贴个半山寨的代码 #include <cstdio>#include<string.h>#define MAX 45 struct node{i ...
- poj 2531(dfs)
题目链接:http://poj.org/problem?id=2531 思路:由于N才20,可以dfs爆搞,枚举所有的情况,复杂度为2^(n). 1 #include<iostream> ...
- POJ - Hopscotch(DFS)
题目链接:http://poj.org/problem?id=3050 Time Limit: 1000MS Memory Limit: 65536K Description The cows pla ...
- POJ 2458 DFS+判重
题意: 思路: 搜+判重 嗯搞定 (听说有好多人用7个for写得-.) //By SiriusRen #include <bitset> #include <cstdio>0 ...
- POJ 2230 DFS
题意: Bessie 最近做了农场看守,他每天晚上的工作就是巡视农场并且保证没有坏人破坏农场.从谷仓出发去巡视,并且最终回到谷仓. Bessie 视力不是很好,不能像其他农场的看守一样,对农场的每一条 ...
- poj 3411(DFS多点访问)
题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱,如果没有去过就付R的钱.求的是最少要花多 ...
- POJ 3051 DFS
题意:判断连通块大小 水题 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm ...
- Wang Xifeng's Little Plot (poj 5024 DFS)
Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
最新文章
- Linux中的文件系统和磁盘管理
- Java取得当前类的路径
- Attribute class invalid for tag present according to TLD
- go语言实现第一个程序-hello,world!
- 性能测试(01)-jmeter元件-线程组、调试取样器
- VS2013动态库文件的创建及其使用详解
- 加载顺序_Java的web.xml组件加载顺序
- HttpClient 学习整理【转】
- mysql数据上传apache_配置Apache服务器 数据库mySQL
- 【正点原子MP157连载】第二章 ATK-STM32MP157烧写系统-摘自【正点原子】STM32MP157快速体验
- Flash MX 2004 帮助CHM 在线版
- jdbcTemplate测试报错:没有合适的驱动
- Endnote自定义文献引用格式
- 微软服务器和onenote,为什么微软推出“阉割版”onenote?一篇文章快速入门onenote uwp...
- Fiddler下载安装、配置https证书、抓取手机app请求教程(官网)
- 封装仿支付宝密码输入效果
- APS高级排产软件解析
- 汇川机器人视觉定位通讯程序
- CPU-流水线的数据相关问题
- 【微前端】591- 微前端在小米 CRM 系统的实践
热门文章
- python2.x文件编码
- 你可能过于高估了机器学习算法能力,带你解读鲜为人知的数据泄露问题
- Docker与OpenStack集成实战
- ADO.NET 数据连接查询
- Ubuntu安装docker-ELK
- Solr增删改查索引
- WMI使用的WIN32_类库名
- linux下 tar解压 gz解压 bz2等各种解压文件使用方法
- 配置DATAGUARD 时关于 LOG_FILE_NAME_CONVERT配置错误的解决
- androdi 中listview.setTextFilterEnabled(boolean)的作用