Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:50:47+00:00 2026-06-02T22:50:47+00:00

I use these two simple loops to search through a grid from height to

  • 0

I use these two simple loops to search through a grid from height to bottom, right to left.

for(int y=0; y<height; y++){
  for(int x=width; x>=0; x--){
  }
}

Basically, I want to search through the grid sideways like the numbers in the picture below until it covers all the cells. My approach hasn’t worked out well so I’m asking for help here.

enter image description here

How can I accomplish this the quickest way possible? I believe this should be possible to do using just two loops but I can’t figure it out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:50:49+00:00Added an answer on June 2, 2026 at 10:50 pm

    The following will do it:

     final int h = 4;
     final int w = 3;
     for (int d = 0; d < w + h; d++) {
         for (int y = 0; y < h; y++) {
             int x = w - d + y;
             if (x < 0 || x >= w) continue;
             System.out.printf("%d %d\n", x, y);
         }
     }
    

    Here, h is the height and w is the width of the grid.

    The algorithm is based on the observation that for each diagonal, the sum of the distances to the top edge and to the right edge remains constant.

    The outer loop iterates over the diagonals; the inner loop, over all cells on the diagonal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use these two classes from lucene - import org.apache.lucene.analysis.snowball.*; import org.apache.lucene.analysis.PorterStemmer;
I use these two files here and here . I created a class in
I'm confused about these two keywords and the way to use them in PHP5.
Is there really a difference in these two calls? If you use getJSON, you
I am making a site with two Youtube videos. These videos use the raw
Following these two links , I was able to implement a simple web service
I have these two files myboot.asm and theirboot.asm (listed respectively): ;---------------------------------------------------------------------- ; A Simple
simple question: How can I use a huge two-dimensional array in C#? What I
A simple Google search will reveal a multitude of solutions for converting a two
I have some doubts over how Enumerators work, and LINQ. Consider these two simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.