kuangbin专题总结一 简单搜索

A - 棋盘问题:在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

解题思路:DFS,在这里有两个搜索方向,同时对每个位置的描述由xy坐标完成,第一次我尝试使用pair+vector保存棋盘位置,用两个数组描述放过棋子的行和列但是由于清除标记没做好WA了。这里是因为DFS搜索中状态转移没确定好,导致清楚标记复杂而出错,改为逐行递归逐列遍历。在这里要注意用getchar()清除读取时回车,还有一次WA是因为把边界检查放在了结果检查的后边(因为检测num修改cnt实在DFS(i+1)中,先进行边界检查就导致解 的结果变少)。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 10;
char g[MAXN][MAXN];
int place[MAXN],n,k,cnt,num;
void DFS(int i)//按行递归,逐列遍历
{
    if(num==k)
    {
        cnt++;
        return ;
    }
    if(i>=n)
        return ;
    for(int j=0;j<n;j++)
    {
        if(!place[j]&&g[i][j]==‘#‘)
        {
            place[j] = 1;
            num++;
            DFS(i+1);
            place[j] = 0;//清除标记
            num--;
        }
    }
    DFS(i+1);//i行也可以不放棋子
}
int main()
{
    while(scanf("%d%d",&n,&k))
    {
        if(n==-1&&k==-1)
            break;
        char c;
        getchar();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                scanf("%c",&g[i][j]);
            getchar();
        }
        cnt = 0;
        num = 0;
        memset(place,0,sizeof(place));
        DFS(0);
        cout<<cnt<<endl;
    }
}

B - 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?

这是一个三维BFS搜索,确定方向设置三维数组解决。注意DFS和BFS的使用场合,DFS适合求出路径和具体信息,BFD适合求出最短路径和长度,这里显然用BFS比较好。。

#include <iostream>
#include<vector>
#include<queue>
#include<string>
#define MAX 31
using namespace std;
int dir[6][3] = { {1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1} };
struct node{
    int l;
    int r;
    int c;
    int step;
};
int dfs(node start,node to);
void Read(int l,int r,int c);

char g[MAX][MAX][MAX];
node start,to;
int l,r,c,d=0;
bool judge(int tl,int tr,int tc)
{
    if(tl<0||tr<0||tc<0||tl>=l||tr>=r||tc>=c||g[tl][tr][tc]==‘#‘)
        return false;
    return true;
}
int main()
{
    cin>>l>>r>>c;
    while(l!=0&&r!=0&&c!=0)
    {
        Read(l,r,c);
        d = dfs(start,to);
        if(d != -1)
            printf("Escaped in %d minute(s).\n", d);
        else
            printf("Trapped!\n");
        cin>>l>>r>>c;
    }
}
void Read(int l,int r,int c)
{
    int k,i,j;
    string str;
    for(k=0;k<l;k++)
    {
        for(i=0;i<r;i++)
        {
            cin>>str;
            for(j=0;j<c;j++)
            {
                if(str[j]==‘S‘)
                {
                    start.r =i;
                    start.c = j;
                    start.l = k;
                }
                if(str[j]==‘E‘)
                {
                    to.r = i;
                    to.c = j;
                    to.l = k;
                }
                g[k][i][j] = str[j];
            }
        }
        getchar();
    }
}
int dfs(node start,node to)
{
    int distance = 0;
    queue<node> Q;
    Q.push(start);
    while(!Q.empty())
    {
        start = Q.front();
        Q.pop();
        if(start.c==to.c&&start.r==to.r&&start.l==to.l)
            return start.step;
        for(int i=0; i<6; i++)
        {
            node q = start;
            q.r += dir[i][0];
            q.c += dir[i][1];
            q.l += dir[i][2];
            q.step += 1;

            if(judge(q.l,q.r,q.c))
            {
                g[q.l][q.r][q.c] = ‘#‘;
                Q.push(q);
            }
        }
    }
    return -1;
}

C - Catch That Cow

armer 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?

这个题目WA了好多次,,首先确定用数组保存到达每个点的最短距离和到达的步数,然后BFS分别在三种情况下搜索,已经搜索过了就不用再次搜素(step肯定比之前大,就不是最优解了),这里主要是没有注意到用数组保存状态还有边界检查,导致超时。

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int N = 1000000;
int map[N+10];
int n,k;
struct node
{
    int x,step;
};
int check(int x)
{
    if(x<0 || x>=N || map[x])
        return 0;
    return 1;
}
int bfs(int x)
{
    int i;
    queue<node> Q;
    node a,next;
    a.x = x;
    a.step = 0;
    map[x] = 1;
    Q.push(a);
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
        if(a.x == k)
            return a.step;
        next = a;
        //每次都将三种状况加入队列之中
        next.x = a.x+1;
        if(check(next.x))
        {
            next.step = a.step+1;
            map[next.x] = 1;
            Q.push(next);
        }
        next.x = a.x-1;
        if(check(next.x))
        {
            next.step = a.step+1;
            map[next.x] = 1;
            Q.push(next);
        }
        next.x = a.x*2;
        if(check(next.x))
        {
            next.step = a.step+1;
            map[next.x] = 1;
            Q.push(next);
        }
    }
    return -1;
}  

