Home > Back-end >  Codeup 100000609 BFS problem, consult about memory overrun
Codeup 100000609 BFS problem, consult about memory overrun

Time:11-16

Doing an ordinary BFS traversal topic, topic link is as follows:
http://codeup.cn/problem.php? Cid=100000609 & amp; Pid=1

About the question is given a map of 8 * 8, there are some obstacles, from the beginning of the lower left corner to the upper right corner of the end, there are some obstacles, drop one, every step obstacles through a given example, can make her go to the end,
Typically BFS queue traversal, template approach, the Internet also has a lot of similar solution,
Attached a reference antithesis: https://blog.csdn.net/qq_43337016/article/details/97284156

My code is as follows (just posted first, will be described in more detail behind my question) :
 
#include
#include
#include
using namespace std;

Typedef struct Node {
Int x;
Int y;
Int step;
//bool stone [10] [10].
The Node (int _x, int _y) : x (_x), y (_y), step (0) {}
The Node (int _x, int _y, int _step) : x (_x), y (_y), step (_step) {}
} the Node;

Int T;
Char map [10] [10].
Bool stone [10] [10].

Int dx [9]={0, 1, 1, 0, 0, 1, 1, 1, 1};
Int dy [9]={0, 0, 0, 1, 1, 1, 1, 1, 1};

Bool flag;

Void BFS () {

Queue q;

Q.p ush (Node (7, 0, 0));

while (! Q.e mpty ()) {
The Node now=q.f ront ();
Q.p op ();

for (int i=0; i <9. I++) {
Int nx=now. X + dx [I];
Int ny=now. Y + dy [I];
If (stone [nx - now step] [ny]) continue;
If (nx & lt; 0 | | ny & lt; 0 | | nx & gt;=8 | | ny & gt;=8) continue;
If (stone [nx - now. Step 1] [ny]) continue;


If (map [nx] [ny]=='A' | | now. The step & gt; 8) {
Flag=true;
return;
}
Node TMP=Node (nx, ny, now. Step + 1);
Q.p ush (TMP);
}

}
}

Int main () {
The scanf (" % d ", & amp; T);
Int CNT=1;
While (T -) {
for (int i=0; i <8; I++) {
The scanf (" % s ", the map [I]);
}
for (int i=0; i <8; I++) {
for (int j=0; J & lt; 8; J++) {
If (map [I] [j]=='S') {
Stone [I] [j]=true;
}
}
}

BFS ();
If (flag) {
Printf (" Case # % d: Yes \ n ", CNT);
}
The else {
Printf (" Case # % d: \ n ", CNT);
}
cnt++;
flag=false;

for (int i=0; i <8; I++) {
for (int j=0; J & lt; 8; J++) {
Stone [I] [j]=false;
}
}
}

return 0;
}


Among them, the for loop inside the while loop, is the search for the following each kind of situation, and stored to the queue, but the problems encountered when condition judgment:
 
for (int i=0; i <9. I++) {
Int nx=now. X + dx [I];
Int ny=now. Y + dy [I];
If (stone [nx - now step] [ny]) continue;
If (nx & lt; 0 | | ny & lt; 0 | | nx & gt;=8 | | ny & gt;=8) continue;
If (stone [nx - now. Step 1] [ny]) continue;

If (map [nx] [ny]=='A' | | now. The step & gt; 8) {

Flag=true;
return;
}

Node TMP=Node (nx, ny, now. Step + 1);
Q.p ush (TMP);
}


In
 
If (stone [nx - now step] [ny]) continue;
.
If (stone [nx - now. Step 1] [ny]) continue;

The two statements, judge whether the current position will run into a stone, stone is a bool type array, hold the position of stones in the title,

I is not the solution to the problem, but at the time of submission will quote memory overrun, more than 128 MB,

But if I change stone array into the map array (that is, input is stored map), judge whether the point of the position is' S 'existence, can full marks, memory is about 6 MB,

I know it is not necessary to open a separate bool array to store the stone position, but could not understand why memory,

Also please give directions! Thank you
  • Related