kuangbin专题简单搜索题目几道题目
1、POJ1321棋盘问题
Input
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
Sample Input
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output
2
1 解题思路:
回溯法递归 从第一行开始每一个一个一个试,下一行也是一个一个试。 AC代码。
import java.util.Scanner; /*
* poj 1321
*/
public class Main{
static char[][] graph;
static boolean[] rows;
static boolean[] cols;
static int n,k,nums = ,res = ;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
n=sc.nextInt();
k=sc.nextInt();
if(n==-) break;
if(n==) {
System.out.println();
continue;
}
graph = new char[n][n];
for(int i=;i<n;i++) {
String str = sc.next();
for(int j=;j<n;j++) {
graph[i][j]=str.charAt(j);
}
}
cols=new boolean[n];
next();
System.out.println(res);
res=;
} }
public static void next(int row) {
if(row==n) return;
for(int i=;i<n;i++)
if(graph[row][i]=='#'&&cols[i]==false) {
cols[i]=true;
nums++;
if(k==nums) {
res++;
}
next(row+);
cols[i]=false;
nums--;
}
next(row+);
}
}
2、POJ2251 Dungeon Master
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 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
解题思路:BFS 注意千万在重新调用之前把数据清干净。
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner; public class Main{
static int L,R,C,res=-1;
static char[][][] graph;
static class Node{
int l,r,c;
}
static Queue<Integer> que = new LinkedList<Integer>();
static HashSet<Integer> hs = new HashSet<Integer>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
L=sc.nextInt();
R=sc.nextInt();
C=sc.nextInt();
if(L==0)break;
graph = new char[L][R][C];
for(int i=0;i<L;i++)for(int j=0;j<R;j++) {
String str = sc.next();
for(int k=0;k<C;k++) {
graph[i][j][k]=str.charAt(k);
if(graph[i][j][k]=='S') {
hs.add(set(i,j,k));
que.offer(set(i,j,k));
}
}
}
bfs();
res=-1;
hs.clear();
que.clear();
}
}
public static void bfs() {
res++;
int size = que.size();
if(size==0) {
System.out.println("Trapped!");
return;
}
while(size-->0){
Node node = get(que.poll());
if(graph[node.l][node.r][node.c]=='E') {
System.out.println("Escaped in "+res+" minute(s).");
return;
}
//上
if(node.l!=L-1) {
if(graph[node.l+1][node.r][node.c]=='.'&&!hs.contains(set(node.l+1,node.r,node.c))) {
hs.add(set(node.l+1,node.r,node.c));
que.offer(set(node.l+1,node.r,node.c));
}else if(graph[node.l+1][node.r][node.c]=='E') {
que.offer(set(node.l+1,node.r,node.c));
}
}
//下
if(node.l!=0) {
if(graph[node.l-1][node.r][node.c]=='.'&&!hs.contains(set(node.l-1,node.r,node.c))) {
hs.add(set(node.l-1,node.r,node.c));
que.offer(set(node.l-1,node.r,node.c));
}else if(graph[node.l-1][node.r][node.c]=='E') {
que.offer(set(node.l-1,node.r,node.c));
}
}
//左
if(node.r!=0) {
if(graph[node.l][node.r-1][node.c]=='.'&&!hs.contains(set(node.l,node.r-1,node.c))) {
hs.add(set(node.l,node.r-1,node.c));
que.offer(set(node.l,node.r-1,node.c));
}else if(graph[node.l][node.r-1][node.c]=='E') {
que.offer(set(node.l,node.r-1,node.c));
}
}
//右
if(node.r!=R-1) {
if(graph[node.l][node.r+1][node.c]=='.'&&!hs.contains(set(node.l,node.r+1,node.c))) {
hs.add(set(node.l,node.r+1,node.c));
que.offer(set(node.l,node.r+1,node.c));
}else if(graph[node.l][node.r+1][node.c]=='E') {
que.offer(set(node.l,node.r+1,node.c));
}
}
//前
if(node.c!=0) {
if(graph[node.l][node.r][node.c-1]=='.'&&!hs.contains(set(node.l,node.r,node.c-1))) {
hs.add(set(node.l,node.r,node.c-1));
que.offer(set(node.l,node.r,node.c-1));
}else if(graph[node.l][node.r][node.c-1]=='E') {
que.offer(set(node.l,node.r,node.c-1));
}
}
//后
if(node.c!=C-1) {
if(graph[node.l][node.r][node.c+1]=='.'&&!hs.contains(set(node.l,node.r,node.c+1))) {
hs.add(set(node.l,node.r,node.c+1));
que.offer(set(node.l,node.r,node.c+1));
}else if(graph[node.l][node.r][node.c+1]=='E') {
que.offer(set(node.l,node.r,node.c+1));
}
}
}
bfs();
}
public static int set(int l,int r,int c) {
return l*10000+r*100+c;
}
public static Node get(int i) {
Node node = new Node();
node.l=i/10000;
node.r=(i-node.l*10000)/100;
node.c=(i-node.l*10000-node.r*100);
return node;
}
}
3、Catch That Cow
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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner; public class Main {
static int N,K;
static int res=-1;
static Queue<Integer> que = new LinkedList<Integer>();
static boolean[] vis = new boolean[1000000];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N=sc.nextInt();
K=sc.nextInt();
que.offer(N);
bfs(0);
System.out.println(res);
}
static void bfs(int deep) {
int size = que.size();
if(que.contains(K)) {
res=deep;
return;
}
while(size-->0) {
int P = que.poll();
vis[P]=true;
if(P<K) {
if(lim(P+1)&&!vis[P+1])que.offer(P+1);
if(lim(P*2)&&!vis[P*2])que.offer(P*2);
if(lim(P-1)&&!vis[P-1])que.offer(P-1);
}else {
if(lim(P-1)&&!vis[P-1])que.offer(P-1);
}
}
if(que.size()!=0)bfs(deep+1); }
static boolean lim(int A) {
if(A>100000||A<0) return false;
else return true;
}
}
3、POJ3279Fliptile
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 which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.
As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.
Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".
Input
Lines 2..M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white
Output
Sample Input
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
Sample Output
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0
思路:二进制枚举+自上而下遍历。
WA错误:要求反转次数最小、其次是字典序最小。(MD挂了好些次)
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner; public class Main{
static int[][] graph;
static int N, M; public static void main(String[] args) { Scanner sc = new Scanner(System.in);
M = sc.nextInt();
N = sc.nextInt();
if (N == 0)
return;
graph = new int[M][N];
int[][] meijushu = setFirst();
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
graph[i][j] = sc.nextInt();
}
}
int MinRes = Integer.MAX_VALUE;
HashMap<Integer, int[][]> hm = new HashMap<Integer,int[][]>();
for (int mjs = 0; mjs < meijushu.length; mjs++) {// 枚举数组
int[][] newGraph = new int[M][N];
int[][] res = new int[M][N];
for (int i = 0; i < M; i++) {
newGraph[i] = Arrays.copyOf(graph[i], N);
}
// 第一行下棋
res[0] = Arrays.copyOf(meijushu[mjs], N);
for (int i = 0; i < N; i++) {
if (meijushu[mjs][i] == 1)
chess(newGraph, 0, i);
}
// 接下来每行下棋
for (int i = 1; i < M; i++) {
for (int j = 0; j < N; j++) {
if (newGraph[i - 1][j] == 1) {
chess(newGraph, i, j);
res[i][j] = 1;
}
}
}
int sign = 0;
for (int i = 0; i < N; i++) {
if (newGraph[M - 1][i] == 1) {
sign++;
break;
}
}
if (sign == 0) { int a = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (1 == res[i][j])
a++;
}
}
if (a < MinRes) {
MinRes = a;
hm.put(a, res);
} }
}
if (!hm.isEmpty()) {
int[][] res = hm.get(MinRes);
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(res[i][j] + " ");
}
System.out.println();
}
return;
}
System.out.println("IMPOSSIBLE"); } // 二进制枚举数组
public static int[][] setFirst() {
int[][] newGraph = new int[(int) Math.pow(2, N)][N];
String str[] = new String[(int) Math.pow(2, N)];
for (int i = 0; i < (int) Math.pow(2, N); i++) {
str[i] = Integer.toBinaryString(i);
int len = str[i].length();
for (int j = 0; j < N - len; j++) {
str[i] = "0" + str[i];
}
} for (int i = 0; i < (int) Math.pow(2, N); i++) {
for (int j = N - 1; j >= 0; j--) {
newGraph[i][j] = str[i].charAt(j) - 48;
} /*
* for(int j=0;j<N;j++) { System.out.print(newGraph[i][j]+"--"); }
* System.out.println();
*/
}
return newGraph;
} // 下棋
public static void chess(int[][] newGraph, int x, int y) {
if (newGraph[x][y] == 1) {
newGraph[x][y] = 0;
} else if (newGraph[x][y] == 0) {
newGraph[x][y] = 1;
}
// 左
if (x > 0) {
if (newGraph[x - 1][y] == 1) {
newGraph[x - 1][y] = 0;
} else if (newGraph[x - 1][y] == 0) {
newGraph[x - 1][y] = 1;
}
}
// 上
if (y > 0) {
if (newGraph[x][y - 1] == 1) {
newGraph[x][y - 1] = 0;
} else if (newGraph[x][y - 1] == 0) {
newGraph[x][y - 1] = 1;
}
}
// 下
if (y < N - 1) { if (newGraph[x][y + 1] == 1) {
newGraph[x][y + 1] = 0;
} else if (newGraph[x][y + 1] == 0) {
newGraph[x][y + 1] = 1;
}
}
// 右
if (x < M - 1) {
if (newGraph[x + 1][y] == 1) {
newGraph[x + 1][y] = 0;
} else if (newGraph[x + 1][y] == 0) {
newGraph[x + 1][y] = 1;
}
}
} }
kuangbin专题简单搜索题目几道题目的更多相关文章
- kuangbin专题——简单搜索
A - 棋盘问题 POJ - 1321 题意 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大 ...
- [kuangbin带你飞]专题一 简单搜索 题解报告
又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...
- hdu 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】(转)
HDU 动态规划(46道题目)倾情奉献~ [只提供思路与状态转移方程] Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955 背包 ...
- C语言超级经典400道题目
C语言超级经典400道题目 1.C语言程序的基本单位是____ A) 程序行 B) 语句 C) 函数 D) 字符.C.1 2.C语言程序的三种基本结构是____构A.顺序结构,选择结构,循环结 B.递 ...
- 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开
[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...
- 小白欢乐多——记ssctf的几道题目
小白欢乐多--记ssctf的几道题目 二哥说过来自乌云,回归乌云.Web400来源于此,应当回归于此,有不足的地方欢迎指出. 0x00 Web200 先不急着提web400,让我们先来看看web200 ...
- 简单搜索 kuangbin C D
C - Catch That Cow POJ - 3278 我心态崩了,现在来回顾很早之前写的简单搜索,好难啊,我怎么写不出来. 我开始把这个写成了dfs,还写搓了... 慢慢来吧. 这个题目很明显是 ...
- 在 n 道题目中挑选一些使得所有人对题目的掌握情况不超过一半。
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quar ...
- 从几道题目带你深入理解Event Loop_宏队列_微队列
目录 深入探究JavaScript的Event Loop Event Loop的结构 回调队列(callbacks queue)的分类 Event Loop的执行顺序 通过题目来深入 深入探究Java ...
随机推荐
- echarts地图map城市间如何连线
let bjData = [ [{name:'北京'}, {name:'上海',value:95}], [{name:'北京'}, {name:'广州',value:90}]]; let conver ...
- u盘 安装 centOS 7
实际上, 对于服务器的安装, 最好是能够远程批量安装(可能有些难度, 不是专业运维) 镜像下载地址: http://59.80.44.49/isoredirect.centos.org/centos/ ...
- Android之okhttp实现socket通讯(非原创)
文章大纲 一.okhttp基础介绍二.socket通讯代码实战三.项目源码下载四.参考文章 一.okhttp基础介绍 https://www.jianshu.com/p/e3291b7808e7 ...
- 使用 shopfiy 模板语言,创建产品模板以搭配购物车实现一键购买
shopfiy 的 product 在添加产品时,如果要将产品详情页面与购物车关联,就是在详情页里面直接下单,而不是从详情页通过点击购买按钮,跳到 shopfy stroe ,再从这个位置再跳转到下单 ...
- Linux—yum使用详解
yum配置 yum的配置文件在 /etc/yum.conf 参考:https://www.cnblogs.com/yhongji/p/9384780.html yum源配置 yum源文件在 /et ...
- appium---元素定位方法
在我们做自动化测试的过程中,最基本的就是要会元素定位,也是自动化中的灵魂所在,如果一个自动化测试工程师说不会定位元素定位,那么肯定也不会做自动化了. 如何查看元素 小伙伴们都知道如果是web端可以通过 ...
- Oracle存储过程(包:PACK_KPI_KERNEL For YS三度评价体系)
CREATE OR REPLACE PACKAGE PACK_KPI_KERNEL IS --定义多级数组 字符串 TYPE TSTRARRY ) INDEX BY BINARY_INTEGER; T ...
- 字典与json转化
json.dumps(字典) #转成json格式 json.loads(json格式) #转成字典格式
- ubuntu建立文件或者文件夹软链接
文件夹建立软链接(用绝对地址) ln -s 源地址 目的地址 比如我把linux文件系统rootfs_dir软链接到/home/jyg/目录下 ln -s /opt/linux/rootfs_dir ...
- mysql数据库的批量数据导入与导出,性能提升。
少量数据批量导入:1. 先从数据库把唯一键的值查询出来,放在列表2. 将导入的数据遍历取出,看是否存在列表中,若不在,说明数据库没有.3. 定义两个空列表,一个做为插入数据,一个做为更新数据4. 步骤 ...