Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
I want to program for ship game : according to below text
For example we have 4 ship and we have a table 4*4 and we want put ship in this table according to capacity of column and row.
Therefore I used backtracking and for this shape is true answer .

http://upload.ugm.ac.id/434shape.docx

It is true answer when we have ships enter with this order : ship 1-ship2- ship3-ship4
But if ships had this order : ship 2-ship1- ship3-ship4 the answer will be wrong.
Actually if input shape was according to below shape , my answer is wrong.

http://upload.ugm.ac.id/434shape.docx[^]

Can any body help me?
My code is :

C++
void General::Ship(int count,int k,int n)  //  count <= k  
{
	 int lenght;

	lenght=L[count];    // L contan ships length means for example 2:is 3-2-2-2

	for (int x=1;x<=n;x++)
	{
		for(int y=1;y<=n;y++)
		{
			if (IsOkhorizontal(x,y,lenght,n)==1 )
			{
				Horizontally(x,y,lenght,count,n);
				if(count==k)
				{
					Print();
				}
				else
					Ship(count+1,k,n);
			}
		    if (IsOkvertical(x,y,lenght,n)==1)
			{
			    Vertically(x,y,lenght,count,n);
				if(count==k)
				{
					Print();
				}
				else
					Ship(count+1,k,n);
			}
			
		}	
	}
}


For this example we should calling ship(1,4,4)
thanks
Posted
Updated 20-Jan-13 23:10pm
v7
Comments
CHill60 21-Jan-13 4:29am    
It's not clear what your question is and I can't use your hyperlinks
homa sh 21-Jan-13 5:04am    
I change my link ..
is this OK?
CHill60 21-Jan-13 8:33am    
Sorry - when I said I can't use your hyperlinks it's because I'm at work and that site is blocked by our firewall. We're still not sure what your question is though
homa sh 21-Jan-13 9:41am    
actually we have a table n*n and some ships . we should put ships in table so that sum numbers row and column not larger than number of top of table.

i think shape is clear .
Quirkafleeg 24-Jan-13 12:31pm    
The problem with this is hard to diagnose. Your variable names remind me of horror stories that I've been told about 30 year old code; you are allowed - and recommended - to give your variables meaningful names, rather single letters.

For example, the parameters "k" and "n" do not help anyone here scan your code for immediate issues. What it does instead is force us to spend more time than we should examining (and decrypting) the code.
Most of us are either too busy or too lazy to do that.

I am probably similar to a number of users by a being averse to having to download things (which is what your hyperlink asks me to do!) - it adds to the "we are having to spend far more time on this than we should" argument.

What also makes it difficult is the other functions that this code calls - there is no code snippets of these functions. As such, when you say "It not worky!", there is no evidence to show what the expected output and the actual output is.
From what I can see, the code snippet seemingly has no problems - the real problem may lie in the code you have not posted, e.g. "Horizontally" or "Vertically" (do these change the value of count?)

So I am sorry to sound harsh, but I cannot help you without at least more code logic - and I'm guessing other people would post a similar request.

1 solution

thanks anyone
I understand ....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900