package OJ;
import java.util.*;
import java.util.regex.Matcher;
public class Bully {
/*
* 动态规划
* 走迷宫
* 1不能走,0可以走
* */
private static int[][] graph = {{0,1,1,0,0},{0,1,1,0,0},{0,1,0,0,0},{0,0,0,1,0},{1,1,0,1,0}};
private static int n = graph.length;
private static StringBuffer sb = new StringBuffer();
private static ArrayList<String> al = new ArrayList<String>();
public static void main(String[] args) {
getOutFromHere(0,0,sb,new int[n][n]);
sop(al);
sop(al.size());
}
private static void getOutFromHere(int i, int j, StringBuffer path,int[][] visit) {
visit[i][j] = 1; //建立的一个访问表,已访问为1
path.append(i+""+j+" ");
if(i==n-1 && j==n-1){
al.add(path.toString());
}else{
if(i+1<n){ //向下移动
if(graph[i+1][j]==0 && visit[i+1][j]==0){
getOutFromHere(i+1,j,path,visit);
}
}
if(j+1<n){ //向右移动
if(graph[i][j+1]==0 && visit[i][j+1]==0){
getOutFromHere(i,j+1,path,visit);
}
}
if(i-1>=0){ //向上移动
if(graph[i-1][j]==0 && visit[i-1][j]==0){
getOutFromHere(i-1,j,path,visit);
}
}
if(j-1>=0){ //向左移动
if(graph[i][j-1]==0 && visit[i][j-1]==0){
getOutFromHere(i,j-1,path,visit);
}
}
}
//四个方向都不能走,那么退回来
visit[i][j] = 0;
path.setLength(path.length()-1-String.valueOf(i).length()-String.valueOf(j).length());
}
public static void sop(Object o){
System.out.println(o);
}
}
import java.util.*;
import java.util.regex.Matcher;
public class Bully {
/*
* 动态规划
* 走迷宫
* 1不能走,0可以走
* */
private static int[][] graph = {{0,1,1,0,0},{0,1,1,0,0},{0,1,0,0,0},{0,0,0,1,0},{1,1,0,1,0}};
private static int n = graph.length;
private static StringBuffer sb = new StringBuffer();
private static ArrayList<String> al = new ArrayList<String>();
public static void main(String[] args) {
getOutFromHere(0,0,sb,new int[n][n]);
sop(al);
sop(al.size());
}
private static void getOutFromHere(int i, int j, StringBuffer path,int[][] visit) {
visit[i][j] = 1; //建立的一个访问表,已访问为1
path.append(i+""+j+" ");
if(i==n-1 && j==n-1){
al.add(path.toString());
}else{
if(i+1<n){ //向下移动
if(graph[i+1][j]==0 && visit[i+1][j]==0){
getOutFromHere(i+1,j,path,visit);
}
}
if(j+1<n){ //向右移动
if(graph[i][j+1]==0 && visit[i][j+1]==0){
getOutFromHere(i,j+1,path,visit);
}
}
if(i-1>=0){ //向上移动
if(graph[i-1][j]==0 && visit[i-1][j]==0){
getOutFromHere(i-1,j,path,visit);
}
}
if(j-1>=0){ //向左移动
if(graph[i][j-1]==0 && visit[i][j-1]==0){
getOutFromHere(i,j-1,path,visit);
}
}
}
//四个方向都不能走,那么退回来
visit[i][j] = 0;
path.setLength(path.length()-1-String.valueOf(i).length()-String.valueOf(j).length());
}
public static void sop(Object o){
System.out.println(o);
}
}