C++ program

Post here if you don't find any other place for your post. But please, stay on-topic: algorithms, programming or something related to this web site and its services.

Moderator: Board moderators

Post Reply
es
New poster
Posts: 1
Joined: Fri Oct 13, 2006 10:15 pm

C++ program

Post by es »

Hello.
I want to solve one problem, but every time there is an error "Wrong answer".
Can anyone help me to detect where I am wrong?

The problem is so.

We have the chess-board and one horse on it.This horse steps are (m,n), which
mean that the horse can do m steps by horizontal and n steps by vertical or
n steps by horizontal and m steps by vertical.

You get 2 positions like a5 c4, and you must say how minimal steps can do horse
and reach from first position to second.
The m and n characters is deferenet.
At the begin the horse step is(1,0).The second step must be (1,1), third - (2,1)
then (2,2), then (3,2) ...... .

ex.
simple input
c4 d5

output must be
3 Which can be c4,d4,c3,d5.

My program is(C++)

_______________________________________________________________________

#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>

using namespace std;

bool Count(short int index1, short int index2,
short int verj1, short int verj2, short int m, short int n);

vector<short int> solution;
int min = 16;

int main()
{
ifstream input("cheval.in.txt");
ofstream output("cheval.out.txt");

short int i1, i2;
char j1, j2;
input >> j1 >> i1 >> j2 >> i2;

if(i1 < 1 || i1 > 8 || i2 < 1 || i2 > 8
|| j1 < 'a' || j1 > 'h' || j2 < 'a' || j2 > 'h')
{ output << -1;
return 0;
}

if(!Count(i1, j1-'a'+1, i2, j2-'a'+1, 0, 0))
{
output << -1;
return 0;
}

while(Count(i1, j1-'a'+1, i2, j2-'a'+1, 0, 0))
{
//for print
/*
for(int i = 0;i < solution.size();i++)
output << solution[solution.size() - i-1] <<endl;
output <<endl;
*/
solution.clear();

}

output << ::min;



return 0;

}

bool Count(short int index1, short int index2,
short int verj1, short int verj2, short int m, short int n)
{
if(index1 > 8 || index2 > 8 || index1 < 1 || index2 < 1)
return false;
if(index1 == verj1 && index2 == verj2)
{
if(::min > m+n)
{
solution.push_back(index1*100+index2);
::min = m+n;
return true;
}
else
return false;
}

if(m == n) {m = n+1;}
else {n = n+1;}
if( Count(index1 - m, index2 - n, verj1, verj2, m, n)
|| Count(index1 - m, index2 + n, verj1, verj2, m, n)
|| Count(index1 + m, index2 - n, verj1, verj2, m, n)
|| Count(index1 + m, index2 + n, verj1, verj2, m, n)

|| Count(index1 - n, index2 - m, verj1, verj2, m, n)
|| Count(index1 - n, index2 + m, verj1, verj2, m, n)
|| Count(index1 + n, index2 - m, verj1, verj2, m, n)
|| Count(index1 + n, index2 + m, verj1, verj2, m, n))
{
solution.push_back(index1*100+index2);
return true;

}

}
yoshiro aoki
New poster
Posts: 21
Joined: Sat Oct 21, 2006 11:50 pm
Contact:

Post by yoshiro aoki »

hi, please identify the problem number & title that you are trying to solve.

I noticed that you are using file i/o in your program. This is not allowed by the 'Judge' program.

good luck! 8)
yoshiro (mark) aoki
Post Reply

Return to “Other words”