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

How do I correlate a Linq query extension method to its signature in the documentation?
Datagridview event not firing
How do I redistribute Microsoft.AnalysisServices.SPClient.Interfaces
Get Standard Touchpad Swipe
How to calculate the sum of the datatable column in c#
System.ArgumentException “Parameter is not valid” when comparing image dimensions
Set field via class
how to use ModelState.IsValid in ASP.NET?
protobuf-net : v2 deserialize v1 objects with inheritance
Getting data on ListView Select event and setting it in a div in C#
c# Oledb add list of parameters in a foreach loop
dotnet ef not works with ef tools 1.1.0-preview4-final
Check permission of a PDF using itext [duplicate]
Is there an event fired when WebBrowser content is redrawn?
How can I filter a DataTable without brute-forcing it?
Creating objects using an older version of Google Protobuf

Categories

HOME
asterisk
algolia
itunesconnect
openacc
wifi
grpc
dronekit
azure-resource-manager
bitbucket
multipartform-data
revit-api
promotions
ethereum
digital
captcha
fastreport
javascript-debugger
exchange-transport-agents
tag-cloud
vivado-hls
android-actionbar
views
audio-streaming
fastboot
rvm
oracle-golden-gate
symlink
setup-project
typeclass
emr
delphi-2010
openoffice-impress
overflow
login-required
contextmenustrip
imageprocessor
webmock
incapsula
mkdir
ragel
abide
extern
stencyl
iterm2
byobu
gem-fury
linkurious
idea-plugin
android-graphview
pyexcel
text-extraction
inner-join
html-entities
spring-webflow-2
line-endings
nao-robot
appcmd
dotnet-cli
px4
jenkins-docker
hellosign
cc
robotc
catalyst
universe
git-cherry-pick
airwatch
nsoperationqueue
shtml
jde
nd4j
ideavim
abstract
jsondoc
sqldataadapter
matlab-engine
gitk
skview
chrome-mobile
esri-arc-engine
method-overloading
report-builder2.0
diffmerge
cocos2d-x-2.x
fscanf
time-frequency
mvcgrid
isl
visual-studio-setup-proje
jsmpp
aspnet-compiler
cab
google-checkout
generator-expression
esi
openafs
shapado
animationdrawable
project-files
copyfile
wdm
isgl3d
vectorwise
cpack
xslt-grouping
pkcs#7
data-formats
httpbrowsercapabilities
asp.net-1.1

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