kuangbin算法刷题:专题一简单搜索

专题一简单搜索


这个专题主要是使用bfs,dfs来解决问题

1. 棋盘问题原题链接

给定一个指定形状的棋盘,和一定数量的棋子,使用旗子去填棋盘,要使得所有的旗子不在同一行,或同一列,求总共有多少种摆法。(旗子都相同,没有区别)

这个题与八皇后问题相似,使用dfs探索出所有符合条件的路。


import java.util.Scanner;

public class Main {

	private String[] map;
	private int[] visted;
	private int n;
	private int k;
	private int res;

	public static void main(String[] args) {
		new Main().resolve();
	}

	public void resolve() {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			String s = scanner.nextLine();
			String[] ss = s.split(" ");
			n = Integer.parseInt(ss[0]);
			k = Integer.parseInt(ss[1]);
			if (n == -1 || k == -1) {
				break;
			}
			map = new String[n];
			visted = new int[n];
			for (int i = 0; i < n; i++) {
				map[i] = scanner.nextLine();
			}
			dfs(0, k);
			System.out.println(res);
			res = 0;
		}
	}

	private void dfs(int line, int k_now) {
		if (k_now == 0) {
			res++;
			return;
		}
		if (line == n) {
			return;
		}
		for (int i = 0; i < n; i++) {
			if (map[line].charAt(i) == '#' && visted[i] == 0) {
				visted[i] = 1;
				dfs(line + 1, k_now - 1);
				visted[i] = 0;
			}
		}
		dfs(line + 1, k_now);
	}

}

2. dungeon master原题链接

