GDPU 竞赛技能实践 天码行空3 搜索

1. 五星填数

在这里插入图片描述

💖 源代码

public class Main
{
	static int[] nums = new int[11];
	static boolean[] used = new boolean[13];
	static long ans = 0;

	static
	{
		used[7] = true;
		used[11] = true;
	}

	public static void main(String[] args)
	{
		dfs(1);
		System.out.println(ans / 10);// 除10去除镜像和旋转的重复项
	}

	private static void dfs(int idx)
	{
		if (idx == 11)
		{
			check();
			return;
		}
		for (int i = 1; i <= 12; i++)
		{
			if (!used[i])
			{
				used[i] = true;
				nums[idx] = i;
				dfs(idx + 1);
				used[i] = false;
			}
		}
	}

	private static void check()
	{
		int a = nums[1] + nums[3] + nums[6] + nums[9];
		int b = nums[1] + nums[4] + nums[7] + nums[10];
		int c = nums[2] + nums[6] + nums[8] + nums[10];
		int d = nums[2] + nums[3] + nums[4] + nums[5];
		int e = nums[5] + nums[7] + nums[8] + nums[9];
		if (a == b && b == c && c == d && d == e)
			ans++;
	}
}

💖 输出

12

2. 遍历黑点

在这里插入图片描述

💖 源代码

public class Main
{
	static int n = 4, m = 5;
	static char[][] map = new char[][] { { '.', '.', '.', '.', '#' }, { '.', '.', '.', '.', '.' },
			{ '#', '@', '.', '.', '.' }, { '.', '#', '.', '.', '#' } };
	static boolean[][] st = new boolean[n][m];
	static boolean[][] out = new boolean[n][m];
	static int[] dx = { 0, 1, 0, -1 };
	static int[] dy = { 1, 0, -1, 0 };

	public static void main(String[] args)
	{

		int sx = 0, sy = 0;// 起点坐标
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				if (map[i][j] == '@')
				{
					sx = i;
					sy = j;
				}
//		从起点开始搜索
		dfs(sx, sy);
	}

	private static void dfs(int x, int y)
	{
		if (!out[x][y])
		{
			out[x][y] = true;
			System.out.println("黑点:[" + x + "][" + y + "]");
		}
		for (int i = 0; i < 4; i++)
		{
			int xx = x + dx[i];
			int yy = y + dy[i];
			if (xx >= 0 && xx < n && yy >= 0 && yy < m && !st[xx][yy] && map[xx][yy] == '.')
			{
				st[x][y] = true;
				dfs(xx, yy);
				st[xx][yy] = false;
			}
		}
	}
}

💖 输出

黑点:[2][1]
黑点:[2][2]
黑点:[2][3]
黑点:[2][4]
黑点:[1][4]
黑点:[1][3]
黑点:[1][2]
黑点:[1][1]
黑点:[1][0]
黑点:[0][0]
黑点:[0][1]
黑点:[0][2]
黑点:[0][3]
黑点:[3][3]
黑点:[3][2]

3. 门牌编号

💖 源代码

public class Main
{
	static int ans = 0;

	static void cal(int x)
	{
		while (x != 0)
		{
			int t = x % 10;
			if (t == 2)
				ans++;
			x /= 10;
		}
	}

	public static void main(String[] args)
	{
		for (int i = 1; i <= 2020; i++)
			cal(i);
		System.out.println(ans);
	}
}

💖 输出

624
  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值