A.棋盘问题——poj1321

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

Input

输入含有多组测试数据。 
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n 
当为-1 -1时表示输入结束。 
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。 


Output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。


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

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy. 
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专项训练的更多相关文章

  1. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  2. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  3. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  4. [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 ...

  5. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  6. DFS/BFS视频讲解

    视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...

  7. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  8. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  9. POJ2308连连看dfs+bfs+优化

    DFS+BFS+MAP+剪枝 题意:       就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路:      首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...

随机推荐

  1. Spring Boot教程(七)通过springboot 去创建和提交一个表单

    创建工程 涉及了 web,加上spring-boot-starter-web和spring-boot-starter-thymeleaf的起步依赖. <dependencies> < ...

  2. HTTP header 介绍 转载

    这篇文章为大家介绍了HTTP头部信息,中英文对比分析,还是比较全面的,若大家在使用过程中遇到不了解的,可以适当参考下 HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/ ...

  3. php判断两个数组是否相等

    php判断两个数组是否相等 一.总结 一句话总结: php判断两个数组是否相等可以直接上==或者===号 二.php 判断两个数组是否相等 转自或参考:php 判断两个数组是否相等https://ww ...

  4. Jmeter-app接口

    1.IOS登录接口涉及的三个接口: 2.三个接口传入的参,第三个是判断用户是否登录成功的 http://118.178.247.67:8449/service/userLogin/phoneQuick ...

  5. 安装vue模板时,选择webpack-simple还是Webpack?

    选用模板常用的是webpack与webpack-simple.webpack-simple是基于Webpack@2.1.0-beta.25进行配置的版本,而webpack模板则是基于Webpack ^ ...

  6. hashMap 底层原理+LinkedHashMap 底层原理+常见面试题

    1.源码 java1.7    hashMap 底层实现是数组+链表 java1.8 对上面进行优化  数组+链表+红黑树 2.hashmap  是怎么保存数据的. 在hashmap 中有这样一个结构 ...

  7. vue2.0中 怎么引用less?

    vue2.0中  怎么引用less? 第一步: 安装less依赖, npm install less less-loader --save 第二步: 修改webpack.config.js文件,配置l ...

  8. python定义接口继承类invalid syntax解决办法

    class s_all(metaclass=abc.ABCMeta): #python2.7用此方法定义接口继承 # __metaclass__ = abc.ABCMeta @abc.abstract ...

  9. ndroid如何监听开机广播和关机广播

    需求描述:有些时候,我们需要我们的程序在开机后能自动运行,在系统即将关闭时,能写入一些记录到指定的文件里. 一.开机广播监听: Android系统启动完成后会发出启动完成广播(android.inte ...

  10. charles-mock数据的方法(有空自己写一个)

    https://www.jianshu.com/p/75d24f264ce2 这个也可以参考下 https://www.cnblogs.com/kaibindirver/p/9104996.html ...