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 ...
随机推荐
- python基础 while 字符串方法 运算符
一.while 1.while 死循环 f=True while f: print(1) print(2) 2.while 活循环 ①.正序 count = 1 while count <= 5 ...
- HTTP GET POST PUT DELETE 四种请求
1.GET请求会向数据库发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改.增加数据,不会影响资源的内容,即该请求不会产生副作用.无论进行多少次操 ...
- 最新内核3.4)Linux 设备树加载I2C client adapter 的流程(内核3.4 高通)【转】
转自:https://blog.csdn.net/lsn946803746/article/details/52515225 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转 ...
- CodeForces - 1253C(思维+贪心)
题意 https://vjudge.net/problem/CodeForces-1253C n个糖果,一天最多吃m个糖果,每个糖果有个值a[i],第d天会变成d*a[i],问吃k(k=1~n)个糖果 ...
- 【bzoj2648】SJY摆棋子(kdtree)
传送门 题意: 二维平面上有若干个点. 现在要维护一种数据结构,支持插入一个点以及询问其余点到某个点的最小曼哈顿距离. 思路: 这是个\(kdtree\)模板题. \(kdtree\)是一种可以高效处 ...
- Jupyter notebook and Octave kernel installation
Jupyter notebook 安装 为了更加方便地写 Python 代码,还需要安装 Jupyter notebook. 利用 pip 安装 Jupyter notebook. 为什么要使用 Ju ...
- es6 的类 class
1.ES6提供了更接近传统语言的写法,引入了Class(类)这个概念,作为对象的模板.通过class关键字,可以定义类. 2. //定义类 class Point { constructor(x, y ...
- Hbulder 调试安卓app
目前开发app有原生开发和web开发两种方式,各有各的优势和劣势,利用web技术开发app可以只用写一套代码,即可以在Android和ios同时应用,比较方便和快捷,有很多中不同的开发方式,例如cor ...
- 【day07】php
一.数组(Array) 1.数组:一组数据的集合 2.数组的分类: 索引数组:键名称是整数,编号从0开始 关联数组:键名称是字符串 3.定义一维数组 ...
- bolb与base64的图片互转
直接看图简单明了. 注:便于测试你可以自己用base64图片测试互转一下.这里base64图片太长了就不给予展示了,望理解