473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Help with for loop

4
I'm trying to create a for loop or an alternative for a recursive backtracking problem. this is how i want the for loop to go through but i'm confused on how to implement it.

given an int[][] grid , row, and col

for(grid[row][col+1] then grid[row][col-1] then grid[row+1][col] then grid[row-1][col])

any tips would help a lot thanks!
Oct 23 '08 #1
6 1201
Nepomuk
3,112 Expert 2GB
I'm trying to create a for loop or an alternative for a recursive backtracking problem. this is how i want the for loop to go through but i'm confused on how to implement it.

given an int[][] grid , row, and col

for(grid[row][col+1] then grid[row][col-1] then grid[row+1][col] then grid[row-1][col])

any tips would help a lot thanks!
I'm not quite sure I understand your question.

Do you mean, you want to go through all possible values for row and col and use those 4 values in every run? Or are you trying to write one for loop, which will go through exactly these 4 values and nothing else?

Greetings,
Nepomuk
Oct 23 '08 #2
pk1m
4
I'm not quite sure I understand your question.

Do you mean, you want to go through all possible values for row and col and use those 4 values in every run? Or are you trying to write one for loop, which will go through exactly these 4 values and nothing else?

Greetings,
Nepomuk
exactly those 4 values, im writing a something similar to recursive backtracking maze algorithm where it goes north,west,east,south in a for loop.
Oct 23 '08 #3
Laharl
849 Expert 512MB
You don't really need a for loop for that. Just put the statements into the code, if none of those four values ever change. Alternatively, if you feel you must use a loop, use
Expand|Select|Wrap|Line Numbers
  1. do { //code here } while(false);
A do loop is identical to a while loop except that it iterates the code once before checking the termination condition.
Oct 23 '08 #4
pk1m
4
You don't really need a for loop for that. Just put the statements into the code, if none of those four values ever change. Alternatively, if you feel you must use a loop, use
Expand|Select|Wrap|Line Numbers
  1. do { //code here } while(false);
A do loop is identical to a while loop except that it iterates the code once before checking the termination condition.
the user inputs a 2d array so it will always be changing. i want it to run the for loop for the locations i specified up there
Oct 24 '08 #5
JosAH
11,448 Expert 8TB
I'm trying to create a for loop or an alternative for a recursive backtracking problem. this is how i want the for loop to go through but i'm confused on how to implement it.

given an int[][] grid , row, and col

for(grid[row][col+1] then grid[row][col-1] then grid[row+1][col] then grid[row-1][col])

any tips would help a lot thanks!
Use auxiliary 'direction' arrays:
Expand|Select|Wrap|Line Numbers
  1. int[] drow= { 0, 0, 1, -1 };
  2. int[] dcol= { 1, -1, 0, 0 };
  3. ...
  4. for (int i= 0; i < drow.length; i++)
  5.    // process grid[row+drow[i]][col+dcol[i]]
  6.  
kind regards,

Jos
Oct 24 '08 #6
pk1m
4
Use auxiliary 'direction' arrays:
Expand|Select|Wrap|Line Numbers
  1. int[] drow= { 0, 0, 1, -1 };
  2. int[] dcol= { 1, -1, 0, 0 };
  3. ...
  4. for (int i= 0; i < drow.length; i++)
  5.    // process grid[row+drow[i]][col+dcol[i]]
  6.  
kind regards,

Jos
thats what i was looking for, thanks a bunch!
Oct 24 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: William Buchanan | last post by:
Hi I have the following stored proc in interbase (which might contain errors - i'm doing it off the top of my head), which I would like to convert into oracle. Can you help? What I want back is...
1
by: nightsaber | last post by:
<script language="JavaScript"> <!-- hide me var the_number = prompt("how many words (3-5 is good)?", "4"); var the_string = ""; var a_word; for (loop = 0; loop < the_number; loop++) {...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
1
by: al2004 | last post by:
Write a program that reads information about youth soccer teams from a file, calculates the average score for each team and prints the averages in a neatly formatted table along with the team name....
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
9
by: TF | last post by:
Hello all, I made a ASP.NET 2.0 site that shows possible "recipes" for paint colors stored in an access dbase. Basically, 1000 colors are stored with specific RGB values in separate columns. A...
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
41
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that...
1
by: mshroom12 | last post by:
Hello to all. I am having difficulty trying to do this Java project using Eclipse. The following is what I have to do. Election Day It's almost election day and the election officials need a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.