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
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 }
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 }
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的走
//自己太弱所以学习了别人的代码...才写出来
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 }
推荐阅读
-
各种迷迷迷宫问题 深搜dfs和广搜bfs做法
-
算法学习笔记 二叉树和图遍历—深搜 DFS 与广搜 BFS
-
图的邻接矩阵、邻接表遍历(python实现)(递归与非递归)(dfs bfs)
-
无向图(无权值)的邻接矩阵与邻接表储存方式及其DFS,BFS遍历
-
POJ3984 迷宫问题记录路径递归 bfs HDU1242 dfs Codeforces25D.Roads in Berland floyd优化 HDU1874畅通工程续 floyd/spfa/dj
-
搜索与图论——DFS与BFS
-
搜索与图论---DFS和BFS、树与图的存储和遍历
-
DFS和BFS的实现(邻接矩阵、邻接表)
-
求连通分量(DFS)(BFS)(STL)
-
岛屿数量(经典 DFS 和 BFS 高频题 商汤、字节面试题)