一、Y总视频进度
二、845. 八数码
(1)题目描述
在一个 3×33×3 的网格中,1∼81∼8 这 88 个数字和一个 x
恰好不重不漏地分布在这 3×33×3 的网格中。
例如:
1 2 3
x 4 6
7 5 8
在游戏过程中,可以把 x
与其上、下、左、右四个方向之一的数字交换(如果存在)。
我们的目的是通过交换,使得网格变为如下排列(称为正确排列):
1 2 3
4 5 6
7 8 x
例如,示例中图形就可以通过让 x
先后与右、下、右三个方向的数字交换成功得到正确排列。
交换过程如下:
1 2 3 1 2 3 1 2 3 1 2 3
x 4 6 4 x 6 4 5 6 4 5 6
7 5 8 7 5 8 7 x 8 7 8 x
现在,给你一个初始网格,请你求出得到正确排列至少需要进行多少次交换。
输入格式
输入占一行,将 3×33×3 的初始网格描绘出来。
例如,如果初始网格如下所示:
1 2 3
x 4 6
7 5 8
则输入为:1 2 3 x 4 6 7 5 8
输出格式
输出占一行,包含一个整数,表示最少交换次数。
如果不存在解决方案,则输出 −1−1。
输入样例:
2 3 4 1 5 x 7 6 8
输出样例
19
(2)代码实现
import java.util.*;
public class Main{
static Map<String, Integer> map = new HashMap<>();
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
String str = "";
for (int i = 0; i < 9; i ++){
str += cin.next();
}
System.out.print(bfs(str));
}
static int bfs(String str){
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
Queue<String> q = new LinkedList<>();
q.add(str);
map.put(str, 0);
while (!q.isEmpty()){
String t = q.remove();
if (t.equals("12345678x")) return map.get(t);
int pos = t.indexOf('x');
int x = pos / 3;
int y = pos % 3;
for (int i = 0; i < 4; i ++){
int tx = x + dx[i];
int ty = y + dy[i];
if (tx < 0 || tx >= 3 || ty < 0 || ty >= 3) continue;
String tstr = makeStr(t, pos, tx * 3 + ty);
if (map.containsKey(tstr)) continue;
map.put(tstr, map.get(t) + 1);
q.add(tstr);
}
}
return -1;
}
static String makeStr(String t, int src, int dest){//交换字符
StringBuilder str = new StringBuilder(t);
str.setCharAt(src, t.charAt(dest));//将字符串指定位置中的字符替换成目标字符
str.setCharAt(dest, 'x');
return str.toString();
}
}
C++
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<queue>
using namespace std;
int bfs(string state)
{
queue<string> q;
unordered_map<string, int> d;
q.push(state);
d[state] = 0;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
string end = "12345678x";
while (q.size())
{
auto t = q.front();
q.pop();
if (t == end) return d[t];
int distance = d[t];
int k = t.find('x');
int x = k / 3, y = k % 3;
for (int i = 0; i < 4; i ++ )
{
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < 3 && b >= 0 && b < 3)
{
swap(t[a * 3 + b], t[k]);
if (!d.count(t))
{
d[t] = distance + 1;
q.push(t);
}
swap(t[a * 3 + b], t[k]);
}
}
}
return -1;
}
int main()
{
char s[2];
string state;
for (int i = 0; i < 9; i ++ )
{
cin >> s;
state += *s;
}
cout << bfs(state) << endl;
return 0;
}
三、 AcWing 844. 走迷宫
(1)题目描述
给定一个 n×mn×m 的二维整数数组,用来表示一个迷宫,数组中只包含 00 或 11,其中 00 表示可以走的路,11 表示不可通过的墙壁。
最初,有一个人位于左上角 (1,1)(1,1) 处,已知该人每次可以向上、下、左、右任意一个方向移动一个位置。
请问,该人从左上角移动至右下角 (n,m)(n,m) 处,至少需要移动多少次。
数据保证 (1,1)(1,1) 处和 (n,m)(n,m) 处的数字为 00,且一定至少存在一条通路。
输入格式
第一行包含两个整数 nn 和 mm。
接下来 nn 行,每行包含 mm 个整数(00 或 11),表示完整的二维数组迷宫。
输出格式
输出一个整数,表示从左上角移动至右下角的最少移动次数。
数据范围
1≤n,m≤1001≤n,m≤100
输入样例:
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
输出样例:
8
(2)代码实现
import java.io.BufferedInputStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int N = 110;
static int[][] g = new int[N][N];
static int[][] d = new int[N][N];
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
int n = scanner.nextInt();
int m = scanner.nextInt();
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
g[i][j] = scanner.nextInt();
System.out.println(bfs(n, m));
scanner.close();
}
public static int bfs(int n,int m) {
Queue<int[]> queue = new LinkedList<>();
d[0][0] = 0;
int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1};
queue.offer(new int[]{0,0});
while(!queue.isEmpty()) {
int a[] = queue.poll();
for(int i=0;i<4;i++) {
int x=a[0]+dx[i],y = a[1]+dy[i];
if(x>=0 && x<n && y>=0 && y<m && g[x][y]==0 && d[x][y] == 0) {
d[x][y] = d[a[0]][a[1]]+1;
queue.offer(new int[] {x,y});
}
}
}
return d[n-1][m-1];
}
}
四、2019蓝桥杯 第六题:特别数的和
(1)题目描述
小明对数位中含有 2、0、1、9 的数字很感兴趣(不包括前导 0),在 1 到 40 中这样的数包括 1、2、9、10 至 32、39 和 40,共 28 个,他们的和是 574。请问,在 1 到 n 中,所有这样的数的和是多少?
【输入格式】
输入一行包含两个整数 n。
【输出格式】
输出一行,包含一个整数,表示满足条件的数的和。
【样例输入】
40
【样例输出】
574
【评测用例规模与约定】
对于 20% 的评测用例,1≤n≤10。
对于 50% 的评测用例,1≤n≤100。
对于 80% 的评测用例,1≤n≤1000。
对于所有评测用例,1≤n≤10000。
时间限制:1.0s
内存限制:512.0MB
(2)题目分析
int转String,使用String.indexOf();判断包含不包含"2", “0”, “1”, “9”,可以使用while循环加%,/运算判断。
(3)代码实现
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int sum=0;
for(int i=1;i<=n;i++){
String target=Integer.toString(i);
if(target.indexOf("2")!=-1||target.indexOf("0")!=-1||target.indexOf("1")!=-1||target.indexOf("9")!=-1){
sum+=i;
}
}
System.out.println(sum);
}
}