参考代码:
import java.util.Scanner;
public class Main {
static int[][] g=new int[51][51];//地图
static int[][] st=new int[51][51];//地图
static int[]c=new int[1010];
static int[]b=new int[1010];
static int[]d=new int[1010];
static int[]a=new int[1010];
static int[] dx= {0,1,0,-1};
static int[] dy= {1,0,-1,0};
static int n,m;//城堡大小
static int x1,y1,x2,y2,x3,y3;
static int min=(int)1e9;
static int min1=(int)1e9;
static char[]path=new char[1010];
static char[]pathmin=new char[1010];
static int count=0;
static boolean f=false;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n=5;m=5;
for(int i=1;i<=n;i++) {
for(int j=0;j<=m-1;j++) {
g[i][j+1]=sc.nextInt();
}
}
dfs1(1,1,0);
int a=0,b=0;
for(int i=1;i<=min;i++) {
System.out.println("("+a+","+" "+b+")");
if(pathmin[i]=='x')a++;
else if(pathmin[i]=='y')b++;
else if(pathmin[i]=='s')a--;
else b--;
}
System.out.println("("+4+","+" "+4+")");
}
public static void dfs1(int x,int y,int step) {
if(step>min)return;
if(x==5&&y==5) {
min=Math.min(min, step);
for(int i=1;i<=count;i++) {pathmin[i]=path[i];}
return;
}
for(int i=0;i<4;i++) {
int a=x+dx[i],b=y+dy[i];
if(g[a][b]==1||st[a][b]==1||a<1||a>n||b<1||b>m)continue;
if(dx[i]==0&&dy[i]==1) {count++;path[count]='y';}
else if(dx[i]==1&&dy[i]==0) {count++;path[count]='x';}
else if(dx[i]==0&&dy[i]==-1) {count++;path[count]='z';}
else {count++;path[count]='s';}
st[a][b]=1;
dfs1(a,b,step+1);
count--;
st[a][b]=0;
}
}
}
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:1051 |
大神老白 (C语言代码)浏览:603 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:450 |
矩阵乘法 (C++代码)浏览:1454 |
人见人爱A+B (C语言代码)浏览:625 |
C语言训练-数字母 (C语言代码)浏览:649 |
WU-小九九 (C++代码)浏览:1680 |
矩阵加法 (C语言代码)浏览:1719 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:1456 |
简单的a+b (C语言代码)浏览:414 |