2016年第七届蓝桥杯Java程序设计本科B组决赛个人题解汇总:
https://blog.csdn.net/daixinliangwyx/article/details/90169154
第四题
标题:路径之谜
交题测试地址:https://www.dotcpp.com/oj/problem1834.html
小明冒充X星球的骑士,进入了一个奇怪的城堡。
城堡里边什么都没有,只有方形石头铺成的地面。
假设城堡地面是 n x n 个方格。【如图1.png】所示。
按习俗,骑士要从西北角走到东南角。
可以横向或纵向移动,但不能斜着走,也不能跳跃。
每走到一个新方格,就要向正北方和正西方各射一箭。
(城堡的西墙和北墙内各有 n 个靶子)
同一个方格只允许经过一次。但不必走完所有的方格。
如果只给出靶子上箭的数目,你能推断出骑士的行走路线吗?
有时是可以的,比如图1.png中的例子。
本题的要求就是已知箭靶数字,求骑士的行走路径(测试数据保证路径唯一)
输入:
第一行一个整数N(0<N<20),表示地面有 N x N 个方格
第二行N个整数,空格分开,表示北边的箭靶上的数字(自西向东)
第三行N个整数,空格分开,表示西边的箭靶上的数字(自北向南)
输出:
一行若干个整数,表示骑士路径。
为了方便表示,我们约定每个小格子用一个数字代表,从西北角开始编号: 0,1,2,3....
比如,图1.png中的方块编号为:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
示例:
用户输入:
4
2 4 3 4
4 3 3 3
程序应该输出:
0 4 5 1 2 3 7 11 10 9 13 14 15
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
解法:因为n的范围很小,所以就是个简单DFS暴搜。
代码:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static InputReader in = new InputReader(new BufferedInputStream(System.in));
public static PrintWriter out = new PrintWriter(System.out);
public static int n, flag;
public static int[][] vis = new int[25][25];
public static int[] ans = new int[410];
public static int[] a = new int[25];
public static int[] b = new int[25];
public static int[] ta = new int[25];
public static int[] tb = new int[25];
public static int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public static void main(String[] args) {
n = in.nextInt();
for (int i = 0; i < n; i++)
a[i] = in.nextInt();
for (int i = 0; i < n; i++)
b[i] = in.nextInt();
flag = 0;
ans[0] = 0;
ta[0]++;
tb[0]++;
vis[0][0] = 1;
dfs(0, 0, 1);
out.close();
}
static void dfs(int x, int y, int k) {
if (flag == 1) return;
if (x == n-1 && y == n-1) {
int f = 0;
for (int i = 0; i < n; i++) {
if (ta[i] != a[i] || tb[i] != b[i]) {
f = 1;
break;
}
}
if (f == 0) {
for (int i = 0; i < k; i++) {
out.print(ans[i] + " ");
out.flush();
}
flag = 1;
}
return;
}
for (int i = 0; i < 4; i++) {
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if (nextx >= 0 && nextx < n && nexty >= 0 && nexty < n && vis[nextx][nexty] == 0 && ta[nexty] < a[nexty] && tb[nextx] < b[nextx]) {
vis[nextx][nexty] = 1;
ta[nexty]++;
tb[nextx]++;
ans[k] = nextx * n + nexty;
dfs(nextx, nexty, k+1);
ta[nexty]--;
tb[nextx]--;
vis[nextx][nexty] = 0;
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
public BigDecimal nextBigDecimal() {
return new BigDecimal(next());
}
}
}