走迷宫dfs

题目描述
  有一个n*m格的迷宫(表示有n行、m列),其中有可走的也有不可走的,
  如果用1表示可以走,0表示不可以走,
  文件读入这n*m个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。
  现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。
  如果一条路都不可行,则输出相应信息(-l表示无路)。 
  请统一用 左上右下的顺序拓展,也就是 (0,-1),(-1,0),(0,1),(1,0)

输入
第一行是两个数n,m( 1 < n , m < 15 ),
接下来是m行n列由10组成的数据,最后两行是起始点和结束点。 

输出
  所有可行的路径,描述一个点时用(x,y)的形式,除开始点外,
  其他的都要用“->”表示方向。 
  
  如果没有一条可行的路则输出-1。

样例输入 
5 6
1 0 0 1 0 1
1 1 1 1 1 1
0 0 1 1 1 0
1 1 1 1 1 0
1 1 1 0 1 1
1 1
5 6

样例输出	
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
import java.util.Scanner;
public class Main {
	static int[][] map; //存放地图
	static int xSize,ySize;//存放地图长度
	static Node[] way;//存放路径
	static int count;//可行的路径总数
	static int index;//路径的下标
	static int end1,end2;//终点 横坐标 纵坐标
	static int begin1,begin2;//起点 横坐标 纵坐标
	
	static class Node{
		int x;
		int y;
		
		public Node(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}
	
	public Main(int[][] map) {
		
		this.map = map;
		this.xSize = map.length;
		this.ySize = map[0].length;
		count = 0;
		way = new Node[1000];
		index = 0;
	}
	
	//将终点的值改为2
	public  void getDestination(int x, int y) {
		map[x][y] = 2;
	}
	
	public  void dfs(int x, int y) {
		//结束条件
		if(map[x][y] == 2) {
			count++;
			//按要求输出
			for(int i = 0; i < index; i++) {
				System.out.print("("+way[i].x+","+way[i].y+")->");
			}
			System.out.print("("+end1+","+end2+")");
			System.out.println();
			return ;
		}
		//路走不通
		if(map[x][y] == 0)
			return;
		//将走过的点标记 并把点存放到路劲中
		map[x][y] = 0;
		int num1 = x + 1;
		int num2 = y + 1;
		way[index] = new Node(num1,num2);
		index++;
		//向左
		if(x > 0)
			dfs(x-1, y);
		//向上
		if(y > 0)
			dfs(x, y-1);
		//向右
		if(x < xSize-1)
			dfs(x+1, y);
		//向下
		if(y < ySize-1 )
			dfs(x, y+1);
		//还原地图  有加就有减
		map[x][y] = 1;
		index--;
	}
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int m = in.nextInt();
		int n = in.nextInt();
		int map[][] = new int[m][n];
		for(int i = 0; i < m; i++)
			for(int j = 0; j < n; j++)
				map[i][j] = in.nextInt();
		
		begin1 = in.nextInt(); 
		begin2 = in.nextInt();
		end1 = in.nextInt();
		end2 = in.nextInt();
		Main main = new Main(map);	
		main.getDestination(end1-1, end2-1);
		main.dfs(begin1-1, begin2-1);
		//可行的路径总数为0
		if(count == 0)
			System.out.println("-1");
	}
}
import java.util.Scanner;
	
	
	public class find2020 {
		static int n,m,start_x,start_y,end_x,end_y;
		static int[][] map ;
		static int count;
		static node[] way;
		static int index;
		
		static public class node{
			int x,y;
			public node(int x,int y) {
				this.x = x;
				this.y = y;
			}
		}
		
		public static void dfs(int x,int y) {
			if(map[x][y] == 2) {
				count++;
				for(int i = 0; i < index;i++) {
					System.out.print("("+way[i].x+","+way[i].y+")->");
				}
				System.out.print("("+end_x+","+end_y+")");
				System.out.println();
				return;
			}
			if(map[x][y] == 0)
				return;
			map[x][y] = 0;
			int dex_x = x + 1;
			int dex_y = y + 1;
			way[index] = new node(dex_x, dex_y);
			index++;
			//向左
			if(y > 0)
				dfs(x, y-1);
			//向上
			if(x > 0)
				dfs(x-1, y);
			//向右
			if(y < m-1)
				dfs(x, y+1);
			//向下
			if(x < n-1 )
				dfs(x+1, y);

			//还原地图  有加就有减
			map[x][y] = 1;
			index--;
		}
		public static void main(String[] args) {
			Scanner in = new Scanner(System.in);
			n = in.nextInt();
			m = in.nextInt();
			map = new int[n][m];
			way = new node[99];
			for(int i = 0; i < n; i++)
				for(int j = 0; j < m;j ++)
					map[i][j] = in.nextInt();
			
			start_x = in.nextInt();	
			start_y = in.nextInt();
			end_x = in.nextInt();
			end_y = in.nextInt();
			index = 0;
			map[end_x-1][end_y-1] = 2;
			dfs(start_x-1, start_y-1);
			
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值