dfs/bfs专项训练
A.棋盘问题——poj1321
Input
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
Sample Input
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output
2
1
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = ;
char s[N][N];
int vis[N],ans,n,k;
void dfs(int len,int d){
//cout<<"len = "<<len<<"d = "<<d<<endl;
if(len >= n&&d!=k) return;
if(d == k){ ans++;return; } for(int i = ;i < n;++i){
if(vis[i]||s[len][i] =='.')continue;
if(!vis[i]||s[len][i]=='#'){
vis[i] = ;
dfs(len+,d+);
vis[i] = ;
}
}
dfs(len+,d);
}
int main()
{
while(~scanf("%d%d",&n,&k)&&(n!=-&&k!=-)){
ans = ;
for(int i = ;i < n;++i) {
scanf("%s",s[i]);vis[i] = ;
}
dfs(,);
cout<<ans<<endl;
}
return ;
}
ac代码
B.A strange lift——hdu1548
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?
InputThe input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.OutputFor each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".
Sample Input
5 1 5
3 3 1 2 5
0
Sample Output
3
//题意是 给你n代表楼层数 然后起点 终点 以及各个楼层可以上行或者下行的层数
//多组测试 0为终止标志
#include<bits/stdc++.h>
using namespace std;
const int N = ;
int vis[N],a[N]; int main()
{
int n,b,e,ans = -;
ios::sync_with_stdio(false);
while(cin>>n){
ans = -;
if(n == )break;
cin>>b>>e;
for(int i = ;i <=n;++i){
cin>>a[i];vis[i] = ;
}
queue<pair<int,int> > q;
q.push(make_pair(b,));vis[b] = ;
while(!q.empty()){ pair<int,int>s = q.front();
q.pop();
if(s.first == e){ans=s.second;break;}
if(s.first+a[s.first]<=n&&!vis[s.first+a[s.first]]){
vis[s.first+a[s.first]] = ;
q.push(make_pair(s.first+a[s.first],s.second+));
}
if(s.first-a[s.first]>=&&!vis[s.first-a[s.first]]){
vis[s.first-a[s.first]] = ;
q.push(make_pair(s.first-a[s.first],s.second+));
}
}
cout<<ans<<endl;
}
return ;
}
ac代码
C.Knight Moves——hdu1372
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
InputThe input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
OutputFor each test case, print one line saying "To get from xx to yy takes n knight moves.".
Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
//骑士是1*2的走
//自己太弱所以学习了别人的代码...才写出来
#include<bits/stdc++.h>
using namespace std;
const int N = ,inf = 1e7;
int dis[N][N];
int ty,tx,minx;
void dfs(int x,int y,int cnt){
if(x<=||y<=||x>||y>)return ;
if(cnt>=minx)return ;
if(cnt>=dis[x][y])return;
if(x==tx&&y==ty)if(cnt<minx)minx = cnt;
dis[x][y] = cnt;
dfs(x+,y+,cnt+);
dfs(x+,y-,cnt+);
dfs(x-,y+,cnt+);
dfs(x-,y-,cnt+);
dfs(x+,y+,cnt+);
dfs(x-,y+,cnt+);
dfs(x+,y-,cnt+);
dfs(x-,y-,cnt+);
return ;
}
int main()
{
char a[],b[];
while(~scanf("%s%s",a,b)){
for(int i = ;i <=;++i)
for(int j = ;j <=;++j)
dis[i][j] = inf;
minx = inf;
tx = b[]-'';ty = b[]-'a'+;
dfs(a[]-'',a[]-'a'+,);
printf("To get from %s to %s takes %d knight moves.\n",a,b,minx);
}
return ;
}
ac代码
dfs/bfs专项训练的更多相关文章
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- ID(dfs+bfs)-hdu-4127-Flood-it!
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- HDU 4771 (DFS+BFS)
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- DFS/BFS视频讲解
视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)
695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...
- POJ2308连连看dfs+bfs+优化
DFS+BFS+MAP+剪枝 题意: 就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路: 首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...
随机推荐
- k8s/Kubernetes常用组件Helm的部署
Helm的安装 1.Helm的基本概念 Helm是Kubernetes的一个包管理工具,用来简化Kubernetes应用的部署和管理.可以把Helm比作CentOS的yum工具. Helm有如下几个基 ...
- DELPHI安卓定位权限申请
DELPHI安卓定位权限申请 安卓8及以后版本的权限分为静态和动态申请2部分,而之前的安卓版本只需要静态申请权限. 1)静态申请定位权限: 2)动态申请定位权限: uses System.Permis ...
- HRNET网络结构简单分析
hrnet相关的两篇文章 CVPR2019 Deep High-Resolution Representation Learning for Human Pose Estimation High- ...
- mysql数据库学习二
最近又复习了mysql中的一些概念:视图,触发器,存储过程,函数,事务,记录下. 1.视图 视图是一个虚拟表,本身并不存储数据,当sql在操作视图时所有数据都是从其他表中查出来的,因此其本质是:根据S ...
- Go 代码审查建议
https://github.com/golang/go/wiki/CodeReviewComments https://studygolang.com/articles/6054
- Java同步数据结构之LinkedBlockingDeque
前言 前面介绍完了队列Queue/BlockingQueue的实现类,接下来介绍双端队列Deque/BlockingDeque的实现类之一LinkedBlockingDeque,它是一种基于链表的可选 ...
- Servlet的概述
A: Servlet的概述: server applet , 是一个运行在服务器端的小应用程序 B: 就是一个接口,作用: servlet 通常通过 HTTP(超文本传输协议)接收和响应来自 Web ...
- java中的异步处理和Feature接口(一)
背景介绍想象这样一个场景:你可能希望为你的法国客户提供指定主题的热点报道.为实现这一功能,你需要向 谷歌或者Twitter的API请求所有语言中针对该主题最热门的评论,可能还需要依据你的内部算法 对它 ...
- Flex 弹性盒子布局使用教程
本文转载于<https://blog.csdn.net/lyznice/article/details/53981062>
- Spring事务管理4-----声明式事务管理(2)
声明式事务管理 基于AspectJ的 XML 方式配置 通过对事务管理器TransactionManager配置通知(增强),然后再配置切点和切面,详细见applicationContext.xml ...