1、POJ1321棋盘问题

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

Input

输入含有多组测试数据。 
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n 
当为-1 -1时表示输入结束。 
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。 

Output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

Sample Input

  1. 2 1
  2. #.
  3. .#
  4. 4 4
  5. ...#
  6. ..#.
  7. .#..
  8. #...
  9. -1 -1

Sample Output

  1. 2
  2. 1
  3.  
  4. 解题思路:
    回溯法递归
  5.  
  6. 从第一行开始每一个一个一个试,下一行也是一个一个试。
  7.  
  8. AC代码。
  1. import java.util.Scanner;
  2.  
  3. /*
  4. * poj 1321
  5. */
  6. public class Main{
  7. static char[][] graph;
  8. static boolean[] rows;
  9. static boolean[] cols;
  10. static int n,k,nums = ,res = ;
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13. while(true) {
  14. n=sc.nextInt();
  15. k=sc.nextInt();
  16. if(n==-) break;
  17. if(n==) {
  18. System.out.println();
  19. continue;
  20. }
  21. graph = new char[n][n];
  22. for(int i=;i<n;i++) {
  23. String str = sc.next();
  24. for(int j=;j<n;j++) {
  25. graph[i][j]=str.charAt(j);
  26. }
  27. }
  28. cols=new boolean[n];
  29. next();
  30. System.out.println(res);
  31. res=;
  32. }
  33.  
  34. }
  35. public static void next(int row) {
  36. if(row==n) return;
  37. for(int i=;i<n;i++)
  38. if(graph[row][i]=='#'&&cols[i]==false) {
  39. cols[i]=true;
  40. nums++;
  41. if(k==nums) {
  42. res++;
  43. }
  44. next(row+);
  45. cols[i]=false;
  46. nums--;
  47. }
  48. next(row+);
  49. }
  50. }

2、POJ2251 Dungeon Master

Description

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?

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
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

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form

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

  1. 3 4 5
  2. S....
  3. .###.
  4. .##..
  5. ###.#
  6.  
  7. #####
  8. #####
  9. ##.##
  10. ##...
  11.  
  12. #####
  13. #####
  14. #.###
  15. ####E
  16.  
  17. 1 3 3
  18. S##
  19. #E#
  20. ###
  21.  
  22. 0 0 0

Sample Output

  1. Escaped in 11 minute(s).
  2. Trapped!

