目录
1.🌟七星填数
📕题目描述:
☀️思路:
全排列后进行判断,注意:判断时用不等于判断,减少代码量
✏️代码 :
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a[12] = {1, 2, 3, 4, 5, 7, 8, 9, 10, 12, 13};
bool check() {
int num1 = a[0] + a[1] + a[2] + a[3];
int num2 = 6 + a[1] + a[4] + 14;
int num3 = 6 + a[2] + a[5] + 11;
int num4 = a[3] + a[5] + a[7] + a[10];
int num5 = a[8] + a[9] + a[7] + 11;
int num6 = a[6] + a[10] + a[8] + 14;
int num7 = a[0] + a[4] + a[6] + a[9];
if (num1 != num2)
return 0;
if (num2 != num3)
return 0;
if (num3 != num4)
return 0;
if (num4 != num5)
return 0;
if (num5 != num6)
return 0;
if (num6 != num7)
return 0;
return 1;
}
int main() {
do {
if (check()) {
cout << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << endl;
return 0;
}
} while (next_permutation(a, a + 11));
return 0;
}
2.🌟旋转
📕题目描述:
☀️思路:
第一行交换到倒数第一列,第二行交换到倒数第二列........ 从此可看出
新矩阵和原矩阵行列之间的关系,再进行输出
✏️代码 :
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110 ;
int n, m;
int a[N][N];
int b[N][N];
//一行到最后一列
//二行到倒数第二列
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a[i][j];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
b[i][j] = a[n - 1 - j][i];
cout << b[i][j] << " ";
}
cout << endl;
}
return 0;
}
3.🌟迷宫与陷阱
📕题目描述:
☀️思路:
bfs题,注意:当捡到无敌药水时,除了墙不能走外都能走,包括已经走过的点。也就是说,捡到药水后可以无视vis标记数组,走除了墙以外的最短路
✏️代码 :
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<queue>
using namespace std;
const int N = 1000 ;
struct node {
int x;
int y;
int st;//无敌状态剩余步数
int d;//步数
};
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
char mp[N][N];
bool vis[N][N];
int n;
int k;//无敌状态持续时间k
bool check(int x, int y) {
if (x < 1 || y < 1 || x > n || y > n) return false; //越界
return true;
}
//无敌时忽略vis[]
int bfs(int x, int y) {
queue<node>q;
q.push({x, y, 0, 0});
vis[x][y] = 1;
while (q.size()) {
node t = q.front();
q.pop();
if (t.x == n && t.y == n) return t.d; //终点
for (int i = 0; i < 4; i++) {
int fx = t.x + dx[i], fy = t.y + dy[i];
if (mp[fx][fy] == '#') continue; //墙
if (!check(fx, fy)) continue; //越界
if (mp[fx][fy] == '%') {
q.push({fx, fy, k, t.d + 1});
mp[fx][fy] = '.';
vis[fx][fy] = 1;
} else if (mp[fx][fy] == '.') {
if (t.st > 0) { //无敌
q.push({fx, fy, t.st - 1, t.d + 1});
vis[fx][fy] = 1;
} else {
if (!vis[fx][fy]) {
q.push({fx, fy, t.st, t.d + 1});
vis[fx][fy] = 1;
}
}
} else if (mp[fx][fy] == 'X') {
if (t.st > 0) { //无敌时才能走陷阱
q.push({fx, fy, t.st - 1, t.d + 1});
vis[fx][fy] = 1;
}
}
}
}
return -1;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> mp[i][j];
}
}
cout << bfs(1, 1);
return 0;
}
补上昨天的打卡,还是有两题不会,后续补充