Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

谁来告诉我 我哪里错了 在vc过了啊

Posted by 33wing at 2010-10-04 01:22:29 on Problem 2243
#include<iostream>
#include<queue>
#include<string>
using namespace std;
int move[8][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}};
int tour();
struct chess{int x,y,step;};
chess m,n;
string str0;
string str1;
int visit[8][8];
int step;
int main()
{
	while(!cin.eof())
	{
		cin>>str0;
		cin>>str1;
		for(int i=0;i<8;i++)  
		{
			for(int j=0;j<8;j++)
				visit[i][j]=0;
		}
		m.x=str0[0]-'a';
		m.y=str0[1]-'1';
		n.x=str1[0]-'a';
		n.y=str1[1]-'1';
		m.step=0;
        visit[m.x][m.y]=1;
		if(tour())	cout<<"To get from "<<str0<<" to "<<str1<<" takes "<<step<<" knight moves."<<endl;	
	}
	return 0;
}
int tour()
{
	chess i,j;
	queue<chess> q;
	q.push(m);
	while(!q.empty())
	{
		i=q.front();
		if(i.x==n.x&&i.y==n.y)
		{
			step=i.step;
			return 1;
	
		}
		q.pop();
		for(int k=0;k<8;k++)
		{
			j.x=i.x+move[k][0];
			j.y=i.y+move[k][1];
			if(j.x>=0&&j.y<8&&j.y>=0&&j.x<8&&visit[j.x][j.y]==0)
			{
				j.step=i.step+1;
				visit[j.x][j.y]=1;
				q.push(j);
			}
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator