http://acm.jlu.edu.cn/joj/showproblem.php?pid=2771&off=2700
Problem B: Chinese KnightThere are different pieces In Chinese Chess, such as General, Advisor, Elephant, Knight, Chariot, Cannon and Solider.The knights(horses) are labeled 馬. They begin the game next to the elephants. A knight moves and captures one point orthogonally and then one point diagonally away from its former position, a move which is traditionally described as being like the Chinese character „日‟. The knight does not jump as the knight does in Western chess. Thus, if there were a piece lying on a point one point away horizontally or vertically from the horse, then the horse's path of movement is blocked and it is unable to move in that direction. Note, however, that a piece two points orthogonally or a single point away diagonally would not impede the movement of the horse. Blocking a horse is also known as "hobbling the horse's leg". The figure 2.1 illustrates the horse's movement.Since knight can be blocked, it is sometimes possible to trap the opponent's horse. It is possible for one player's knight to attack the opponent's horse while the opponent's knight is blocked from attacking, as seen in the figure 2.2.Figure 2.1 Movement of knight Figure 2.2 Asymmetric movementOn a n*n grid chessboard, a Chinese knight want to visit each placement exactly once. This is just like traditional “Knight‟s tour problem”. Unfortunately, there are some obstacles on the placement. The knight can‟t reach it‟s placement and maybe blocked by these obstacles. On the other hand, it is also fortunate, the number of all possible paths is decreased rapidly.Our knight will always begin his tour at top-left. The number of obstacles is between 1 and 5. You should calculate the number of valid paths. It is not necessary that the knight return his starting position. And all the obstacles is excluded from the path.6th Jilin Province Collegiate Programming Contest - Mar 20, 2012InputThe first line of each case are three integers m, n and k. m and n are the size of chessboard (m, n<=6) and k is indicated the number of obstacles. k is -1 means the end of input.The next k lines are two numbers that are coordinates of each obstacle (startingfrom 1).
OutputFor each case, output the number of all successful paths.
Sample Input5 5 22 35 55 5 -1
Sample Output2
HintOne of possible path of the sample input is described as follows. Each number is the step of knight except -1 means obstacle.1 18 13 6 2312 7 -1 19 1417 2 9 22 58 11 4 15 203 16 21 10 -1
参考代码:
#include<iostream>
#include<cstring>
using namespace std;
int m,n,k;
int sum,total;
int cB[7][7];
int dir[8][2]={{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2},{-1,2}};
void fWay(int px,int py,int no)
{
if(no==total)
{
sum++;
return;
}
int x,y;
for(int i=0;i<8;i++)
{
x=dir[i][0]+px;
y=dir[i][1]+py;
if(x<1||x>m||y<1||y>n||cB[x][y]!=0)
continue;
if(i==0||i==1)
{
if(cB[px-1][py]==-1)
continue;
}
else if(i==2||i==3)
{
if(cB[px][py-1]==-1)
continue;
}
else if(i==4||i==5)
{
if(cB[px+1][py]==-1)
continue;
}
else if(i==6||i==7)
{
if(cB[px][py+1]==-1)
continue;
}
cB[x][y]=no+1;
fWay(x,y,cB[x][y]);
cB[x][y]=0;
}
}
int main()
{
cin>>m>>n>>k;
while(k!=-1)
{
sum=0;
memset(cB,0,sizeof(cB));
int x,y;
for(int i=0;i<k;i++)
{
cin>>x>>y;
cB[x][y]=-1;
}
total=m*n-k;
cB[1][1]=1;
fWay(1,1,1);
cout<<sum<<endl;
cin>>m>>n>>k;
}
return 0;
}
说明:
这个的时间虽然也不是很少,但是相比之前的要好得多~