迷宫问题,一个3D迷宫,寻找最短出路。我这里使用了两种方法

  • (超时)使用dfs可以找到所有路径,从中找出最短的路径即可获得最优解。

    因为每条路都探索,并且有重复走过的路程,比如 (1,2,3)->(1,2,2)->(1,2,1) , (1,2,3)->(1,1,3)->(1,1,1)->(1,2,1)->(1,2,2)->(1,2,1),后面有重复的路(1,2,2)->(1,2,1),也就是说在第二次探索路的时候,如果将第一次探索的结果存起来,在到(1,2,2)的时,就可以知道还有一步即可到达终点(1,2,1)。

    那我们是不是可以把距离存起来呢,答案是否定的,因为需要的是最短距离,dfs是深度优先是做不到的,就拿上面的举例,假设(1,2,3)和(1,2,1)不是起点和终点,而是需要经过的点,如果先走的是第一条我们标记(1,2,3)->(1,2,1)距离为2,之后当搜索到(1,2,3)时,我们只需加上2,便是到(1,2,1)的距离。但是要是先走的是第二条,就是加6了,显然不合理。

    因此可以想到要是用bfs自然可以解决这个问题,bfs每次循环,即得到下一步可以到达的所以点,因此一点是最短的路。

    import java.util.Scanner;
    
    public class Main {
    	private static int[] x_move = { 1, -1, 0, 0, 0, 0 };
    	private static int[] y_move = { 0, 0, 1, -1, 0, 0 };
    	private static int[] z_move = { 0, 0, 0, 0, 1, -1 };
    	private String[][] maze;
    	private int l;
    	private int r;
    	private int c;
    	private int[][][] visted;
    	private int res = Integer.MAX_VALUE;
    
    	public static void main(String[] args) {
    		new Main().resolve();
    	}
    
    	public void resolve() {
    		Scanner scanner = new Scanner(System.in);
    		while (true) {
    			String firstLine = scanner.nextLine();
    			String[] ss = firstLine.split(" ");
    			l = Integer.parseInt(ss[0]);
    			r = Integer.parseInt(ss[1]);
    			c = Integer.parseInt(ss[2]);
    			if (l == 0 || r == 0 || c == 0) {
    				break;
    			}
    			maze = new String[l][r];
    			visted = new int[l][r][c];
    
    			int S_x = -1;
    			int S_y = -1;
    			int S_z = -1;
    			for (int i = 0; i < l; i++) {
    				for (int j = 0; j < r; j++) {
    					maze[i][j] = scanner.nextLine();
    				}
    				scanner.nextLine();
    			}
    			here: for (int i = 0; i < l; i++) {
    				for (int j = 0; j < r; j++) {
    					if ((S_y = maze[i][j].indexOf("S")) != -1) {
    						S_z = i;
    						S_x = j;
    						break here;
    					}
    				}
    			}
    
    			dfs(S_x, S_y, S_z, 0);
    			if (res == Integer.MAX_VALUE) {
    				System.out.println("Trapped!");
    			} else
    				System.out.println("Escaped in " + res + " minute(s).");
    			res = Integer.MAX_VALUE;
    		}
    	}
    
    	private void dfs(int x, int y, int z, int steps) {
    		if (x < 0 || x >= c || y < 0 || y >= r || z < 0 || z >= l || visted[z][y][x] == 1
    				|| maze[z][y].charAt(x) == '#') {
    			return;
    		}
    		if (maze[z][y].charAt(x) == 'E') {
    			res = res > steps ? steps : res;
    			return;
    		}
    		visted[z][y][x] = 1;
    		for (int i = 0; i < 6; i++) {
    			dfs(x + x_move[i], y + y_move[i], z + z_move[i], steps + 1);
    		}
    		visted[z][y][x] = 0;
    	}
    }
    
  • bfs解法

    
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		new Main().resolve();
    	}
    
    	class Point {
    		int x;
    		int y;
    		int z;
    		int step;
    
    		public Point(int x, int y, int z, int step) {
    			this.x = x;
    			this.y = y;
    			this.z = z;
    			this.step = step;
    		}
    	}
    
    	private static int[] x_move = { 1, -1, 0, 0, 0, 0 };
    	private static int[] y_move = { 0, 0, 1, -1, 0, 0 };
    	private static int[] z_move = { 0, 0, 0, 0, 1, -1 };
    	private String[][] maze;
    	private int l;
    	private int r;
    	private int c;
    	private int[][][] visted;
    	private Queue<Point> queue;
    	private int res = -1;
    
    	public void resolve() {
    		Scanner scanner = new Scanner(System.in);
    		while (true) {
    			String firstLine = scanner.nextLine();
    			String[] ss = firstLine.split(" ");
    			l = Integer.parseInt(ss[0]);
    			r = Integer.parseInt(ss[1]);
    			c = Integer.parseInt(ss[2]);
    			if (l == 0 || r == 0 || c == 0) {
    				break;
    			}
    			maze = new String[l][r];
    			visted = new int[l][r][c];
    
    			int S_x = -1;
    			int S_y = -1;
    			int S_z = -1;
    			for (int i = 0; i < l; i++) {
    				for (int j = 0; j < r; j++) {
    					maze[i][j] = scanner.nextLine();
    				}
    				scanner.nextLine();
    			}
    			here: for (int i = 0; i < l; i++) {
    				for (int j = 0; j < r; j++) {
    					if ((S_x = maze[i][j].indexOf("S")) != -1) {
    						S_z = i;
    						S_y = j;
    						break here;
    					}
    				}
    			}
    			queue = new LinkedList<Point>();
    			queue.offer(new Point(S_x, S_y, S_z, 0));
    			visted[S_z][S_y][S_x] = 1;
    			Bfs();
    			if (res == -1) {
    				System.out.println("Trapped!");
    			} else
    				System.out.println("Escaped in " + res + " minute(s).");
    			res = -1;
    		}
    	}
    
    	private void Bfs() {
    		while (!queue.isEmpty()) {
    			Point now = queue.poll();
    			for (int i = 0; i < 6; i++) {
    				int x = now.x + x_move[i];
    				int y = now.y + y_move[i];
    				int z = now.z + z_move[i];
    				if (x < 0 || x >= c || y < 0 || y >= r || z < 0 || z >= l || visted[z][y][x] == 1
    						|| maze[z][y].charAt(x) == '#') {
    					continue;
    				}
    				Point next = new Point(x, y, z, now.step + 1);
    				if (maze[z][y].charAt(x) == 'E') {
    					res = next.step;
    					return;
    				}
    				visted[z][y][x] = 1;
    				queue.offer(next);
    			}
    		}
    	}
    }
    

3. Catch That Cow原题链接

给定两个数字,求其中一个数字至少经过多少次+1,-1,*2操作后可以成为另一个数。

一次有三个选择,+1,-1或*2。使用bfs。

分析一下可以进行剪枝,比如start比end小,只能进行-1(比如81->1,加一和乘二显然是不可以的,所以就是80)



import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

	static int[] ans = new int[100001];
	static boolean[] visited = new boolean[100001];

	public static void resolve() {
		Queue<Integer> queue = new LinkedList<Integer>();
		int start;
		int end;
		Scanner scanner = new Scanner(System.in);
		String string = scanner.nextLine();
		String[] ss = string.split(" ");
		start = Integer.parseInt(ss[0]);
		end = Integer.parseInt(ss[1]);
		queue.offer(start);
		visited[start] = true;
		if (end <= start) {
			System.out.println(start - end);
			return;
		}
		while (!queue.isEmpty()) {
			int now = queue.poll();

			for (int i = 0; i < 3; i++) {
				int next = now;
				if (i == 0) {
					next -= 1;
				} else if (i == 1) {
					next += 1;
				} else {
					next *= 2;
				}
				if (next == end) {
					System.out.println(ans[now] + 1);
					return;
				} else if (next >= 0 && next <= 100000 && visited[next] == false) {
					queue.offer(next);
					visited[next] = true;
					ans[next] = ans[now] + 1;
				}
			}

		}

	}

	public static void main(String[] args) {
		resolve();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值