解题思路:BFS  注意千万在重新调用之前把数据清干净。

  1. import java.util.HashSet;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import java.util.Scanner;
  5.  
  6. public class Main{
  7. static int L,R,C,res=-1;
  8. static char[][][] graph;
  9. static class Node{
  10. int l,r,c;
  11. }
  12. static Queue<Integer> que = new LinkedList<Integer>();
  13. static HashSet<Integer> hs = new HashSet<Integer>();
  14. public static void main(String[] args) {
  15. Scanner sc = new Scanner(System.in);
  16. while(true) {
  17. L=sc.nextInt();
  18. R=sc.nextInt();
  19. C=sc.nextInt();
  20. if(L==0)break;
  21. graph = new char[L][R][C];
  22. for(int i=0;i<L;i++)for(int j=0;j<R;j++) {
  23. String str = sc.next();
  24. for(int k=0;k<C;k++) {
  25. graph[i][j][k]=str.charAt(k);
  26. if(graph[i][j][k]=='S') {
  27. hs.add(set(i,j,k));
  28. que.offer(set(i,j,k));
  29. }
  30. }
  31. }
  32. bfs();
  33. res=-1;
  34. hs.clear();
  35. que.clear();
  36. }
  37. }
  38. public static void bfs() {
  39. res++;
  40. int size = que.size();
  41. if(size==0) {
  42. System.out.println("Trapped!");
  43. return;
  44. }
  45. while(size-->0){
  46. Node node = get(que.poll());
  47. if(graph[node.l][node.r][node.c]=='E') {
  48. System.out.println("Escaped in "+res+" minute(s).");
  49. return;
  50. }
  51. //上
  52. if(node.l!=L-1) {
  53. if(graph[node.l+1][node.r][node.c]=='.'&&!hs.contains(set(node.l+1,node.r,node.c))) {
  54. hs.add(set(node.l+1,node.r,node.c));
  55. que.offer(set(node.l+1,node.r,node.c));
  56. }else if(graph[node.l+1][node.r][node.c]=='E') {
  57. que.offer(set(node.l+1,node.r,node.c));
  58. }
  59. }
  60. //下
  61. if(node.l!=0) {
  62. if(graph[node.l-1][node.r][node.c]=='.'&&!hs.contains(set(node.l-1,node.r,node.c))) {
  63. hs.add(set(node.l-1,node.r,node.c));
  64. que.offer(set(node.l-1,node.r,node.c));
  65. }else if(graph[node.l-1][node.r][node.c]=='E') {
  66. que.offer(set(node.l-1,node.r,node.c));
  67. }
  68. }
  69. //左
  70. if(node.r!=0) {
  71. if(graph[node.l][node.r-1][node.c]=='.'&&!hs.contains(set(node.l,node.r-1,node.c))) {
  72. hs.add(set(node.l,node.r-1,node.c));
  73. que.offer(set(node.l,node.r-1,node.c));
  74. }else if(graph[node.l][node.r-1][node.c]=='E') {
  75. que.offer(set(node.l,node.r-1,node.c));
  76. }
  77. }
  78. //右
  79. if(node.r!=R-1) {
  80. if(graph[node.l][node.r+1][node.c]=='.'&&!hs.contains(set(node.l,node.r+1,node.c))) {
  81. hs.add(set(node.l,node.r+1,node.c));
  82. que.offer(set(node.l,node.r+1,node.c));
  83. }else if(graph[node.l][node.r+1][node.c]=='E') {
  84. que.offer(set(node.l,node.r+1,node.c));
  85. }
  86. }
  87. //前
  88. if(node.c!=0) {
  89. if(graph[node.l][node.r][node.c-1]=='.'&&!hs.contains(set(node.l,node.r,node.c-1))) {
  90. hs.add(set(node.l,node.r,node.c-1));
  91. que.offer(set(node.l,node.r,node.c-1));
  92. }else if(graph[node.l][node.r][node.c-1]=='E') {
  93. que.offer(set(node.l,node.r,node.c-1));
  94. }
  95. }
  96. //后
  97. if(node.c!=C-1) {
  98. if(graph[node.l][node.r][node.c+1]=='.'&&!hs.contains(set(node.l,node.r,node.c+1))) {
  99. hs.add(set(node.l,node.r,node.c+1));
  100. que.offer(set(node.l,node.r,node.c+1));
  101. }else if(graph[node.l][node.r][node.c+1]=='E') {
  102. que.offer(set(node.l,node.r,node.c+1));
  103. }
  104. }
  105. }
  106. bfs();
  107. }
  108. public static int set(int l,int r,int c) {
  109. return l*10000+r*100+c;
  110. }
  111. public static Node get(int i) {
  112. Node node = new Node();
  113. node.l=i/10000;
  114. node.r=(i-node.l*10000)/100;
  115. node.c=(i-node.l*10000-node.r*100);
  116. return node;
  117. }
  118. }

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

Input

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

  1. 5 17

Sample Output

  1. 4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
 
 
思路:BFS 注意剪枝  否则会报RE  不信的话输入 0 1000000
  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6. static int N,K;
  7. static int res=-1;
  8. static Queue<Integer> que = new LinkedList<Integer>();
  9. static boolean[] vis = new boolean[1000000];
  10. public static void main(String[] args) {
  11. Scanner sc = new Scanner(System.in);
  12. N=sc.nextInt();
  13. K=sc.nextInt();
  14. que.offer(N);
  15. bfs(0);
  16. System.out.println(res);
  17. }
  18. static void bfs(int deep) {
  19. int size = que.size();
  20. if(que.contains(K)) {
  21. res=deep;
  22. return;
  23. }
  24. while(size-->0) {
  25. int P = que.poll();
  26. vis[P]=true;
  27. if(P<K) {
  28. if(lim(P+1)&&!vis[P+1])que.offer(P+1);
  29. if(lim(P*2)&&!vis[P*2])que.offer(P*2);
  30. if(lim(P-1)&&!vis[P-1])que.offer(P-1);
  31. }else {
  32. if(lim(P-1)&&!vis[P-1])que.offer(P-1);
  33. }
  34. }
  35. if(que.size()!=0)bfs(deep+1);
  36.  
  37. }
  38. static boolean lim(int A) {
  39. if(A>100000||A<0) return false;
  40. else return true;
  41. }
  42. }

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

Line 1: Two space-separated integers: M and N 
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

Lines 1..M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.

Sample Input

  1. 4 4
  2. 1 0 0 1
  3. 0 1 1 0
  4. 0 1 1 0
  5. 1 0 0 1

Sample Output

  1. 0 0 0 0
  2. 1 0 0 1
  3. 1 0 0 1
  4. 0 0 0 0

思路:二进制枚举+自上而下遍历。

