dfs/bfs专项训练

2019-08-16 07:57:17来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

dfs/bfs专项训练

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

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 const int N = 15;
 6 char s[N][N];
 7 int vis[N],ans,n,k;
 8 void dfs(int len,int d){
 9     //cout<<"len = "<<len<<"d = "<<d<<endl;
10     if(len >= n&&d!=k) return;
11     if(d == k){ ans++;return; }
12 
13     for(int i = 0;i < n;++i){
14         if(vis[i]||s[len][i] =='.')continue;
15         if(!vis[i]||s[len][i]=='#'){
16             vis[i] = 1;
17             dfs(len+1,d+1);
18             vis[i] = 0;
19         }
20     }
21     dfs(len+1,d);
22 }
23 int main()
24 {
25     while(~scanf("%d%d",&n,&k)&&(n!=-1&&k!=-1)){
26         ans = 0;
27         for(int i = 0;i < n;++i) {
28             scanf("%s",s[i]);vis[i] = 0;
29         }
30         dfs(0,0);
31         cout<<ans<<endl;
32     }
33     return 0;
34 }
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为终止标志

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N = 250;
 4 int vis[N],a[N];
 5 
 6 int main()
 7 {
 8     int n,b,e,ans = -1;
 9     ios::sync_with_stdio(false);
10     while(cin>>n){
11             ans = -1;
12         if(n == 0)break;
13         cin>>b>>e;
14     for(int i = 1;i <=n;++i){
15         cin>>a[i];vis[i] = 0;
16     }
17     queue<pair<int,int> > q;
18     q.push(make_pair(b,0));vis[b] = 1;
19     while(!q.empty()){
20 
21         pair<int,int>s = q.front();
22         q.pop();
23         if(s.first == e){ans=s.second;break;}
24         if(s.first+a[s.first]<=n&&!vis[s.first+a[s.first]]){
25             vis[s.first+a[s.first]] = 1;
26             q.push(make_pair(s.first+a[s.first],s.second+1));
27         }
28         if(s.first-a[s.first]>=1&&!vis[s.first-a[s.first]]){
29             vis[s.first-a[s.first]] = 1;
30             q.push(make_pair(s.first-a[s.first],s.second+1));
31         }
32     }
33       cout<<ans<<endl;
34     }
35     return 0;
36 }
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的走

//自己太弱所以学习了别人的代码...才写出来

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N = 25,inf = 1e7;
 4 int dis[N][N];
 5 int ty,tx,minx;
 6 void dfs(int x,int y,int cnt){
 7     if(x<=0||y<=0||x>8||y>8)return ;
 8     if(cnt>=minx)return ;
 9     if(cnt>=dis[x][y])return;
10     if(x==tx&&y==ty)if(cnt<minx)minx = cnt;
11     dis[x][y] = cnt;
12     dfs(x+2,y+1,cnt+1);
13     dfs(x+2,y-1,cnt+1);
14     dfs(x-2,y+1,cnt+1);
15     dfs(x-2,y-1,cnt+1);
16     dfs(x+1,y+2,cnt+1);
17     dfs(x-1,y+2,cnt+1);
18     dfs(x+1,y-2,cnt+1);
19     dfs(x-1,y-2,cnt+1);
20     return ;
21 }
22 int main()
23 {
24     char a[5],b[5];
25     while(~scanf("%s%s",a,b)){
26         for(int i = 1;i <=8;++i)
27             for(int j = 1;j <=8;++j)
28                 dis[i][j] = inf;
29         minx = inf;
30         tx = b[1]-'0';ty = b[0]-'a'+1;
31         dfs(a[1]-'0',a[0]-'a'+1,0);
32         printf("To get from %s to %s takes %d knight moves.\n",a,b,minx);
33     }
34     return 0;
35 }
ac代码

 

 

 


原文链接:https://www.cnblogs.com/h404nofound/p/11280864.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:从存图到最短路算法的图论总结

下一篇:浅谈高精度算法(加减乘除)