const std::string fi = "OCSEN.INP"; const std::string fo = "OCSEN.OUT"; const int maxn = 2000; const short tkx[4] = {0, 0, 1, -1}; const short tky[4] = {-1, 1, 0, 0};
struct dbg { int x, y; long dem; };
short a[maxn][maxn]; bool mark[maxn][maxn]; int n, m, x, y; bool ok; int kq; long dem; std::ifstream f_in; std::ofstream f_out; dbg kqu[maxn * maxn];
void docfile() { std::memset(mark, false, sizeof(mark)); f_in.open(fi); f_in >> n >> m >> x >> y; dem = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { f_in >> a[i][j]; } } f_in.close(); }
void xuli() { ok = false; long fi = 1, ki = 1; kqu[1].x = x; kqu[1].y = y; kqu[1].dem = 1; mark[x][y] = true; ++dem; while (fi <= ki) { int i = fi; ++fi; for (int j = 1; j <= 4; ++j) { long u = kqu[i].x + tkx[j]; long v = kqu[i].y + tky[j]; if (u > 0 && u <= n && v > 0 && v <= m && !mark[u][v] && a[u][v] == 0) { mark[u][v] = true; ++ki; kqu[ki].x = u; kqu[ki].y = v; ++dem; ok = true; } } } }
void ghifile() { f_out.open(fo); if (dem == 0) { f_out << -1 << std::endl; } else { f_out << dem << std::endl; } f_out.close(); }
int main() { docfile(); if (a[x][y] != 1) { xuli(); } ghifile(); return 0; }
Ah! The classic "OCSEN" maze solver in C++. If only we could decipher its name into something like "Obviously Calculated, Somehow Excellence Needed." Let's take a gander at this code that foreshadows the peculiarity of labyrinths: sometimes things don't always work as expected!
Here's a succinct breakdown of the code functionality:
File Handling:
OCSEN.INP
.OCSEN.OUT
.Grid Initialization:
a
and mark
to store the grid and mark visited cells, respectively.Movement Logic:
tkx
and tky
to describe the movements (up, down, left, right).Processing Function: xuli
:
(x, y)
it attempts to traverse the grid. 0
.dem
is incremented for each valid move.Output Results:
-1
to the output.Array Indexing Adventures:
Boundary Issues:
tkx
and tky
are zero-indexed but your loop is 1-indexed—salvation lies in the details!Bug Busters:
tkx
and tky
are correctly zero-indexed, but access should reflect this, i.e., iterate from 0 to 3
instead.Open to Optimization:
std::memset
and loop-based array copy into separate functions to ensure clarity and reuse.If you're aching to see similar code secrets, bonkers bug hunts or tantalizing tech tutorials, trituenhantao.io awaits to unfurl its folds of technological tapestry. Visit, because as they say in AI land, curiosity didn't kill the cat, it just rebooted it with a cooler upgrade!