WA错误:要求反转次数最小、其次是字典序最小。(MD挂了好些次)

  1. import java.util.Arrays;
  2. import java.util.HashMap;
  3. import java.util.Scanner;
  4.  
  5. public class Main{
  6. static int[][] graph;
  7. static int N, M;
  8.  
  9. public static void main(String[] args) {
  10.  
  11. Scanner sc = new Scanner(System.in);
  12. M = sc.nextInt();
  13. N = sc.nextInt();
  14. if (N == 0)
  15. return;
  16. graph = new int[M][N];
  17. int[][] meijushu = setFirst();
  18. for (int i = 0; i < M; i++) {
  19. for (int j = 0; j < N; j++) {
  20. graph[i][j] = sc.nextInt();
  21. }
  22. }
  23. int MinRes = Integer.MAX_VALUE;
  24. HashMap<Integer, int[][]> hm = new HashMap<Integer,int[][]>();
  25. for (int mjs = 0; mjs < meijushu.length; mjs++) {// 枚举数组
  26. int[][] newGraph = new int[M][N];
  27. int[][] res = new int[M][N];
  28. for (int i = 0; i < M; i++) {
  29. newGraph[i] = Arrays.copyOf(graph[i], N);
  30. }
  31. // 第一行下棋
  32. res[0] = Arrays.copyOf(meijushu[mjs], N);
  33. for (int i = 0; i < N; i++) {
  34. if (meijushu[mjs][i] == 1)
  35. chess(newGraph, 0, i);
  36. }
  37. // 接下来每行下棋
  38. for (int i = 1; i < M; i++) {
  39. for (int j = 0; j < N; j++) {
  40. if (newGraph[i - 1][j] == 1) {
  41. chess(newGraph, i, j);
  42. res[i][j] = 1;
  43. }
  44. }
  45. }
  46. int sign = 0;
  47. for (int i = 0; i < N; i++) {
  48. if (newGraph[M - 1][i] == 1) {
  49. sign++;
  50. break;
  51. }
  52. }
  53. if (sign == 0) {
  54.  
  55. int a = 0;
  56. for (int i = 0; i < M; i++) {
  57. for (int j = 0; j < N; j++) {
  58. if (1 == res[i][j])
  59. a++;
  60. }
  61. }
  62. if (a < MinRes) {
  63. MinRes = a;
  64. hm.put(a, res);
  65. }
  66.  
  67. }
  68. }
  69. if (!hm.isEmpty()) {
  70. int[][] res = hm.get(MinRes);
  71. for (int i = 0; i < M; i++) {
  72. for (int j = 0; j < N; j++) {
  73. System.out.print(res[i][j] + " ");
  74. }
  75. System.out.println();
  76. }
  77. return;
  78. }
  79. System.out.println("IMPOSSIBLE");
  80.  
  81. }
  82.  
  83. // 二进制枚举数组
  84. public static int[][] setFirst() {
  85. int[][] newGraph = new int[(int) Math.pow(2, N)][N];
  86. String str[] = new String[(int) Math.pow(2, N)];
  87. for (int i = 0; i < (int) Math.pow(2, N); i++) {
  88. str[i] = Integer.toBinaryString(i);
  89. int len = str[i].length();
  90. for (int j = 0; j < N - len; j++) {
  91. str[i] = "0" + str[i];
  92. }
  93. }
  94.  
  95. for (int i = 0; i < (int) Math.pow(2, N); i++) {
  96. for (int j = N - 1; j >= 0; j--) {
  97. newGraph[i][j] = str[i].charAt(j) - 48;
  98. } /*
  99. * for(int j=0;j<N;j++) { System.out.print(newGraph[i][j]+"--"); }
  100. * System.out.println();
  101. */
  102. }
  103. return newGraph;
  104. }
  105.  
  106. // 下棋
  107. public static void chess(int[][] newGraph, int x, int y) {
  108. if (newGraph[x][y] == 1) {
  109. newGraph[x][y] = 0;
  110. } else if (newGraph[x][y] == 0) {
  111. newGraph[x][y] = 1;
  112. }
  113. // 左
  114. if (x > 0) {
  115. if (newGraph[x - 1][y] == 1) {
  116. newGraph[x - 1][y] = 0;
  117. } else if (newGraph[x - 1][y] == 0) {
  118. newGraph[x - 1][y] = 1;
  119. }
  120. }
  121. // 上
  122. if (y > 0) {
  123. if (newGraph[x][y - 1] == 1) {
  124. newGraph[x][y - 1] = 0;
  125. } else if (newGraph[x][y - 1] == 0) {
  126. newGraph[x][y - 1] = 1;
  127. }
  128. }
  129. // 下
  130. if (y < N - 1) {
  131.  
  132. if (newGraph[x][y + 1] == 1) {
  133. newGraph[x][y + 1] = 0;
  134. } else if (newGraph[x][y + 1] == 0) {
  135. newGraph[x][y + 1] = 1;
  136. }
  137. }
  138. // 右
  139. if (x < M - 1) {
  140. if (newGraph[x + 1][y] == 1) {
  141. newGraph[x + 1][y] = 0;
  142. } else if (newGraph[x + 1][y] == 0) {
  143. newGraph[x + 1][y] = 1;
  144. }
  145. }
  146. }
  147.  
  148. }

