This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
1926 The nearest exit to the entrance in the maze
Thought analysis
Given that this problem is a breadth-first traversal problem, we need to find the number of steps that the first person takes to reach the edge of the maze that is not the starting point.
It is natural, but not elegant, to judge whether it is a starting point on the basis of judging whether it is a wall every time you reach the edge, so you can merge the two judgments by changing the starting point to ‘+’.
In the breadth-first question, when to mark a point in the array as read, and when to judge whether it is the answer according to the points in the queue, I think it is not the same level. For example, in this question, our judgment condition is “. “on the edge, and when we traversal the breadth layer by layer, we inevitably need to make an edge judgment.
So, it’s definitely not necessary to judge the same thing twice, so we can merge the two into one
Nature has:
while(q.size() != 0) {auto [cx, cy, d] = q.front(a); q.pop(a);for (int i = 0; i < 4; i++){
int cxn = cx + ls[i], cyn = cy + ls[i];
if (cxn == 0 || cxn == l - 1 || cyn == 0 || cyn == r - 1) {if (maze[cxn][cyn] == '. ')return d+1;
}else if(cxn > 0 || cxn < l - 1 || cyn > 0 || cyn < r - 1) {if (maze[cxn][cyn] == '. '){
maze[cxn][cyn] = '+';
q.emplace(cxn, cyn, d + 1); }}}}Copy the code
When the use case is originally an edge operation, this if judgment will be calculated out of bounds, will array out of bounds, look at the answer, more harmonious, but it is hard to follow their own logic.
1927 Summation Game
Comment area big guy’s train of thought analysis
Calculate the difference in the number of question marks and the difference in the number of numbers on both sides, and lose if they are both 0 or on the same side.
If the difference in numbers can be made up by the difference in numbers, you win.
So I have one line of code
return` ` `! ((sum ==0 && cnt == 0) || (cnt % 2= =0 && (sum ^ cnt) < 0 && Math.abs(sum) == Math.abs(cnt * 9) / 2))
Copy the code