POJ 3083 Bfs+Dfs
注意求最短路的时候用Bfs。
- #include<iostream>
- #include<stdio.h>
- using namespace std;
- int w,h,ex,ey,sx,sy;
- int map[100][100],can[100][100];
- struct vid{
- int x,y,step;
- }queue[5000];
- int zan[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
- int dirl[4][2]={0,-1,-1,0,0,1,1,0},dirr[4][2]={0,1,1,0,0,-1,-1,0};
- int dfsr(int dstep,int x,int y,int di )
- {
- int i,temp1,temp2;
- for(i=0;i<4;i++)
- {
- temp1=x+dirr[i][0];
- temp2=y+dirr[i][1];
- if((temp1>=0)&&(temp1<h)&&(temp2>=0)&&(temp2<w))
- {
- if(dfsr(dstep+1,x+dirr[i][0],y+dirr[i][1],i))
- return dstep;
- else
- return 0;
- }
- }
- return 0;
- }
- int dfsl(int dstep,int x,int y,int di )
- {
- int i,temp1,temp2;
- for(i=0;i<4;i++)
- {
- temp1=x+dirl[i][0];
- temp2=y+dirl[i][1];
- if((temp1>=0)&&(temp1<h)&&(temp2>=0)&&(temp2<w))
- {
- if(dfsl(dstep+1,temp1,temp2,i))
- return dstep;
- else
- return 0;
- }
- }
- return 0;
- }
- int bfs()
- {
- if (sx == ex && sy == ey)
- {
- return 1;
- }
- int t,ww,x,y,t1,t2;
- t=ww=1;
- queue[t].x=sx;
- queue[t].y=sy;
- queue[t].step=0;
- can[sx][sy]=1;
- while(t<=ww&&!can[ex][ey])
- {
- x=queue[t].x;
- y=queue[t].y;
- for(int i=0;i<4;i++)
- if((!map[x+zan[i][0]][y+zan[i][1]])&&(!can[x+zan[i][0]][y+zan[i][1]]))
- {
- t1=x+zan[i][0];
- t2=y+zan[i][1];
- if(t1>=0&&(t1<h)&&(t2>=0)&&(t2<w)&&(!map[t1][t2])&&(!can[t1][t2]))
- {
- queue[++ww].x=t1;
- queue[ww].y=t2;
- queue[ww].step=queue[t].step+1;
- can[t1][t2]=1;
- }
- }
- t++;
- }
- return queue[ww].step+1;
- }
- int main ()
- {
- int t;
- char c;
- scanf("%d",&t);
- getchar();
- while(t--)
- {
- scanf("%d%d",&w,&h);
- getchar();
- for(int i=0;i<h;i++)
- {
- for(int j=0;j<w;j++)
- {
- can[i][j]=0;
- c=getchar();
- if(c=='#')
- map[i][j]=1;
- else if(c=='.')
- map[i][j]=0;
- else if(c=='S')
- {
- map[i][j]=0;
- sx=i,sy=j;
- }
- else
- if(c=='E')
- {
- map[i][j]=0;
- ex=i;ey=j;
- }
- }
- getchar();
- }
- // init();
- // dfsr();
- // printf("%d ",bfs());
- printf("%d %d %d\n",dfsl(0,sx,sy,0)+1,dfsr(0,sx,sy,0)+1,bfs());
- }
- return 0;
- }
POJ 3083 Bfs+Dfs的更多相关文章
- POJ 3083 BFS+DFS 40行
题意:给你一个迷宫. 先输出当左转优先的时候走的路程长度,再输出当右转优先时走的路程长度,最后输出从起点到终点的最短路程长度. 嗯嗯 奴哥活跃气氛的题.随便写了写.. 此题 知道了思路以后就是水题了. ...
- Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)
Q - 迷宫问题 POJ - 3984 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- poj 3083 dfs,bfs
传送门 Children of the Candy Corn Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- 搜索入门_简单搜索bfs dfs大杂烩
dfs题大杂烩 棋盘问题 POJ - 1321 和经典的八皇后问题一样. 给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...
- 邻结矩阵的建立和 BFS,DFS;;
邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- Collect More Jewels(hdu1044)(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- 20145127《java程序设计》第九周学习总结
一.教材学习内容总结 第十六章 整合数据库 16.1 JDBC入门 JDBC(Java DataBase Connectivity) 驱动的四种类型 JDBC-ODBC Bridge Driver N ...
- 20145317彭垚 MSF基础应用
20145317彭垚 MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode? exploit就相当于是载具,将真正要负责攻击的代码传送到靶机中,我觉得老师上课 ...
- Python3基础 ** 幂运算 // 整除运算
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- thymeleaf和easyui配合可能出现的错误
thymeleaf和easyui 在easyui的内页,不再使用th:href引入静态资源文件. 在easyui页面中,script执行easyui自己的方法要加入: <script th:in ...
- Nlog、elasticsearch、Kibana以及logstash在项目中的应用(一)
前言 最近在做文档管理中,需要记录每个管理员以及用户在使用过程中的所有操作记录,本来是通过EF直接将操作数据记录在数据库中,在查询的时候直接从数据库中读取,但是这样太蠢了,于是在网上找到了logsta ...
- python 操作浏览器打开指定网页
#! /usr/bin/env python # encoding=utf8 import webbrowser import time webbrowser.open("http://ww ...
- 用java代码将数组元素顺序颠倒
package test; public class Recover { public int[] reverse(int[] a) { int[] b = new int[a.length]; in ...
- Linux忘记root登录密码解决方法
有时候由于长时间米有登录linux系统,等需要用的时候突然忘记root密码,怎么办?下面简单介绍解决方法. redhat 和 centos 6.5 可以,7.0以上未测 在系统重启后,不停地按”e”键 ...
- Linux - 命令重定向
命令重定向, 就是将目前得到的数据转移到指定的地方.分为以下几种: >>>1>2>1>>2>>< 1. > 与 >>先看一 ...
- Android中如何实现EditText的自动换行
要实现EditText的自动换行需要实现如下设置: <EditText android:id="@+id/function_lifingcost_edit_txtRemark" ...