搜索入门题合集

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<cstring>
using namespace std;
#define ll long long
const int M = 10;
int vis[M],n,k,cnt;
string s[M];
void dfs(int r,int num){
    if(num == k){
        cnt ++;
        return ;
    }
    if(r == n) return ;
    for(int i = 0;i < n;i ++){
        if(s[r][i]=='#'&&!vis[i]){
            vis[i] = 1;
            dfs(r+1,num+1);
            vis[i] = 0;
        }
    }
    dfs(r+1,num);
}
int main()
{
    while(cin>>n>>k){
        if(n==-1&&k==-1) break;
        for(int i = 0;i < n;i ++){
            cin>>s[i];
        }
        cnt = 0;
        memset(vis,0,sizeof(vis));
        dfs(0,0);
        cout<<cnt<<endl;
    }
    return 0;
}

 POJ - 2251 Dungeon Master

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 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

基础bfs,之前用dfs写了一发超时。

实现代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int inf = 1e9+7;
int minn,c,l,r,x1,y1,z1,x2,z2,y2;
bool vis[40][40][40];
char mp[40][40][40];
int d[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
struct node{
    int x,y,z,step;
};
int check(int x,int y,int z){
    if(x<0||x>=c||y<0||y>=l||z<0||z>=r||vis[x][y][z]||mp[x][y][z]=='#')
        return 0;
    return 1;
}

int bfs(){
    queue<node>q;
    node now,next;
    now.x = x1;now.y=y1;now.z=z1;now.step = 0;
    vis[x1][y1][z1] = 1;
    q.push(now);
    while(!q.empty()){
        now = q.front();
        q.pop();
        if(now.x == x2&&now.y == y2&&now.z == z2)
            return now.step;
        for(int i = 0;i < 6;i++){
            next.x = now.x + d[i][0];
            next.y = now.y + d[i][1];
            next.z = now.z + d[i][2];
            if(check(next.x,next.y,next.z)){
                vis[next.x][next.y][next.z] = 1;
                next.step=now.step+1;
                q.push(next);
            }
        }
    }
    return 0;
}
int main()
{
    while(scanf("%d%d%d",&c,&l,&r)){
    //init();
    if(c==0&&l==0&&r==0) break;
    getchar();
    for(int i = 0;i < c;i ++){
        for(int j = 0;j < l;j ++){
            scanf("%s",&mp[i][j]);
            for(int k = 0;k < r;k ++){
                vis[i][j][k] = 0;
                if(mp[i][j][k] == 'S'){
                    x1 = i; y1 = j; z1 = k;
                }
                if(mp[i][j][k]=='E'){
                    x2 = i; y2 = j;z2 = k;
                }
            }
        }
    }
    int ans = bfs();
    if(ans==0)  printf("Trapped!
");
    else printf("Escaped in %d minute(s).
",ans);
    }
}

  POJ - 3278  Catch That Cow

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.
 
基础bfs
 
实现代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int n,k;
int step[100010];
bool vis[100010];
queue<int>q;
int bfs(){
    int now = n; step[now] = 0;
    q.push(now);
    while(!q.empty()){
        now = q.front();
        q.pop();
        int next;
        for(int i = 0;i < 3;i ++){
            if(i == 0)  next  = now + 1;
            else if(i == 1) next = now - 1;
            else next = now * 2;
            if(next < 0||next >= 100010) continue;
            if(!vis[next]){
                vis[next] = 1;
                step[next] = step[now] + 1;
                q.push(next);
            }
            if(next == k) return step[next];
        }
    }
    return 0;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>n>>k;
    if(n>=k) cout<<n-k<<endl;
    else {
    int ans = bfs();
    cout<<ans<<endl;
    }
    return 0;
}
 

poj 1426 Find The Multiple

 
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
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111

基础bfs,

实现代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define ll unsigned long long
ll n;
ll bfs(){
    queue<ll>q;
    q.push(1);
    while(!q.empty()){
        ll k = q.front();
        q.pop();
        if(k/n*n==k){
            return k;
        }
        q.push(k*10);
        q.push(k*10+1);
    }
    return 0;
}
int main()
{
    ll ans;
    while(cin>>n){
        if(n==0) break;
        ans = bfs();
        cout<<ans<<endl;
    }
}

 POJ - 3126 Prime Path

 题目链接:http://poj.org/problem?id=3126

思路:

bfs暴力搜出四个位数的所有可能组合,逐个判断是否是素数,是的话加入队列并标记当前数字。之前想把数字转换成字符串去操作,结果转了半天把自己绕昏了。。直接暴力搜就好了

实现代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
using namespace std;
struct node{
    int num,step;
};
node a;
int b;
int vis[10000];
bool isprim( int num )
{
                 if(num ==2|| num==3 )
                    return 1 ;
                 if(num %6!= 1&&num %6!= 5)
                        return 0 ;
                 int tmp =sqrt( num);
                 for(int i= 5;i <=tmp; i+=6 )
                    if(num %i== 0||num %(i+ 2)==0 )
                        return 0 ;
                 return 1 ;
}

int bfs(){
    node now,next;
    queue<node>q;
    a.step = 0;
    q.push(a);
    while(!q.empty()){
        now = q.front();
        q.pop();
        //cout<<now.num<<endl;
        int w0 = now.num%10;
        int w1 = (now.num/10)%10;
        int w2 = (now.num/100)%10;
        int w3 = (now.num/1000)%10;
        //cout<<w0<<" "<<w1<<" "<<w2<<" "<<w3<<endl;
        for(int i = 0;i < 10;i ++){
            if(w0==i) continue;
            next.num = i+w1*10+w2*100+w3*1000;
            next.step = now.step + 1;
            //cout<<next.num<<endl;
            if(next.num == b) return next.step;
            if(isprim(next.num)&&!vis[next.num]){
                vis[next.num] = 1;
                q.push(next);
            }
        }
        for(int i = 0;i < 10;i ++){
            if(w1==i) continue;
            next.num = w0+i*10+w2*100+w3*1000;
            next.step = now.step + 1;
            //cout<<next.num<<endl;
            if(next.num == b) return next.step;
            if(isprim(next.num)&&!vis[next.num]){
                vis[next.num] = 1;
               q.push(next);
            }
        }
        for(int i = 0;i < 10;i ++){
            if(w2==i) continue;
            next.num = w0+w1*10+i*100+w3*1000;
            next.step = now.step + 1;
            //cout<<next.num<<endl;
            if(next.num == b) return next.step;
            if(isprim(next.num)&&!vis[next.num]){
                vis[next.num] = 1;
                q.push(next);
            }
        }
        for(int i = 1;i < 10;i ++){
            if(w3==i) continue;
            next.num = w0+w1*10+w2*100+i*1000;
            next.step = now.step + 1;
            //cout<<next.num<<endl;
            if(next.num == b) return next.step;
            if(isprim(next.num)&&!vis[next.num]){
                vis[next.num] = 1;
               q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        memset(vis,0,sizeof(vis));
        cin>>a.num>>b;
        if(a.num == b) cout<<0<<endl;
        else{
        int ans = bfs();
        if(ans == -1)
            cout<<"Impossible"<<endl;
        else
            cout<<ans<<endl;
        }
    }
}
原文地址:https://www.cnblogs.com/kls123/p/8671904.html