12,881,367 members (29,877 online)
Rate this:
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 .

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.

Can any body help me?
My code is :

```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 20-Jan-13 22:12pm
homa sh326
Updated 20-Jan-13 23:10pm
v7
CHill60 21-Jan-13 4:29am

homa sh 21-Jan-13 5:04am

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.
Sergey Alexandrovich Kryukov 30-Jun-13 17:06pm

Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

Rate this:

## Solution 1

thanks anyone
I understand ....

Top Experts
Last 24hrsThis month
 OriginalGriff 148 ppolymorphe 110 bling 95 Karthik Bangalore 40 Menon Santosh 35
 OriginalGriff 3,532 Karthik Bangalore 2,391 CHill60 2,358 Jochen Arndt 1,998 ppolymorphe 1,780