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的连连看状态,然后问你最后能不能全部消没? 思路: 首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...
随机推荐
- NSArray 的创建和遍历
数组 用来存贮对象的有序列表,它是不可变的 不能存数C语言的基本数据类型 只支持OC对象 #pragma mark Create a array //Initialize NSArray void a ...
- 64位linux下玩32位汇编编程
利用下假期,打算把linux下的汇编语言给熟悉下,结果是以32位为版本的,只能在办公室的机器上跑了个opensuse的32位版本,家里的suse挂了,无法输入中文.打算再安装下32位系统,今天找到了个 ...
- tomcat+myeclipse+mysql环境搭建
毕业设计要做的题目,搭环境我就搭了两天...网上的教程都好碎啊.. 我的需求是这样,我有一个Android app需要与电脑上的数据库交换信息,实现增删查改,这样不管用什么方法,电脑上都要有一个服务器 ...
- tomcat单机多实例
catalina.home指向公用信息的位置,就是bin和lib的父目录. catalina.base指向每个Tomcat目录私有信息的位置,就是conf.logs.temp.webapps和work ...
- arcpy workspace already in transaction mode
arcpy workspace already in transaction mode RuntimeError: workspace already in transaction mode 同一个工 ...
- kafka 基本原理简介
Kafka是啥?用Kafka官方的话来说就是: Kafka is used for building real-time data pipelines and streaming apps. It i ...
- 从UDP的”连接性”说起–告知你不为人知的UDP
原文地址:http://bbs.utest.qq.com/?p=631 很早就计划写篇关于UDP的文章,尽管UDP协议远没TCP协议那么庞大.复杂,但是,要想将UDP描述清楚,用好UDP却要比TCP难 ...
- [转]用代码访问 Https
可以参考: https://blog.csdn.net/irokay/article/details/78801307 跳过证书验证方法 HttpClient简介HTTP 协议可能是现在 Intern ...
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_8.RabbitMQ研究-工作模式-发布订阅模式-生产者
Publish/subscribe:发布订阅模式 发布订阅模式: 1.每个消费者监听自己的队列. 2.生产者将消息发给broker,由交换机将消息转发到绑定此交换机的每个队列,每个绑定交换机的队列都将 ...
- linux简单命令5---开机与重启
时间可以写为:now.shutdown命令是安全的命令(保存运行程序) 2:下面为其他不安全的关机命令 必须正确退出登录,window是注销