kuangbin专题简单搜索题目几道题目的更多相关文章

  1. kuangbin专题——简单搜索

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

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

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

  3. hdu 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】(转)

    HDU 动态规划(46道题目)倾情奉献~ [只提供思路与状态转移方程] Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包 ...

  4. C语言超级经典400道题目

    C语言超级经典400道题目 1.C语言程序的基本单位是____ A) 程序行 B) 语句 C) 函数 D) 字符.C.1 2.C语言程序的三种基本结构是____构A.顺序结构,选择结构,循环结 B.递 ...

  5. 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开

    [kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...

  6. 小白欢乐多——记ssctf的几道题目

    小白欢乐多--记ssctf的几道题目 二哥说过来自乌云,回归乌云.Web400来源于此,应当回归于此,有不足的地方欢迎指出. 0x00 Web200 先不急着提web400,让我们先来看看web200 ...

  7. 简单搜索 kuangbin C D

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

  8. 在 n 道题目中挑选一些使得所有人对题目的掌握情况不超过一半。

    Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quar ...

  9. 从几道题目带你深入理解Event Loop_宏队列_微队列

    目录 深入探究JavaScript的Event Loop Event Loop的结构 回调队列(callbacks queue)的分类 Event Loop的执行顺序 通过题目来深入 深入探究Java ...

随机推荐

  1. linux远程桌面连接 VNC Server

    更新源 # sudo apt-get update 安装vnc4server # sudo apt-get install vnc4server 修改vnc远程连接密码 # vncpasswd 编辑v ...

  2. [20190910]索引分支块中TERM使用什么字符表示.txt

    [20190910]索引分支块中TERM使用什么字符表示.txt --//做索引块转储,一些root,分支节点出现TERM,从来没有关注使用字符表示,简单探究看看. 1.环境:SCOTT@test01 ...

  3. RAW数据格式解析

    RAM数据格式解析 Raw格式是sensor的输出格式,是未经处理过的数据,表示sensor接受 到的各种光的强度. Raw数据在输出的时候是有一定的顺序的,一般为以下四种: 00: GR/BG 01 ...

  4. [PHP] vscode配置SFTP扩展同步文件

    在我们的项目开发过程中,经常有一种模式,有一台linux的开发机作为我们的测试机器,上面安装了ftp服务.我们在windows系统的本地机器使用IDE编写代码,自动或者保存时同步上传到测试机下,这样就 ...

  5. PHP转Go系列:字符串

    字符串的赋值 在PHP中,字符串的赋值虽然只有一行,其实包含了两步,一是声明变量,二是赋值给变量,同一个变量可以任意重新赋值. $str = 'Hello World!'; $str = 'hia'; ...

  6. 解决vue/cli3.0 语法验证规则 ESLint: Expected indentation of 2 spaces but found 4. (indent)

    当你使用vue/cli3.0的时,有可能出现雁阵规则 ESLint: Expected indentation of 2 spaces but found 4. (indent) 解决方法 1.在vu ...

  7. Pycharm 2019 添加 docker 解释器

    打开docker的tls

  8. JavaScript显式类型转换与隐式类型转换

    隐式类型转换 四则运算 判断语句 toString 在 JavaScript 中声明变量不需指定类型,对变量赋值也没有类型检查,同时还允许隐式类型转换. 这些特征说明 JavaScript 属于弱类型 ...

  9. 【Maven】如何使用pom.xml引入自定义jar包

    这里我以这个jar包为例,aliyun-java-sdk-core-3.2.3.jar ,这是我在做手机短信服务用到的jar包 ①进入C盘下的maven仓库C:\Users\用户\.m2\reposi ...

  10. centos7.6离线安装mysql5.7(附下载链接)

    本来打算直接用原生yum源安装,但是跨国访问网络太慢,只好采用离线安装的方式,原理就是把所需的rpm下载下来再上传服务器安装. 1.rpm文件下载地址: 目录: http://repo.mysql.c ...