int main()
{
    int ans;
    while(~scanf("%d%d",&n,&k))
    {
        memset(map,0,sizeof(map));
        ans = bfs(n);
        printf("%d\n",ans);
    }
    return 0;
}  

E - 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.

BFS搜素,两个状态是ans = ans*10+1,ans*=10,注意边界检查

#include <stdio.h>
int n,flat;
unsigned long long b;
void DFS(unsigned long long a,int step)
{
    if(flat||step==19)
    {
        return ;
    }
    if(a%n==0)
    {
        printf("%I64u\n",a);
        flat=1;
        return ;
    }
    else
    {
        DFS(a*10,step+1);
        DFS(a*10+1,step+1);
    }
    return ;
}
int main()
{
    while(scanf("%d",&n),n)
    {
        flat=0;
        DFS(1,0);
    }
    return 0;
}
时间: 2024-08-04 01:51:30

kuangbin专题总结一 简单搜索的相关文章

专题一、简单搜索 - Virtual Judge

很久以前刷完了Virtual Judge上的简单搜索专题,现总结如下: POJ 1321 由于题目的数据范围比较小,可以直接dfs暴力.读入时记录每个空位的位置,保存在pX[]以及pY[]数组中.暴力的时候统计当前处理第几个空格以及当前处理到了第几行即可. #include <iostream> #include <memory.h> using namespace std; const int MAX = 128; long long ans; int N, K, nCnt; b

kuangbin带你飞专题一 简单搜索 题解

目录 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题一 简单搜索 总结:用时2天半终于把这个专题刷完了 对于最基础的dfs bfs 路径打印 状态转移也有了一点自己些微的理解 其实2天半可以压缩到1天半的 主要是自己太懒了...慢慢加油刷bin神的专题呀 从大二下学期开始学算法 一开始就知道这个专题 一开始对于这个专题里的所有问题感觉都好难啊..就直接放弃了 看lrj的书 现在看到这个专题还挺唏嘘的吧 突然觉得思维和想法也不是很难 果然是那个时候心不静&还是储量不够吗

‘简单’搜索专题总结

kuangbin专题一 A. 棋盘问题 在是棋盘的格子上放置棋子,其中要求所有棋子不同行也不同列,求摆放的方案数. dfs,参数:行.棋子数,遍历的时候要回溯. B. Dungeon Master 走迷宫,3D的走迷宫. C. Catch That Cow 最短时间找到那只牛. bfs,剪枝. D. FilpTile 翻方块,上一行状态决定下一行的翻转. E. Find The Multiple 大胆的bfs,数据范围很小的. F. Prime Path bfs G. Shuffle'm Up

java 模拟简单搜索

Java 模拟简单搜索 实体类 package org.dennisit.entity; /** * * * @version : 1.0 * * @author : 苏若年 <a href="mailto:[email protected]">发送邮件</a> * * @since : 1.0 创建时间: 2013-4-8 下午04:51:03 * * @function: TODO * */ public class Medicine { private I

lucene4.3简单搜索示例代码

原文:lucene4.3简单搜索示例代码 源代码下载地址:http://www.zuidaima.com/share/1550463715560448.htm   示例代码,请牛哥们多

kuangbin专题四 : 最短路 I 题 Arbitrage

kuangbin专题四 : 最短路 I 题  Arbitrage POJ 2240 Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound,

参与过三次搜索引擎发展转折的百度,为什么要回归“简单搜索”

相信在搜索引擎技术产品化的二十余年间,我们早已经习惯了搜索引擎的商业化.不过在最近的数博会上,李彦宏却展示了一款名为"简单搜索"的语音交互搜索App,并且声明简单搜索永无广告. 相信很多人也曾想象过这样一款产品--通过语音发问,得出最简单的答案.不过可能大多数人没有想到,推出这款产品的是百度.很长一段时间内,百度的搜索引擎商业化一直为人诟病.但这次百度率先推出永无广告的语音交互搜索,无疑是在搜索引擎的技术和产品形式上都进行了重要创新. 当我们在质疑大企业的创新能力时常常会忽视一点,在技

poj 3279 Fliptile (简单搜索)

Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16558   Accepted: 6056 Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in whic

和我一起打造个简单搜索之Logstash实时同步建立索引

用过 Solr 的朋友都知道,Solr 可以直接在配置文件中配置数据库连接从而完成索引的同步创建,但是 ElasticSearch 本身并不具备这样的功能,那如何建立索引呢?方法其实很多,可以使用 Java API 的方式建立索引,也可以通过 Logstash 的插件 logstash-input-jdbc 完成,今天来探讨下如何使用 logstash-input-jdbc 完成全量同步以及增量同步. 环境 本文以及后续 es 系列文章都基于 5.5.3 这个版本的 elasticsearch