Ural 1298 Knight 题解

题意

给定一个\(n\times n(1\le n\le8)\)的国际象棋棋盘和一个骑士(基本上相当于中国象棋的马),问可否用经过每个格子\(1\)次。如果可以,输出路径,否则输出IMPOSSIBLE

题解

考虑回溯。暴力程序十分好写,但是会超时。

可以用启发式优化。

设当前点为\((x,y)\),可到达的点为\((x',y')\)。优先选择\((x',y')\)状态种数少的回溯,即可以转移的格子的数量少。

这样优化后就可以过了。

Tip: 优化后很快,为\(0.015s\)。

程序

// #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000") // #include <bits/stdc++.h>
#include <map>
#include <set>
#include <list>
#include <array>
#include <cfenv>
#include <cmath>
#include <ctime>
#include <deque>
#include <mutex>
#include <queue>
#include <ratio>
#include <regex>
#include <stack>
#include <tuple>
#include <atomic>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <chrono>
#include <cstdio>
#include <cwchar>
#include <future>
#include <limits>
#include <locale>
#include <memory>
#include <random>
#include <string>
#include <thread>
#include <vector>
#include <cassert>
#include <climits>
#include <clocale>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <cwctype>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <ccomplex>
#include <cstdbool>
#include <iostream>
#include <typeinfo>
#include <valarray>
#include <algorithm>
#include <cinttypes>
#include <cstdalign>
#include <stdexcept>
#include <typeindex>
#include <functional>
#include <forward_list>
#include <system_error>
#include <unordered_map>
#include <unordered_set>
#include <scoped_allocator>
#include <condition_variable>
// #include <conio.h>
// #include <windows.h>
using namespace std; typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef double ld;
typedef long double LD;
typedef pair<int,int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || (WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((double)(n))
#define LD(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed<<setprecision(n) const int INF=1061109567;
const int NINF=-1044266559;
const LL LINF=4557430888798830399;
const ld eps=1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971) /*
#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffffU
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 0xffffffffU
#define LONG_MIN (-2147483647L - 1)
#define LONG_MAX 2147483647L
#define ULONG_MAX 0xffffffffUL
#define LLONG_MAX 9223372036854775807ll
#define LLONG_MIN (-9223372036854775807ll - 1)
#define ULLONG_MAX 0xffffffffffffffffull
*/ #define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(),(a).end()
#define pall(a) (a).rbegin(),(a).rend()
#define log2(x) log(x)/log(2)
#define Log(x,y) log(x)/log(y)
#define SZ(a) ((int)(a).size())
#define rep(i,n) for(int i=0;i<((int)(n));i++)
#define rep1(i,n) for(int i=1;i<=((int)(n));i++)
#define repa(i,a,n) for(int i=((int)(a));i<((int)(n));i++)
#define repa1(i,a,n) for(int i=((int)(a));i<=((int)(n));i++)
#define repd(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define repd1(i,n) for(int i=((int)(n));i>=1;i--)
#define repda(i,n,a) for(int i=((int)(n));i>((int)(a));i--)
#define repda1(i,n,a) for(int i=((int)(n));i>=((int)(a));i--)
#define FOR(i,a,n,step) for(int i=((int)(a));i<((int)(n));i+=((int)(step)))
#define repv(itr,v) for(__typeof((v).begin()) itr=(v).begin();itr!=(v).end();itr++)
#define repV(i,v) for(auto i:v)
#define repE(i,v) for(auto &i:v)
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x) MS(x,0)
#define MINF(x) MS(x,63)
#define MCP(x,y) memcpy(x,y,sizeof(y))
#define sqr(x) ((x)*(x))
#define UN(v) sort(All(v)),v.erase(unique(All(v)),v.end())
#define filein(x) freopen(x,"r",stdin)
#define fileout(x) freopen(x,"w",stdout)
#define fileio(x)\
freopen(x".in","r",stdin);\
freopen(x".out","w",stdout)
#define filein2(filename,name) ifstream name(filename,ios::in)
#define fileout2(filename,name) ofstream name(filename,ios::out)
#define file(filename,name) fstream name(filename,ios::in|ios::out)
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define PC(x) putchar(x)
#define GC(x) x=getchar()
#define Endl PC('\n')
#define SF scanf
#define PF printf inline int Read()
{
int X=0,w=0;char ch=0;while(!isdigit(ch)){w|=ch=='-';ch=getchar();}while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
inline void Write(int x){if(x<0)putchar('-'),x=-x;if(x>9)Write(x/10);putchar(x%10+'0');} inline LL powmod(LL a,LL b){LL RES=1;a%=MOD;assert(b>=0);for(;b;b>>=1){if(b&1)RES=RES*a%MOD;a=a*a%MOD;}return RES%MOD;}
inline LL gcdll(LL a,LL b){return b?gcdll(b,a%b):a;}
const int dx[]={1,1,2,2,-1,-1,-2,-2};
const int dy[]={2,-2,1,-1,2,-2,1,-1};
/************************************************************Begin************************************************************/
const int maxn=10; int n,cnt[maxn][maxn];
bool vis[maxn][maxn];
pair<int,int> pre[maxn][maxn];
vector<pair<int,int> > v; inline bool ok()
{
rep(i,n) rep(j,n) if(!vis[i][j]) return 0;
return 1;
} inline void print(int x,int y)
{
if(pre[x][y].fs!=-1) print(pre[x][y].fs,pre[x][y].sc);
PF("%c%c\n",char(x+'a'),char(y+'1'));
} inline bool cmp(pair<int,int> x,pair<int,int> y)
{
return cnt[x.fs][x.sc]<cnt[y.fs][y.sc];
} inline void dfs(int x,int y)
{
vis[x][y]=1;
if(ok())
{
print(x,y);
exit(0);
} vector<pair<int,int> > w;w.clear();
rep(i,8)
{
int cx=x+dx[i],cy=y+dy[i];
if(cx>=0&&cx<n&&cy>=0&&cy<n&&!vis[cx][cy]) w.push_back({cx,cy});
} sort(All(w),cmp); repV(i,w)
{
int cx=i.fs,cy=i.sc;
pre[cx][cy]={x,y};
dfs(cx,cy);
} vis[x][y]=0;
} int main()
{
SF("%d",&n); rep(i,n) rep(j,n)
{
v.push_back({i,j}); rep(k,8)
{
int ci=i+dx[k],cj=j+dy[k];
if(ci>=0&&ci<n&&cj>=0&&cj<n) cnt[i][j]++;
}
} sort(All(v),cmp); repV(it,v)
{
int i=it.fs,j=it.sc; MC(vis);
MC(pre);
pre[i][j]={-1,-1}; dfs(i,j);
} PF("IMPOSSIBLE"); return 0;
}
/*************************************************************End**************************************************************/

Ural 1298 Knight 题解的更多相关文章

  1. Ural 1029 Ministry 题解

    目录 Ural 1029 Ministry 题解 题意 题解 程序 Ural 1029 Ministry 题解 题意 给定一个\(n\times m(1\le n \le10,1\le m \le50 ...

  2. Ural 1238 Folding 题解

    目录 Ural 1238 Folding 题解 题意 题解 程序 Ural 1238 Folding 题解 题意 定义折叠.展开为: 单个大写英文字母是一个折叠的串,把它展开后是它本身. 如果\(S\ ...

  3. URAL 1936 Roshambo 题解

    http://acm.timus.ru/problem.aspx?space=1&num=1936 F - Roshambo Time Limit:1000MS Memory Limit:65 ...

  4. 【计算几何】URAL - 2101 - Knight's Shield

    Little Peter Ivanov likes to play knights. Or musketeers. Or samurai. It depends on his mood. For pa ...

  5. hdu1251

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  6. Ural 1197 - Lonesome Knight

    The statement of this problem is very simple: you are to determine how many squares of the chessboar ...

  7. URAL题解三

    URAL题解三 URAL 1045 题目描述:有\(n\)个机场,\(n-1\)条航线,任意两个机场有且只有一种方案联通.现有两个恐怖分子从\(m\)号机场出发,第一个人在机场安装炸弹,乘坐飞机,引爆 ...

  8. URAL题解二

    URAL题解二 URAL 1082 题目描述:输出程序的输入数据,使得程序输出"Beutiful Vasilisa" solution 一开始只看程序的核心部分,发现是求快排的比较 ...

  9. URAL题解一

    URAL题解一 URAL 1002 题目描述:一种记住手机号的方法就是将字母与数字对应,如图.这样就可以只记住一些单词,而不用记住数字.给出一个数字串和n个单词,用最少的单词数来代替数字串,输出对应的 ...

随机推荐

  1. Maven项目转化出WebContent风格的目录

    这篇文章已被废弃. 现在,Deolin已经比较熟悉Maven,完全可以使用Jetty插件调试.热部署项目了. 在 [SpringMVC框架下Web项目的搭建与部署] 中,提到了将tomcat的默认上下 ...

  2. 5.4.1 sequenceFile读写文件、记录边界、同步点、压缩排序、格式

    5.4.1      sequenceFile读写文件.记录边界.同步点.压缩排序.格式 HDFS和MapReduce是针对大文件优化的存储文本记录,不适合二进制类型的数据.SequenceFile作 ...

  3. Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤

    Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤 一.在ubuntu下尝试 1. 在虚拟机VWware Workstation 12. ...

  4. DockerFile体系结构(保留字指令)

    1.FROM --基础镜像,当前新镜像是基于那个镜像的 2.MAINTAINER --镜像维护者的姓名和邮箱地址 3.RUN --容器构建时需要运行的指令 4.EXPOSE --当前容器对外暴露出的端 ...

  5. Hadoop环境搭建|第四篇:hive环境搭建

    一.环境搭建 注意:hive版本不能太高,否则会出现兼容性问题 1.1.上传hive安装包 创建文件夹用于存放hive文件命令:mkdir hive 1.2.解压hive安装包 命令:tar -zxv ...

  6. Flask 编写一个授权登录验证的模块(一)

    看一个关于授权登陆的简易模块,觉得挺不错,学习学习. 1.登录的逻辑:如果用户名和密码正确,就返回 token .2.生成 token 的逻辑,根据用户名,随机数,当前时间 + 2 小时3.然后放在 ...

  7. DataTable 转换为List

           注意table 列的参数类型,若不为string 需要详细声明 如 typeof(Int32)          public static IList<T> Convert ...

  8. 模糊C均值聚类-FCM算法

    FCM(fuzzy c-means) 模糊c均值聚类融合了模糊理论的精髓.相较于k-means的硬聚类,模糊c提供了更加灵活的聚类结果.因为大部分情况下,数据集中的对象不能划分成为明显分离的簇,指派一 ...

  9. javascript-类型、值和变量

    基本类型和引用类型 MDN-JavaScript 数据类型和数据结构 ECMAScript 变量可能包含两种不同数据类型的值:基本类型值和引用类型值.基本类型值指的是 简单的数据段,而引用类型值指那些 ...

  10. 20 Flutter仿京东商城项目 商品详情 底部弹出筛选属性 以及筛选属性页面布局

    ProductContentFirst.dart import 'package:flutter/material.dart'; import '../../widget/JdButton.dart' ...