package org.acm.search;
/*最少步数
时间限制:3000 ms | 内存限制:65535 KB
难度:4
描述
这有一个迷宫,有0~8行和0~8列:
1,1,1,1,1,1,1,1,1
1,0,0,1,0,0,1,0,1
1,0,0,1,1,0,0,0,1
1,0,1,0,1,1,0,1,1
1,0,0,0,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,0,0,0,1
1,1,0,1,0,0,0,0,1
0表示道路,1表示墙。
现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?
(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)
输入
第一行输入一个整数n(0<n<=100),表示有n组测试数据;
随后n行,每行有四个整数a,b,c,d(0<=a,b,c,d<=8)分别表示起点的行、列,终点的行、列。
输出
输出最少走几步。
样例输入
2
3 1 5 7
3 1 6 7
样例输出
12
11
分别用深搜和广搜做了一遍,听大神说的,广搜好一些。
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class search_dfs_58 {
private static int map[][] = new int[][] {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 0, 0, 1, 1, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 0, 1, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 1 },
{ 1, 1, 0, 1, 0, 1, 0, 0, 1 },
{ 1, 1, 0, 1, 0, 1, 0, 0, 1 },
{ 1, 1, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
private static boolean visited[][] = new boolean[9][9];
private static int a, b, c, d, count;
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int n = sc.nextInt();
while (n-- > 0) {
count = 100;
for (int i = 0; i <= 8; i++)
Arrays.fill(visited[i], false);
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
d = sc.nextInt();
dfs(a, b, 0);
System.out.println(count);
}
sc.close();
}
//深搜
private static void dfs(int x, int y, int z) {
if (x < 0 || y < 0 || x > 8 || y > 8 || visited[x][y] || map[x][y] == 1 || z >= count)
return;
if (x == c && y == d) {
count = z;
return;
}
visited[x][y] = true;
dfs(x - 1, y, z + 1);// 上搜
dfs(x, y - 1, z + 1);// 左搜
dfs(x + 1, y, z + 1);// 下搜
dfs(x, y + 1, z + 1);// 右搜
visited[x][y] = false;
}
}
package org.acm.search;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Scanner;
public class search_bfs_58 {
//创建一个点类
public static class Dot {
int x, y, step;
public Dot() {
}
public Dot(int x, int y) {
this.x = x;
this.y = y;
}
}
private static int map[][];
private static LinkedList<Dot> queue = new LinkedList<Dot>();
private static int a, b, c, d;
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int n = sc.nextInt();
while (n-- > 0) {
// 初始化
map = new int[][] { { 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 0, 0, 1, 0, 0, 1, 0, 1 }, { 1, 0, 0, 1, 1, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 0, 1, 1 }, { 1, 0, 0, 0, 0, 1, 0, 0, 1 }, { 1, 1, 0, 1, 0, 1, 0, 0, 1 },
{ 1, 1, 0, 1, 0, 1, 0, 0, 1 }, { 1, 1, 0, 1, 0, 0, 0, 0, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
queue.clear();
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
d = sc.nextInt();
// 起点入队
queue.add(new Dot(a, b));
bfs();// 广搜
}
sc.close();
}
private static int dir[][] = new int[][] { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };
private static void bfs() {
Dot dot = null;
while (!queue.isEmpty()) {
dot = queue.poll();
if (dot.x == c && dot.y == d) {
System.out.println(dot.step);
break;
}
// 当前可以走的点,将它入队
for (int i = 0; i < 4; i++) {
Dot tmp = new Dot();
tmp.x = dot.x + dir[i][0];
tmp.y = dot.y + dir[i][1];
// 将走过的点置为1,不可走
if (map[tmp.x][tmp.y] == 0) {
map[tmp.x][tmp.y] = 1;
tmp.step = dot.step + 1;
queue.add(tmp);
}
}
}
}
}