第一问 Hint:模拟|暴力广搜
偷偷制造
打赏偷偷🙋🏻♂️:[置顶]⬆️修考使用必看🔥
#include<bits/stdc++.h>
using namespace std;
// (1)直接试卷上画图
int wall[100][100];
vector<int> loc;
string s;
void solve2(){
ifstream fin("maze2.txt");
while(getline(fin, s, ',')){
loc.push_back(stoi(s));
}
for(int i = 0; i < loc.size(); i+=2){
wall[loc[i]][loc[i + 1]] = 1;
}
int ans = 0;
for(int i = 0; i < 40; i++){
for(int j = 0; j < 40; j++){
int cnt = 0;
if(wall[2 * i][2 * j + 1] == 1 ) cnt++; // upper
if(wall[2 * i + 2][2 * j + 1] == 1 ) cnt++; // lower
if(wall[2 * i + 1][2 * j] == 1 ) cnt++; // left
if(wall[2 * i + 1][2 * j + 2] == 1 ) cnt++; // right
if(cnt >= 3) ans++;
}
}
cout << ans << endl;
}
int dead[100][100];
bool vis[100][100];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int dis[100][100];
bool valid(int x, int y){ // 检查是否越界,是否先前被访问过,是否是死胡同
if(x >= 0 && x < 40 && y >= 0 && y < 40 && !vis[x][y] && !dead[x][y]) return true;
return false;
}
int bfs(int sx, int sy, int desx, int desy){
memset(dis, -1, sizeof(dis)); // 初始化距离都为-1
queue<pair<int, int>> que;
que.push({sx, sy});
vis[sx][sy] = 1;
dis[sx][sy] = 1;
while(!que.empty()){
auto p = que.front();
que.pop();
int cur_x = p.first;
int cur_y = p.second;
for(int i = 0; i < 4; i++){
int nx = cur_x + dx[i];
int ny = cur_y + dy[i];
if(valid(nx, ny)){
vis[nx][ny] = 1;
dis[nx][ny] = dis[cur_x][cur_y] + 1;
que.push({nx, ny});
if(nx == desx && ny == desy) return dis[nx][ny];
}
}
}
return dis[desx][desy];
}
void solve3(){
ifstream fin("maze2.txt");
while(getline(fin, s, ',')){
loc.push_back(stoi(s));
}
for(int i = 0; i < loc.size(); i+=2){
wall[loc[i]][loc[i + 1]] = 1;
}
for(int i = 0; i < 40; i++){
for(int j = 0; j < 40; j++){
int cnt = 0;
if(wall[2 * i][2 * j + 1] == 1 ) cnt++; // upper
if(wall[2 * i + 2][2 * j + 1] == 1 ) cnt++; // lower
if(wall[2 * i + 1][2 * j] == 1 ) cnt++; // left
if(wall[2 * i + 1][2 * j + 2] == 1 ) cnt++; // right
if(cnt >= 3) dead[i][j] = 1;
}
}
int ans = bfs(0, 0, 2, 0);
cout << ans << endl;
}
void my_read(const string &path, int num){
ifstream fin(path);
while(getline(fin, s, ',')){
loc.push_back(stoi(s));
}
for(int i = 0; i < loc.size(); i+=2){
wall[loc[i]][loc[i + 1]] = 1;
}
for(int i = 0; i < 40; i++){
for(int j = 0; j < 40; j++){
int cnt = 0;
if(wall[2 * i][2 * j + 1] == 1 ) cnt++; // upper
if(wall[2 * i + 2][2 * j + 1] == 1 ) cnt++; // lower
if(wall[2 * i + 1][2 * j] == 1 ) cnt++; // left
if(wall[2 * i + 1][2 * j + 2] == 1 ) cnt++; // right
if(cnt >= 3) dead[i][j] = 1;
}
}
for(int i = 0; i < 40; i++){
for(int j = 0; j < 40; j++){
memset(vis, 0, sizeof(vis)); // 初始化
if(bfs(0, 0, i, j) == -1){
cout << "file" << num << "NO!" << endl;
break;
}
}
}
}
void solve4(){
for(int i = 10; i <= 19; i++){
string path = "maze";
path += to_string(i) + ".txt";
memset(dead, 0, sizeof(dead)); // 每次读取之前进行初始化
memset(vis, 0, sizeof(vis));
memset(dis, -1, sizeof(dis));
my_read(path, i); // 检验每个文档
}
}
int main(){
// solve2();
// solve3();
// solve4();
}
第二问:纯模拟
#include<bits/stdc++.h>
using namespace std;
vector<int> nums;
void solve1(){
ifstream fin("sequence.txt");
string s;
while(getline(fin, s, ',')){
nums.push_back(stoi(s));
}
// for(auto x : nums) cout << x << " ";
cout << nums[216] << endl;
auto p = max_element(nums.begin(), nums.end());
cout << *p << endl;
}
int upper_wall[100][100];
int left_wall[100][100];
int right_wall[100][100];
int lower_wall[100][100];
vector<int> p;
void solve2(){
ifstream fin("p.txt");
string ss;
while(getline(fin, ss, ',')) p.push_back(stoi(ss));
// for(auto x : p) cout << x << " ";
int i, j = 0;
for(int s = 0; s < p.size(); s++){
i = s % 40;
j = s - i * 40;
if(i < 1 || j < 1) continue;
if(p[s] == 0) upper_wall[i][j] = 1;
if(p[s] == 1) left_wall[i][j] = 1;
if(p[s] == 2) lower_wall[i - 1][j - 1] = 1;
if(p[s] == 3) right_wall[i - 1][j - 1] = 1;
}
// cout << (upper_wall[5][20] == 1 ? "upperwall[5][20] exists" : "No upperwall[5][20]") << endl;
// cout << (lower_wall[5][20] == 1 ? "lowerwall[5][20] exists" : "No lowerwall[5][20]") << endl;
// cout << (left_wall[5][20] == 1 ? "leftwall[5][20] exists" : "No leftwall[5][20]") << endl;
// cout << (right_wall[5][20] == 1 ? "rightwall[5][20] exists" : "No rightwall[5][20]") << endl;
//
// cout << (upper_wall[20][20] == 1 ? "upperwall[20][20] exists" : "No upperwall[20][20]") << endl;
// cout << (lower_wall[20][20] == 1 ? "lowerwall[20][20] exists" : "No lowerwall[20][20]") << endl;
// cout << (left_wall[20][20] == 1 ? "leftwall[20][20] exists" : "No leftwall[20][20]") << endl;
// cout << (right_wall[20][20] == 1 ? "rightwall[20][20] exists" : "No rightwall[20][20]") << endl;
//
// cout << (upper_wall[30][33] == 1 ? "upperwall[30][33] exists" : "No upperwall[30][33]") << endl;
// cout << (lower_wall[30][33] == 1 ? "lowerwall[30][33] exists" : "No lowerwall[30][33]") << endl;
// cout << (left_wall[30][33] == 1 ? "leftwall[30][33] exists" : "No leftwall[30][33]") << endl;
// cout << (right_wall[30][33] == 1 ? "rightwall[30][33] exists" : "No rightwall[30][33]") << endl;
int Lcount = 0;
for(int i = 0; i < 40; i++){
for(int j = 0; j < 40; j++){
if(upper_wall[i][j] == 1 && (left_wall[i][j] == 1 || right_wall[i][j] == 1)) Lcount++;
if(lower_wall[i][j] == 1 && (left_wall[i][j] == 1 || right_wall[i][j] == 1)) Lcount++;
}
}
cout << Lcount << endl;
}
void solve3(){
// 纯模拟
// 太长了不想做了
}
int main(){
// solve1();
// solve2();
solve3();
return 0;
}