专题一 简单搜索题集

来源:互联网 发布:域名到期查询 编辑:程序博客网 时间:2024/05/19 20:22
棋盘问题
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 42226 Accepted: 20563

Description

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放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

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;}

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 31463 Accepted: 12102

Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. 

Is an escape possible? If yes, how long will it take? 

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
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

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 
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;    }

Catch That Cow
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 85814 Accepted: 26925

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 - 1 or + 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

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.


/*  题意:给你两个数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;}

Find The Multiple
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 29996 Accepted: 12464 Special Judge

Description

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.

Input

The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.

Output

For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

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


Problem Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
 

Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
 

Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
 

Sample Input
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
 

Sample Output
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;}




0 0