关注
import java.util.*;
public class Main{
static Point[] po;
static int[][] b;
static boolean bo = false;
public void dfs(int[][] a, int count, int p, int x, int y) {
int n = a.length;
int m = a[0].length;
if (x == 0 && y == m -1) {
if (p >= 0) {
bo = true;
System.out.print("[0,0],");
for (int i = 0; i < count - 1; i ++) {
System.out.print("["+po[i].x+","+po[i].y+"],");
}
System.out.println("["+po[count - 1].x+","+po[count - 1].y+"]");
}
} else {
if ((x >= 0 && x < n)&&(y >= 0 && y < m - 1)&&a[x][y + 1] != 0&&b[x][y+1] == 0) {
y++;
count++;
p--;
Point point = new Point(x, y);
po[count - 1] = point;
b[x][y] = 1;
dfs(a, count, p,x,y);
b[x][y] = 0;
y--;
count--;
p++;
po[count] = null;
} else
if ((x > 0 && x < n)&&(y >= 0 && y < m)&&a[x - 1][y] != 0&&b[x-1][y] == 0) {
x--;
count++;
p = p - 3;
Point point = new Point(x, y);
po[count - 1] = point;
b[x][y] = 1;
dfs(a, count, p,x,y);
b[x][y] = 0;
x++;
count--;
p = p + 3;
po[count] = null;
} else
if ((x >= 0 && x < n - 1)&&(y >= 0 && y < m)&&a[x + 1][y] != 0&&b[x + 1][y] == 0) {
x++;
count++;
Point point = new Point(x, y);
po[count - 1] = point;
b[x][y] = 1;
dfs(a, count, p,x,y);
b[x][y] = 0;
x--;
count--;
po[count] = null;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int p = sc.nextInt();
int[][] a = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
a[i][j] = sc.nextInt();
}
Main tutle = new Main();
tutle.po = new Point[10000];
tutle.b = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
tutle.b[i][j] = 0;
}
tutle.b[0][0] = 1;
tutle.dfs(a,0,p,0,0);
if (!tutle.bo) {
System.out.println("Can not escape!");
}
}
}
class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
写的好丑,也没有添加最优判断但还是ac了,只能说测试用例比较好过。
查看原帖
点赞 4
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 2025年终总结 #
173353次浏览 2927人参与
# 找工作,行业重要还是岗位重要? #
85399次浏览 1693人参与
# 职场上哪些行为很加分? #
307061次浏览 3455人参与
# 大家每天通勤多久? #
69932次浏览 447人参与
# 实习的内耗时刻 #
211309次浏览 1544人参与
# 你面试体验感最差/最好的公司 #
18072次浏览 299人参与
# 一人说一个提前实习的好处 #
10987次浏览 206人参与
# 今年你最想重开的一场面试是? #
4211次浏览 70人参与
# 秋招落幕,你是He or Be #
12541次浏览 245人参与
# 互联网行业现在还值得去吗 #
46931次浏览 351人参与
# 实习没事做是福还是祸? #
16980次浏览 258人参与
# 面试吐槽bot #
165028次浏览 814人参与
# 重来一次,你会对开始求职的自己说 #
6175次浏览 155人参与
# 反问环节如何提问 #
126393次浏览 2664人参与
# 礼物开箱Plog #
703次浏览 24人参与
# 工作中听到最受打击的一句话 #
6779次浏览 117人参与
# 团建是“福利”还是是 “渡劫” #
7289次浏览 150人参与
# 我的第一份实习怎么找的 #
208596次浏览 1827人参与
# 比亚迪工作体验 #
74766次浏览 281人参与
# 大家实习每天都在干啥 #
106557次浏览 581人参与
顺丰集团工作强度 379人发布