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的连连看状态,然后问你最后能不能全部消没? 思路: 首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...
随机推荐
- 有向网络(带权的有向图)的最短路径Dijkstra算法
什么是最短路径? 单源最短路径(所谓单源最短路径就是只指定一个顶点,最短路径是指其他顶点和这个顶点之间的路径的权值的最小值) 什么是最短路径问题? 给定一带权图,图中每条边的权值是非负的,代表着两顶点 ...
- layer 漂亮的弹窗
layer.confirm('<font color="red">请认真核对账目信息,提交后将不可撤回!!</font>', { icon:3, title ...
- Java主线程在子线程执行完毕后再执行
一.join() Thread中的join()方法就是同步,它使得线程之间由并行执行变为串行执行. public class MyJoinTest { public static void main( ...
- 树莓派中将caplock映射为esc键
据说,喜欢vimer都呵caplock有仇,明明caplock占着原来esc的位置,却从来没有起到应有的作用,你说气人吗,没关系,我改啊:将下面语句加入到.bashrc中,启动即可xmodmap -e ...
- nginx使用certbot配置https
一般现在的网站都要支持https,即安全的http. 机器:阿里云Ubuntu 16.04.3 LTS 方案一:自己申请证书 配置时需要确保有ssl模块, 之后域名解析下, 之后时申请证书,可以去阿里 ...
- Python 中操作 MySQL
引入模块 在py文件中引入pymysql模块 from pymysql import * Connection 对象 用于建立与数据库的连接 创建对象:调用connect()方法 conn=conne ...
- fiddler 基本知识(一)
工作原理: fiddler,原理是在本机开启了一个http代理服务器,默认监听127.0.0.1:8888. 打开fiddler之后,会帮我们设置ie代理,虽然只设置了ie,但整个计算机都通过整个代理 ...
- Linux设备驱动程序 之 per-cpu变量
数组形式 支持SMP的现代操作系统使用每个cpu上的数据,对于给定的处理器其数据是唯一的:一般来说,每个cpu的数据存放在一个数组中,数组总的每一项对应着系统上的一个存在的处理器:按当前处理器号确定这 ...
- Get Raster Properties获得栅格的信息
Summary Returns the properties of a raster dataset. Usage The property returned will be displayed in ...
- 通过generate解析SQL日志生成xml进行SQL回放
查看Oracle redo日志来分析SQL执行记录 1)设置Oracle数据字典导出路径参数(可选) shutdown immediatealter system set UTL_FILE_DIR=' ...