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 ...
随机推荐
- 五分钟搞懂什么是B-树(全程图解)【转】
前戏 我们大家都知道动态查找树能够提高查找效率,比如:二叉查找树,平衡二叉查找树,红黑树.他们查找效率的时间复杂度O(log2n),跟树的深度有关系,那么怎么样才能提高效率呢?当然最快捷的方式就是减少 ...
- Ubuntu16.04初始配置
Ubuntu16.04初始化 清理系统 删除libreoffice:sudo apt-get remove libreoffice-common 删除Amazon链接:sudo apt-get rem ...
- RDPGuard6.1.7之后的问题
RDPGuard是一款保护远程桌面RDP端口不被暴力猜解的软件,说下在使用RDP Guard中遇到的一些问题: 1.似乎D版RDPGuard 6.1.7或之后的版本,启用IP Cloud会自动将大量I ...
- vlmcsd
scp ./vlmcsd-x64-musl-static xxx@host.ip:/opt/kms/ chmod u+x /opt/kms/vlmcsd-x64-musl-static ./vlmcs ...
- 201871010111-刘佳华《面向对象程序设计(java)》第十五周学习总结
201871010111-刘佳华<面向对象程序设计(java)>第十五周学习总结 实验十三 Swing图形界面组件(二) 实验时间 2019-12-6 第一部分:理论知识总结 5> ...
- jwt揭秘(含源码示例和视频)
JSON Web Tokens,是一种开发的行业标准 RFC 7519 ,用于安全的表示双方之间的声明.目前,jwt广泛应用在系统的用户认证方面,特别是现在前后端分离项目. 1. jwt认证流程 在项 ...
- [C12] 大规模机器学习(Large Scale Machine Learning)
大规模机器学习(Large Scale Machine Learning) 大型数据集的学习(Learning With Large Datasets) 如果你回顾一下最近5年或10年的机器学习历史. ...
- NOIP2007 奖学金 结构体排序
是结构体排序的练习题,可供选手们巩固结构体排序的一些相关内容. 关于结构体排序 1.结构体定义 struct student { int num,a,b,c,sum; }p[]; 2.结构体初始化 ; ...
- LG4051/BZOJ1031 「JSOI2007」字符加密 后缀数组
问题描述 BZOJ1031 LG4051 题解 发现这是一个环,根据经验,破环为链,于是字符环变为了字符串 之后对这个复制之后的字符串求后缀数组. $len$代表原字符串长度,代表复制后的字符串长度 ...
- Angular 4.x NgClass ngStyle 指令用法
<some-element [ngClass]="'first second'">...</some-element> <some-element [ ...