可以把教室外面圈一层 '0',这样免去了一些检查。
'0'
#include <vector> #include <cstdint> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int main() { uint32_t n; while (cin >> n) { struct Classroom_rec { uint32_t id; uint32_t dis; }; vector<Classroom_rec> classrooms_available; for (uint32_t cnt = 0; cnt < n; ++cnt) { uint32_t id, dis, row, col; cin >> id >> dis >> row >> col; char room[16][17]; memset(room, '0', sizeof(room)); for (uint32_t i = 1; i <= row; ++i) { cin >> (room[i] + 1); room[i][col + 1] = '0'; } for (uint32_t i = 1; i <= row; ++i) { for (uint32_t j = 1; j <= col; ++j) { if (room[i][j] == '0' && room[i][j - 1] == '0' && room[i][j + 1] == '0' && room[i - 1][j] == '0') { classrooms_available.push_back({id, dis}); goto NEXT_CLASSROOM; } } } NEXT_CLASSROOM : {} } if (classrooms_available.empty()) cout << "Bad Luck,Rocker!\n"; else { sort(classrooms_available.begin(), classrooms_available.end(), [](Classroom_rec a, Classroom_rec b) { return a.dis < b.dis; }); cout << classrooms_available.front().id << '\n'; } } }
可以把教室外面圈一层
'0'
,这样免去了一些检查。