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. 最简单的babel+webpack配置

    首先先介绍一下2个重要的库:core-js 和 regenerator core-js core-js 是用于 JavaScript 的组合式标准化库,它包含 es5 (e.g: object.fre ...

  2. [linux]sudo 出现unable to resolve host 解决方法

    Ubuntu环境, 假设这台机器名字(hostname)叫abc, 每次执行sudo 就出现这个警告讯息:sudo: unable to resolve host abc虽然sudo 还是可以正常执行 ...

  3. JavaWeb_(Spring框架)认识Spring中的aop

    1.aop思想介绍(面向切面编程):将纵向重复代码,横向抽取解决,简称:横切 2.Spring中的aop:无需我们自己写动态代理的代码,spring可以将容器中管理对象生成动态代理对象,前提是我们对他 ...

  4. k8s之yaml详解

    k8s之yaml详解 apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数 ...

  5. Qtcreator中printf()/fprintf()不显示问题处理方法

    此处只介绍解决办法,有兴趣的朋友可以分析原因. [问题] 使用Qtcreator开发项目中,printf()的诊断信息,在“应用程序输出”窗口不显示. [解决方法] 1.printf()不显示解决示例 ...

  6. Linux设备驱动程序 之 软中断

    软中断保留给系统中对时间要求严格以及最重要的下半部使用:目前,只有两个子系统(网络和SCSI)直接使用软中断:此外,内核定时器和tasklet都是建立在软中断上的:在使用软中断之前,要先确定为什么不能 ...

  7. Qt configure 参数

    在编译QT前,可加各种参数来定制自己想要的QT库.这对需要裁减QT库的朋友来说非常重要.对于如何编译QT,可以参考:http://hi.baidu.com/agassi%5Fp/blog/item/4 ...

  8. 推送kafka消息失败

    晚上变更 怎么都推不过去,蛋疼,睡饱后加了个hosts没想到好了,然后搜了一下,大概是如下的原因 转自 https://www.cnblogs.com/linlianhuan/p/9258061.ht ...

  9. Vue于React特性对比(三)

    最近重学React,再次和vue做了对比. 一,为官方插件提供便利的第三方插件横行 React仅仅是一个ui框架.虽然官方提供了redux,react-router:但也有第三方的redux-thun ...

  10. <JavaScript> 寄生继承详解

    // 将原型继承和非原型继承组合为一体的继承方式叫做组合继承,但是这种方法的继承是有一点小缺陷的,下级函数继承了无用的属性,所以我们有了寄生继承来解决污染问题; //创建上级构造函数-食物 funct ...