专题一 简单搜索题集
来源:互联网 发布:域名到期查询 编辑:程序博客网 时间:2024/05/19 20:22
Description
Input
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
Sample Input
2 1#..#4 4...#..#..#..#...-1 -1
Sample Output
21
/* 要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列。 这一个细节要考虑好该如何处理。 写这题可能会因为写成visited[i][j]来判断是否重复 这样的话就不能很好的处理这个情况 应该用一维的 visited[j]来判断 因为是按行进行dfs 如果某行的某一列*/#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>#include <string>#include <cstdlib>using namespace std;char mp[10][10];int n,k;int ans=0;bool visited[10];void dfs(int row,int cnt)//定义当前要dfs的行号 和 已经放了几个棋子;{ if(cnt==k) { ans++; return ; } if(row>=n) return ;//防止越界 for(int j=0;j<n;j++) { if(!visited[j]&&mp[row][j]=='#') { visited[j]=true; //标记 dfs(row+1,cnt+1); visited[j]=false; //回溯 } } dfs(row+1,cnt);//这是一个值得注意的地方。 //当k<n时候 可能没有遍历完全部行的时候cnt就已经达到了k 所以为了处理这种情况 //当前行不放棋子 在下一行放棋子 return ;}/* 写法二:基本原理是一样的 相对更加的直观 可以避免考虑很多细节的坑。 dfs(-1,0);void dfs(int row,int num){ if(num==k) { ans++; return ; } for(int i=row+1; i<n; i++) { for(int j=0; j<n; j++) { if(mp[i][j]=='#'&&!visited[j]) { visited[j]=true; dfs(i,num+1); visited[j]=false; } } } return ;}*/int main(void){while(scanf("%d%d",&n,&k)!=EOF) { if(n==-1&&k==-1) break; ans=0; memset(visited,false,sizeof(visited)); for(int i=0;i<n;i++) scanf("%s",mp[i]); dfs(0,0); printf("%d\n",ans); }return 0;}
Description
Is an escape possible? If yes, how long will it take?
Input
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!
/* 题意:三维的空间中L,R,C;起点为S 终点为E It takes one minute to move one unit north, south, east, west, up or down need to find the quickest way out! 在不同L维的图中,相同R和C坐标处是可以穿梭的 最短路问题直接用BFS即可。 */ #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <string> #include <cstdlib> #include <queue> using namespace std; int L,R,C; struct node {int x,y,z,step; }S,E; int flag=0,ans=0; int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}}; char mp[50][50][50]; bool visited[50][50][50]; bool check(node a) { if(a.x>=0&&a.x<L&&a.y>=0&&a.y<R&&a.z>=0&&a.z<C) return true; return false; } int bfs(node start,node end) { queue<node>Q; start.step=0; Q.push(start); visited[start.x][start.y][start.y]=true; while(!Q.empty()) { node head=Q.front(); Q.pop(); for(int i=0;i<6;i++) { node next; next.x=head.x+dir[i][0]; next.y=head.y+dir[i][1]; next.z=head.z+dir[i][2]; if(check(next)&&!visited[next.x][next.y][next.z]&&mp[next.x][next.y][next.z]!='#') { next.step=head.step+1; Q.push(next); visited[next.x][next.y][next.z]=true; } if(next.x==end.x&&next.y==end.y&&next.z==end.z) { flag=1; return next.step; } } if(flag) return 0; } return 0; } int main(void) { while(scanf("%d%d%d",&L,&R,&C)!=EOF) { if(L==0&&R==0&&C==0) break; flag=0; memset(visited,false,sizeof(visited)); for(int i=0;i<L;i++) { for(int j=0;j<R;j++) scanf("%s",mp[i][j]); } for(int i=0;i<L;i++) { for(int j=0;j<R;j++) { for(int k=0;k<C;k++) { if(mp[i][j][k]=='S') { S.x=i; S.y=j; S.z=k; S.step=0; } if(mp[i][j][k]=='E') { E.x=i; E.y=j; E.z=k; E.step=0; } } } } ans=bfs(S,E); if(flag) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } return 0; }
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
/* 题意:给你两个数n k 从n到k最少需要多少分钟,一共有以下三种操作: X - 1 or X + 1 or 2 × X 每个操作花费1分钟。 简单bfs。 */#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>#include <string>#include <cstdlib>#include <queue>using namespace std;const int maxn = 100000+100;bool visited[maxn];int dist[maxn];int flag=0;int bfs(int n,int k){ queue<int>Q; Q.push(n); dist[n]=0; visited[n]=true; while(!Q.empty()) { int head=Q.front(); Q.pop(); for(int i=0;i<3;i++) { int next; if(i==0) next=head-1; else if(i==1) next=head+1; else if(i==2) next=head*2; if(!visited[next]&&next<=maxn&&next>=0) { Q.push(next); dist[next]=dist[head]+1; visited[next]=true; if(next==k) { flag=1; return dist[next]; } } } } return 0;}int main(void){ int n,k; while(scanf("%d%d",&n,&k)!=EOF) { flag=0; memset(visited,false,sizeof(visited)); memset(dist,0,sizeof(dist)); int ans=bfs(n,k); if(n>=k) printf("%d\n",n-k); else printf("%d\n",ans); } return 0;}
Description
Input
Output
Sample Input
26190
Sample Output
10100100100100100100111111111111111111
/* 题意:给你一个数n 他的k次倍只包含0和1 输出这个数。 暴力dfs。*/#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>#include <string>#include <cstdlib>#include <queue>using namespace std;int flag=0;void dfs(unsigned __int64 num,int n,int k){ if(flag) return ; if(num%n==0) { printf("%I64u\n",num); flag=1; return ; } if(k==19) return; dfs(num*10,n,k+1); dfs(num*10+1,n,k+1);}int main(void){ int n; while(scanf("%d",&n)!=EOF,n) { flag=0; dfs(1,n,0); } return 0;}
Oil Deposits
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
0122
/* 水题,DFS求联通块的入门题目。*/#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>#include <string>#include <cstdlib>#include <queue>using namespace std;char mp[110][110];int n,m;bool check(int x,int y){ if(0<=x&&x<n&&y>=0&&y<m)return true; return false;}void dfs(int x,int y){ mp[x][y]='*'; for(int dx=-1;dx<=1;dx++) { for(int dy=-1;dy<=1;dy++) { int nx=x+dx,ny=y+dy; if(check(nx,ny)&&mp[nx][ny]=='@') dfs(nx,ny); } }}int main(void){ while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; for(int i=0;i<n;i++) scanf("%s",mp[i]); int res=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(mp[i][j]=='@') { dfs(i,j); res++; } } } printf("%d\n",res); } return 0;}
- 专题一 简单搜索题集
- 专题一 简单搜索 题集
- kuangbin专题一简单搜索总结
- kuangbin专题一 简单搜索 题解思路
- kuangbin [简单搜索专题]
- 简单搜索专题
- [kuangbin带你飞]专题一 简单搜索 K题迷宫
- [kuangbin带你飞]专题一 简单搜索 刷题总结
- [kuangbin神带你飞]专题一 简单搜索
- kuangbin带你飞 专题一 简单搜索 (题解)
- Fire Game [kuangbin带你飞]专题一 简单搜索
- [kuangbin带你飞]专题一 简单搜索 总结
- Kuangbin带你飞专题一 简单搜索
- [kuangbin带你飞]专题一 简单搜索 D - Fliptile
- [kuangbin带你飞]专题一 简单搜索-J - Fire!
- [kuangbin带你飞]专题一 简单搜索 - H - Pots
- [kuangbin带你飞]专题一 简单搜索 A POJ1321
- [kuangbin带你飞]专题一 简单搜索 B POJ2251
- CSS样式自动换行(强制换行)
- Codeforces Round #369 (Div. 2) C. Coloring Trees 动态规划
- MySQL性能优化的最佳20+条经验
- 使用Nexus Repository 3的十二个理由
- 算法之二分搜索树C语言实现
- 专题一 简单搜索题集
- [编程题] 连续整数
- 斜率优化
- Boost之LockFree
- Lecture 7: Designing Specifications
- 利用GBDT模型构造新特征
- 华为机试-查找组成一个偶数最接近的两个素数
- how to listen to N channels? (dynamic select statement)
- 第三方组件BottomBar使用