Iterating over the boundary of a grid

Revision en2, by 0-jij-0, 2021-11-11 00:47:23

Hello,

I was wondering if there's any trick that allows us to iterate over the boundary of an $$$N \times M$$$ grid using a single loop instead of 4 or 2.

To iterate over the whole grid for example we can iterate from 0 to $$$NM$$$ and have $$$i = x / M$$$ and $$$j = x \% M$$$.

So is there any smart thing like this to do for the boundary only (i.e pairs $$$(i, j)$$$ such that either $$$i = 0$$$ or $$$i = N - 1$$$ or $$$j = 0$$$ or $$$j = M - 1$$$)

Thanks for future help!

EDIT: Problem Solved!

Solution 1
Solution 2
My Solution inspired by the previous one
Tags grid

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English 0-jij-0 2021-11-11 00:47:23 1182 Tiny change: 'ture help!\n\nEDIT: ' -> 'ture help! [user:under_score_07]\n\nEDIT: '
en1 English 0-jij-0 2021-11-10 17:44:40 488 Initial revision (published)