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

Cant find the right syntax?
C# Environment.GetCommandLineArgs() from context menu
Call from Service Reference to Java Web Service returns nothing
NHibernate map object to multiple tables using same Id?
How do I use System.Net.ConnectStream?
Failed to load viewstate - Content Place holder
Selecting multiple items or objects in starUML (using ctrl + mouse left click)
events passed up chain of objects in c# and silverlight
if statement works incorrectly - disassembling explains nothing for me
Alternatives to the Dynamic Time Warping (DTW) method
Session user not register using membership provider of asp.net mvc c#
GetDynamicJsonObject exception
combobox not displaying items
How to cast List<object> to List<SomethingElse>
Major difference between 'for each' and 'for' loop in .NET [duplicate]
Improving recursion method in C#

Categories

HOME
qlikview
airflow
ssms
msbuild
liquibase
highmaps
livecharts
django-middleware
azure-resource-manager
assign
singleton
kohana
virtualhost
ionic-native
obd-ii
tracking
dat.gui
greensock
simd
ibatis
udeploy
glibc
openoffice-basic
keystone
angular2-pipe
concatenation
my.cnf
rancher
scripting-language
multilanguage
visual-studio-debugging
fastadapter
incapsula
extern
oracle-bpm-suite
redhat-brms
blueimp
fisheye
aura-framework
result
weld
jsfl
aws-rds
chain
timex
mac-app-store
eclipse-mars
dbi
disconnect
key-value-store
normalisation
aws-kms
phpdesigner
cexception
dday
overlapping
sdkman
php-gd
jain-sip
first-order-logic
nmock
dcg
p4python
page-refresh
httpmodule
attiny
qf-test
conditional-comments
growl
lwuit
redmine-plugins
livescript
banana-pi
android-relativelayout
power-management
class-library
cvi
starling-framework
complete
mencoder
dylan
listpicker
xrandr
latex-suite
codeigniter-form-helper
nx
brew
volatility
m3u
esi
ios6-maps
wxperl
parsec
helicontech
datejs
automapper-2
vectorwise
netbeans7.0

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