一只小蒟蒻备考蓝桥杯的日志
笔记
受之前某题影响好深
满脑子都是二维转一位(便于储存和查重)
那么enCode和deCode如下
chatGPT教的,字符串的用处
string
//编码
string enCode(int x, int y, int direct){
string xstr = to_string(x);
string ystr = to_string(y);
string dstr = to_string(direct);
//补齐3位数,前缀0
if(xstr.length() < 3) {
xstr = string(3 - xstr.length(), '0') + xstr;
}
if(ystr.length() < 3) {
ystr = string(3 - ystr.length(), '0') + ystr;
}
return (xstr + ystr + dstr);
}
//解码
void deCode(string in, int& x, int& y, int& direct){
int startIndex = 0;
int length = 3;
//这个substr好妙
x = stoi(in.substr(startIndex, length));
startIndex += length;
y = stoi(in.substr(startIndex, length));
startIndex += length;
direct = stoi(in.substr(startIndex, 1));
}
完整代码
Attention!这是小蒟蒻自用代码,主要用于string和思路复习,因为远没有ac!!!
#include <stdio.h>
#include <iostream>
#include <queue>
#include <map>
#include <string.h>
using namespace std;
char mp[305][305];
queue<string> q;
//string store 201 322 0
// x y direct
map<string, int> cost;
int mov[5][3] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
// 0right 1down 2left 3up
// 4
//传送们
int door[30][5];
//door[0]->A door[0][0]x1 door[0][1]y1 door[0][2]x2 door[0][3]y2
//编码
string enCode(int x, int y, int direct){
string xstr = to_string(x);
string ystr = to_string(y);
string dstr = to_string(direct);
//补齐3位数,前缀0
if(xstr.length() < 3) {
xstr = string(3 - xstr.length(), '0') + xstr;
}
if(ystr.length() < 3) {
ystr = string(3 - ystr.length(), '0') + ystr;
}
return (xstr + ystr + dstr);
}
//解码
void deCode(string in, int& x, int& y, int& direct){
int startIndex = 0;
int length = 3;
//这个substr好妙
x = stoi(in.substr(startIndex, length));
startIndex += length;
y = stoi(in.substr(startIndex, length));
startIndex += length;
direct = stoi(in.substr(startIndex, 1));
}
int main() {
int N, M;
cin >> N >> M;
int sx, sy, fx, fy;
memset(door, -1, sizeof(door));
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
scanf("%c", &mp[i][j]);
if(mp[i][j] == '=') {
fx = j, fy = i;
} else if(mp[i][j] == '@') {
sx = j, sy = i;
} else if(mp[i][j] >= 'A' && mp[i][j] <= 'Z') {
int index = mp[i][j] - 'A';
if(door[index][0] != -1) {
door[index][2] = j;
door[index][3] = i;
} else {
door[index][0] = j;
door[index][1] = i;
}
}
}
//格外小心
getchar(); // 读取换行符
}
string in = enCode(sx, sy, 4);
q.push(in);
cost[in] = 0;
int ans;
while(q.empty() == false){
string now = q.front();
q.pop();
auto it = cost.find(now);
int ncost = it->second;
// cout << ncost << endl;
int nx, ny, nd;
deCode(now, nx, ny, nd);
// cout << "nx=" << nx << " ny=" << ny << endl;
if(mp[ny][nx] == '=') {
ans = ncost;
break;
}
for(int i = 0; i < 4; i++) {
if((nd + 2) % 4 == i) { //防止来回走
continue;
}
int x1 = nx + mov[i][0];
int y1 = ny + mov[i][1];
//越界、不能过
if(x1 < 0 || x1 >= M || y1 < 0 || y1 >= N || mp[y1][x1] == '#') {
continue;
}
//不是传送们
if(!(mp[y1][x1] >= 'A' && mp[y1][x1] <= 'Z')) {
string later = enCode(x1, y1, i);
if(cost.find(later) == cost.end()) {
cost[later] = ncost + 1;
q.push(later);
} else {
continue;
}
}
//是传送门
int index = mp[y1][x1] - 'A';
int x2, y2;
if(door[index][0] != x1) {
x2 = door[index][0];
y2 = door[index][1];
} else {
x2 = door[index][2];
y2 = door[index][3];
}
string later = enCode(x2, y2, 4);
if(cost.find(later) == cost.end()) {
cost[later] = ncost + 1;
q.push(later);
} else {
continue;
}
}
}
cout << ans << endl;
return 0;
}
刷题
- [P1825 [USACO11OPEN] Corn Maze S](P1825 [USACO11OPEN] Corn Maze S)
心得
- 代码在上面,远没有ac,但是感觉思路大致很对,但是题目细节没有很好处理(比如说一个传送门可能用多次不一定实现),不打算细究,bfs挺熟,留档复习string用法
小结
害,怎么办呢,总是有忙不完的事,上课、作业、计设…
昨天晚上写的,今天晚上交的…
时间就像海绵里的水…是的吧
“业精于勤荒于嬉,行成于思毁于随”
小蒟蒻一个月,冲省一!