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 = ;
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=;j<n;j++)
{
if(!place[j]&&g[i][j]=='#')
{
place[j] = ;
num++;
DFS(i+);
place[j] = ;//清除标记
num--;
}
}
DFS(i+);//i行也可以不放棋子
}
int main()
{
while(scanf("%d%d",&n,&k))
{
if(n==-&&k==-)
break;
char c;
getchar();
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
scanf("%c",&g[i][j]);
getchar();
}
cnt = ;
num = ;
memset(place,,sizeof(place));
DFS();
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[][] = { {,,},{-,,},{,,},{,-,},{,,},{,,-} };
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=;
bool judge(int tl,int tr,int tc)
{
if(tl<||tr<||tc<||tl>=l||tr>=r||tc>=c||g[tl][tr][tc]=='#')
return false;
return true;
}
int main()
{
cin>>l>>r>>c;
while(l!=&&r!=&&c!=)
{
Read(l,r,c);
d = dfs(start,to);
if(d != -)
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=;k<l;k++)
{
for(i=;i<r;i++)
{
cin>>str;
for(j=;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 = ;
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=; i<; i++)
{
node q = start;
q.r += dir[i][];
q.c += dir[i][];
q.l += dir[i][];
q.step += ; if(judge(q.l,q.r,q.c))
{
g[q.l][q.r][q.c] = '#';
Q.push(q);
}
}
}
return -;
}

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 = ;
int map[N+];
int n,k;
struct node
{
int x,step;
};
int check(int x)
{
if(x< || x>=N || map[x])
return ;
return ;
}
int bfs(int x)
{
int i;
queue<node> Q;
node a,next;
a.x = x;
a.step = ;
map[x] = ;
Q.push(a);
while(!Q.empty())
{
a = Q.front();
Q.pop();
if(a.x == k)
return a.step;
next = a;
//每次都将三种状况加入队列之中
next.x = a.x+;
if(check(next.x))
{
next.step = a.step+;
map[next.x] = ;
Q.push(next);
}
next.x = a.x-;
if(check(next.x))
{
next.step = a.step+;
map[next.x] = ;
Q.push(next);
}
next.x = a.x*;
if(check(next.x))
{
next.step = a.step+;
map[next.x] = ;
Q.push(next);
}
}
return -;
} int main()
{
int ans;
while(~scanf("%d%d",&n,&k))
{
memset(map,,sizeof(map));
ans = bfs(n);
printf("%d\n",ans);
}
return ;
}
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==)
{
return ;
}
if(a%n==)
{
printf("%I64u\n",a);
flat=;
return ;
}
else
{
DFS(a*,step+);
DFS(a*+,step+);
}
return ;
}
int main()
{
while(scanf("%d",&n),n)
{
flat=;
DFS(,);
}
return ;
}

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

  1. [kuangbin带你飞]专题一 简单搜索 题解报告

    又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...

  2. 简单搜索 kuangbin C D

    C - Catch That Cow POJ - 3278 我心态崩了,现在来回顾很早之前写的简单搜索,好难啊,我怎么写不出来. 我开始把这个写成了dfs,还写搓了... 慢慢来吧. 这个题目很明显是 ...

  3. ElasticSearch 5学习(4)——简单搜索笔记

    空搜索: GET /_search hits: total 总数 hits 前10条数据 hits 数组中的每个结果都包含_index._type和文档的_id字段,被加入到_source字段中这意味 ...

  4. nyoj 284 坦克大战 简单搜索

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...

  5. 分布式搜索ElasticSearch构建集群与简单搜索实例应用

    分布式搜索ElasticSearch构建集群与简单搜索实例应用 关于ElasticSearch不介绍了,直接说应用. 分布式ElasticSearch集群构建的方法. 1.通过在程序中创建一个嵌入es ...

  6. solr简单搜索案例

    solr简单搜索案例 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字搜索商品信息,根据商品分类.价格过滤搜索结果,也可以根据价格进行排序,实现分页. 架构分为: 1. solr服务器 2. ...

  7. 和我一起打造个简单搜索之SpringDataElasticSearch入门

    网上大多通过 java 操作 es 使用的都是 TransportClient,而介绍使用 SpringDataElasticSearch 的文章相对比较少,笔者也是摸索了许久,接下来本文介绍 Spr ...

  8. 和我一起打造个简单搜索之SpringDataElasticSearch关键词高亮

    前面几篇文章详细讲解了 ElasticSearch 的搭建以及使用 SpringDataElasticSearch 来完成搜索查询,但是搜索一般都会有搜索关键字高亮的功能,今天我们把它给加上. 系列文 ...

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

    用过 Solr 的朋友都知道,Solr 可以直接在配置文件中配置数据库连接从而完成索引的同步创建,但是 ElasticSearch 本身并不具备这样的功能,那如何建立索引呢?方法其实很多,可以使用 J ...

随机推荐

  1. CSS :first-child 选择器 和 HTML DOM firstChild 属性

    CSS 选择器参考手册 实例 选择属于其父元素的首个子元素的每个 <p> 元素,并为其设置样式: p:first-child { background-color:yellow; } 亲自 ...

  2. Android—Socket中关闭IO流后导致Socket关闭不能再收发数据的解决办法

    以Socket发送数据为例: 发送数据时候要声明:DataOutputStream os = new DataOutputStream(socket.getOutputStream()); 最近开发遇 ...

  3. JAVA静态代理模式(从现实生活角度理解代码原理)

    代理模式(Proxy):为其他对象提供一种代理以控制对这个对象的访问. 代理模式说白了就是"真实对象"的代表,在访问对象时引入一定程度的间接性,因为这种间接性可以附加多种用途. 在 ...

  4. 通过jconsole监控tomcat JVM 内存、线程、CPU

    从Java 5开始 引入了 JConsole,来监控 Java 应用程序性能和跟踪 Java 中的代码.jconsole是JDK自带监控工具,只需要找到 JDK 安装路径,打开 bin 文件夹,双击  ...

  5. windows下用QTwebkit解析html

    环境 windows7 + VS2010 + QT5.2_opengl 配置开发环境 1.安装VS2010 2.安装QT 5.2 QT网站:http://qt-project.org/download ...

  6. MySQL 使用XtraBackup的shell脚本介绍

    mysql_backup.sh是关于MySQL的一个使用XtraBackup做备份的shell脚本,实现了简单的完整备份和增量备份.以及邮件发送备份信息等功能.功能目前还比较简单,后续将继续完善和增加 ...

  7. 锐捷与Vmare网络冲突解决办法

    首先,禁用掉VMare的两个适配器,然后在我的电脑右键管理中找到服务,把VMare的所有服务开起来,并把手动改成自动,最后把VMare的网络设置设置为NAT连接即可.

  8. css div中内容绝对居中(多行内容)

    div中的内容绝对居中(不适合IE6哦,IE6我已经不考虑了),直接看代码吧. <!DOCTYPE HTML> <html> <head> <title> ...

  9. ipv4理论知识1-ipv4介绍,ipv4记法,地址段个数算法

    定义 在TCP/IP协议中,用于在IP层识别连接到因特网设备的标识符称为因特网地址或IP地址.IPv4地址是一个32位的地址. 地址空间 像IPv4这种定义了地址的协议都有一个地址空间.地址空间就是协 ...

  10. redis 查看的版本

    linux环境下查看redis的版本: 查看redis的版本有两种方式:1. redis-server --version 和 redis-server -v 得到的结果是:Redis server ...