c#


C# cube tower with Backtracking [on hold]


I received a project at university and I can't get what's the problem with my code so far.
The text of the task is:
Write a C# Console Application which takes the number n of cubes, the size, color of each of them, then print to the screen all k (also read from the keyboard) cube high towers that can be built with the two following conditions:
1. A larger cube cannot be placed on a smaller cube.
2. No cubes of the same color can be placed on top of each other.
The only specific instruction was that we have to use backtracking.
This is what I managed so far:
namespace Feladat8
{
class Program
{
static int n;
static int k;
static int currLvl = 0;
static string prevCol = "";
static string currCol;
static int prevLen = 500;
static int currLen;
static int[] len;
static string[] col;
static void orderElements()
{
for (int j = len.Length - 1; j > 0; j--)
{
for (int i = 0; i < j; i++)
{
if (len[i] < len[i + 1])
{
int t1 = len[i + 1];
string t2 = col[i + 1];
len[i + 1] = len[i];
col[i + 1] = col[i];
len[i] = t1;
col[i] = t2;
}
}
}
}
static bool backtrack(int currIndex)
{
if (currLvl == k)
{
currCol = col[currIndex+1];
currLen = len[currIndex+1];
if ((prevCol != currCol) && (prevLen >= currLen))
{
Console.Write(currCol + "-" + currLen+"; ");
currLvl--;
return true;
}
else
{
currLvl--;
return false;
}
}
else
{
for(int i = 0; i < len.Length; i++ )
{
currCol = col[i];
currLen = len[i];
currLvl++;
if ((prevCol == currCol) || (prevLen < currLen))
{
currLvl--;
return false;
}
if ((prevCol != currCol) && (prevLen >= currLen))
{
prevCol = currCol;
prevLen = currLen;
if (backtrack(i) == true)
{
Console.Write(prevCol + "-" + prevLen + " ");
prevCol = col[currIndex];
prevLen = len[currIndex];
currLvl--;
return true;
}
}
}
return false;
}
}
static void Main(string[] args)
{
Console.Write("How many cubes? ");
n = int.Parse(Console.ReadLine());
col = new string[n];
len = new int[n];
for (int i = 0; i < n; i++)
{
Console.Write("{0}th cube \nLength: ", i+1);
len[i] = int.Parse(Console.ReadLine());
Console.Write("Color: ");
col[i] = Console.ReadLine();
}
orderElements();
Console.WriteLine("How high should the tower be??");
k = int.Parse(Console.ReadLine())-1;
Console.Clear();
Console.WriteLine("Solutions: ");
for (int c = -1; c < n-1; c++)
{
backtrack(c);
Console.Write(col[c] + "-" + len[c] + " ");
currLvl = 0;
prevLen = 500;
prevCol = "";
}
Console.ReadKey();
}
}
}
I would like to ask if anyone has any idea as to why doesn't the program work.

Related Links

Azure configuration settings and Microsoft.WindowsAzure.CloudConfigurationManager
Usersettings Upgrade not working
Will Dictionary<TKey, TValue> use TKey.Equals and TKey.GetHashCode?
How can i drag over multiple or group of selected files from the hard disk files to a listBox?
IIS Windows Authentication: The (SAMAccountName=) search filter is invalid
How can you force Unity to Create a new instance?
Nested sorting for predefined filters using LINQ and RavenDB
Print a WPF control to an Image control
Displaying a table in PHP with repeated columns
select divs and put into collection using htmlagilitypack not working
Powershell: Remote Runspace vs. remote sessions
Why isn't Color Image Displaying?
About list sorting and delegates & lambda expressions Func stuff
Can you initialize Html.TextBoxFor with a value in localStorage?
Visual Studio 2012 Conditional Bundling
Query some properties on relation with NHibernate

Categories

HOME
pact
spring-boot
bigtable
highmaps
spss
sms
confluence-rest-api
foreach
aggregate-functions
liquidsoap
jquery-select2
angularjs-ng-repeat
normals
mongoimport
xmlhttprequest
hdf5
react-select
loss
game-engine
owncloud
css-float
internet-explorer-7
vivado-hls
onclicklistener
struts
expressionengine
dynamics-ax-2012-r3
jms-topic
jbossfuse
edirectory
intermec
jcr
scene7
openvms
angularjs-resource
tunnel
ansible-inventory
mkdir
pygraphviz
mediastream
tapply
jquery-ui-slider
wufoo
libzip
html-entities
weld
automapper-4
jsfl
confluent
emacs24
ng-class
dna-sequence
openpgp.js
ajv
selection-sort
spring-cloud-aws
acceleo
toggleclass
visual-web-developer
csc
boost-bind
shtml
jde
salesforce-service-cloud
jersey-test-framework
veeam
caption
kss
information-hiding
novell
register-allocation
lwuit
msdropdown
acrofields
adabas
ie-developer-tools
kohana-orm
qgraphicsscene
qt5.1
correctness
google-email-audit-api
nude.js
mcpd
httppostedfilebase
backlight
object-tag
cpack
azure-appfabric
viewdata
activescaffold
carbon-emacs
lts
pkcs#7
authkit

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App