#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//addons
#include <iostream>
#include <stack>
#include <ctime>
#include <list>
#include <vector>
//using namespace std;
using std::cout;
using std::endl;
//
#define N 8
#define STMAX N*N
//int stack[STMAX];
int stp = 0;
int board[N][N];
int x,
y,
newx,
newy,
move_number,
move_type;
template< typename T > void push(int v, T &stackRef);
template< typename T > int pop( T &stackRef );
template< typename T > int top( T &stackRef );
template< typename T > void retract_move (T &stackRef);
int stack_empty();
int stack_full();
int main () {
//addons
std::stack<int> intDequeStack;
//
int p=0;
unsigned long t;
double time;
int move_valid ();
void print_board();
void try_move (int);
//addons
void push(int v, intDequeStack);
pop( intDequeStack);
top( intDequeStack);
//
// void push(int v);
// int pop();
// int top();
int stack_empty();
int stack_full();
board[0][0] = 1;
move_number = 2;
move_type = 0;
newx = x = 0;
newy = y = 0;
// insert prior to the code you want to time
t = clock();
do {
do {
while (move_type == 8) {
retract_move (intDequeStack);
}
move_type++;
try_move (move_type);
} while (!move_valid ());
p++;
push (move_type);
x = newx;
y = newy;
move_type = 0;
board[x][y] = move_number++;
if(move_number> N*N)
print_board();
} while (move_number <= N*N);
// insert after to the code you want to time
time = ((double)(clock() - t)) / CLOCKS_PER_SEC;
printf ("\n\nTime taken = %6.1f secs.\t %d pops\n\n\n", time, p);
}
void print_board()
{
int i,j;
printf("\n\n\n");
for(i=N-1; i>=0; i--) {
for(j=0; j<N; j++)
printf("\t%d", board[j][i]);
printf("\n\n");
}
}
int move_valid () {
return ((newx >= 0) && (newx < N) &&
(newy >= 0) && (newy < N) &&
(board[newx][newy] == 0));
}
void try_move (int mt) {
switch (mt) {
case 1:
newx = x + 1;
newy = y + 2;
break;
case 2:
newx = x + 2;
newy = y + 1;
break;
case 3:
newx = x + 2;
newy = y - 1;
break;
case 4:
newx = x + 1;
newy = y - 2;
break;
case 5:
newx = x - 1;
newy = y - 2;
break;
case 6:
newx = x - 2;
newy = y - 1;
break;
case 7:
newx = x - 2;
newy = y + 1;
break;
case 8:
newx = x - 1;
newy = y + 2;
break;
default:
printf ("Illegal move type %d in try_move", move_type);
exit (1);
}
}
template< typename T > void retract_move (T &stackRef) {
int mtype;
if (!stack_empty())
move_type = pop(intDequeStack);
else
printf (" exiting\n\n"), exit (-1);
board[x][y] = 0;
mtype =(move_type+4)%8;
if (mtype==0)mtype=8;
try_move (mtype);
x = newx;
y = newy;
move_number--;
}
template< typename T > void push(int v, T &stackRef)
{
//stack[stp++] = v;
stackRef.push(v);
}
template< typename T > int pop(T &stackRef)
{
//return stack[--stp];
return stackRef.pop;
}
template< typename T > int top(T&stackRef)
{
//return stack[stp-1];
return stackRef.top();
}
int stack_empty()
{
return stp == 0;
}
int stack_full()
{
return stp == STMAX;
}
Wasn't sure whether to paste the entire code or not but I went for gold.
"tours 8 - 8.cpp(52) : error C2061: syntax error : identifier 'intDequeStack'"
This is the error I get, I'd much appreciate anyone who could fix or explain to me how to fix this problem.

New Topic/Question
Reply




MultiQuote





|