题目:
Description
在一个4×5的棋盘上,求马能返回初始位置的所有不同走法的总数(马走过的位置不能重复,马走“日”字)。
Input
从输入文件中读入数据。文件的第一行马的初始位置的个数n,后面n行是初始位置坐标。
Output
对于每个初始位置给出走法总数,如果不能回到初始位置,输出“ERROR”。
Sample Input
1
2 2
Sample Output
4596 我的代码如下:请大侠看看;#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;struct point
{
int x;
int y;
};bool operator == (point a, point b)
{
return (a.x == b.x && a.y == b.y);
}bool operator != (point a, point b)
{
return (a.x != b.x || a.y != b.y);
}int c = 0;
int main()
{
int dx[8] = {-2, -2, -1, -1, 1, 1, 2, 2 };
int dy[8] = {-1, 1, -2, 2, -2, 2, -1, 1 };
point pt, temp, curp;
pt.x = 2; //原始位置
pt.y = 2; int count = 0;
vector<point> vp;
vector<point> vVisted;
vector<point>::iterator itv;
vp.push_back(pt); while( !vp.empty() )
{
itv = vp.end()-1;
curp.x = itv->x;
curp.y = itv->y;
if( curp != pt )
vVisted.push_back(*itv); // 保存已经访问过的点 vp.erase(itv); // 删除栈顶元素 for(int i = 0; i < 8; i++)
{c++;
temp.x = curp.x + dx[i];
temp.y = curp.y + dy[i];
itv = std::find(vVisted.begin(), vVisted.end(), temp); if( (temp.x>0 && temp.x<5) && (temp.y>0 && temp.y<6) && (itv == vVisted.end()) )
{
if( temp == pt ) // 到达原来的位置 回溯
{
itv = vVisted.end()-1;
vVisted.erase( itv );
count++;
}
else
vp.push_back(temp);
}
}
}
cout<<count<<endl;
//cout<<c<<" "<<c/8<<endl;
return 0;
}
解决方案 »
- 请教各位高手这个问题如何解决?error LNK2001: unresolved external symbol "long __stdcall WinSunP
- VS2008,编译生成Release,报错error PRJ0019
- vc++ 操作word 使用msword类
- 文件写入问题
- ATL COM调用错误
- 在一表中取出了一条记录,然后要把这条记录修改或删除该怎么做啊??
- 在一个对话框中怎样得到视中的一个数据?(在线等)
- 求助:请问怎么release我的应用程序?
- dll ilk lib 代码修改后需要改哪个文件
- SerialPort 串口类在 Win7系统上不能正常通信为什么?
- 给予200话费做为报酬!编译出现错误请大牛来帮忙!远程都OK
- 请问MFC中类的源代码在哪可以看到?
#include <iostream>
using namespace std;struct Dir
{
int x , y ;
}dir[8] = { -2 , -1 , -2 , 1 , -1 , 2 , 1 , 2 , 2 , 1 , 2 , -1 , 1 , -2 , -1 , -2 };struct STACK
{
int x , y , dir ;
}stack[20] ;int main()
{
int top , ans ;
bool [4][5] ;
int startx , starty ;
int t ;
cin >> t ;
while( t-- )
{
ans = top = 0 ;
memset( , true , sizeof( ) ) ;
cin >> startx >> starty ;
startx --;
starty --;
[startx][starty] = false ;
stack[0].x = startx ;
stack[0].y = starty ;
stack[0].dir = 0 ; do
{
for( int i = stack[top].dir ; i < 8 ; i ++ )
{
int x = stack[top].x ;
int y = stack[top].y ;
if( x + dir[i].x > -1 && x + dir[i].x < 4 && y + dir[i].y > -1 && y + dir[i].y < 5 )
{
if( [x+dir[i].x][y+dir[i].y] )
{
stack[top].dir = i + 1 ;
top++;
stack[top].x = x + dir[i].x ;
stack[top].y = y + dir[i].y ;
[x+dir[i].x][y+dir[i].y] = false ;
i = -1 ;
continue ;
}
else
{
if ( x + dir[i].x == startx && y + dir[i].y == starty )
{
ans++;
}
}
} }
[stack[top].x][stack[top].y] = true ;
top -- ; if(top == 0 )
{ top = 0 ;
}
}while( top >= 0 );
cout << ans <<endl ;
} return 0;
}