// 与sicily 1152 题意完全一样,但数据量大很多,直接深搜肯定会TLE,需要剪枝
// 可以先搜索扩展性最差的点,这样能有效减少路径数
#include <iostream> // DFS
#include<stdio.h>
#include <algorithm>
using namespace std;
int vis[10][10],path[100],rear,suc;
int mv[8][2]={{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}};
bool in(int x,int y)
{
return x>=0 && x<8 && y>=0 && y<8;
}
int expand(int x,int y)
{
int cnt=0,newx,newy;
for(int i=0;i<8;++i)
{
newx=x+mv[i][0]; newy=y+mv[i][1];
if(in(newx,newy) && !vis[newx][newy])
cnt++;
}
return cnt;
}
struct node
{
int x,y;
bool operator<(const node& o)const
{
return expand(x,y)<expand(o.x,o.y);
}
}pos[8];
void dfs(int x,int y) //需要剪枝
{
int newx,newy,tail=0;
for(int d=0;d<8;++d)
{
newx=x+mv[d][0];
newy=y+mv[d][1];
if(in(newx,newy) && !vis[newx][newy])
{
pos[tail].x=newx;
pos[tail++].y=newy;
}
}
sort(pos,pos+tail);
for(int i=0;i<tail;++i)
{
newx=pos[i].x;
newy=pos[i].y;
path[rear++]=newx*8+newy+1;
vis[newx][newy]=1;
if(rear==64) //经过所有位置
{
suc=1; //标记成功
return ;
}
dfs(newx,newy);
if(suc)
return ;
vis[newx][newy]=0;
rear--;
}
}
int main()
{
int n;
while(cin>>n&&n!=-1)
{
fill(&vis[0][0],&vis[8][8],0);
int x=(n-1)/8,y=(n-1)%8;
vis[x][y]=1;
rear=0;
path[rear++]=n;
suc=0;
dfs(x,y);
for(int i=0;i<rear;++i)
cout<<path[i]<<" ";
cout<<endl;
}
return 0;
}
sicily 1153. 马的周游问题[Special judge]
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
浅谈online judge平台 spj [special judge] 使用 | 修改问题
浅谈oj平台
c++ online judge #include ios 文件指针 -
Java编程:马踏棋盘算法(骑士周游问题)
马踏棋盘算法(骑士周游问题)
马踏棋盘 java 深度优先搜索 代码实现 数组 -
HUSTOJ特判程序Special Judge使用方法整理
Special Judge通常的ACM题目包括以下几项内容:题目描述(Description)、输入描述(Input)、输出描述(Out
HUSTOJ #include 服务器 测试数据 -
hust OJ如何写一个special judge
读了一下judge_client,如果是spj, 系统会调用system,传入spj默认路径和三个文件指针
#define #include 文件指针 -
算法-经典趣题-马踏棋盘(又称骑士周游)
一、问题 马踏棋盘问题,又称骑士漫步、,它是一个非常有趣的智力问题。马踏棋盘问题的大意如下: 国际象棋的棋盘有8行8列共64个单元格,无论将马放于棋盘
Java Algorithm 马踏棋盘 最优解 深度优先搜索 -
题目1153:括号匹配问题(栈的使用)
栈的使用,以及输出注意事项
#include 括号匹配 i++ 字符串 入栈