Ural 1298 Knight 题解

题意

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

题解

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

可以用启发式优化。

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

这样优化后就可以过了。

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

程序

  1. // #pragma GCC optimize(2)
  2. // #pragma G++ optimize(2)
  3. // #pragma comment(linker,"/STACK:102400000,102400000")
  4. // #include <bits/stdc++.h>
  5. #include <map>
  6. #include <set>
  7. #include <list>
  8. #include <array>
  9. #include <cfenv>
  10. #include <cmath>
  11. #include <ctime>
  12. #include <deque>
  13. #include <mutex>
  14. #include <queue>
  15. #include <ratio>
  16. #include <regex>
  17. #include <stack>
  18. #include <tuple>
  19. #include <atomic>
  20. #include <bitset>
  21. #include <cctype>
  22. #include <cerrno>
  23. #include <cfloat>
  24. #include <chrono>
  25. #include <cstdio>
  26. #include <cwchar>
  27. #include <future>
  28. #include <limits>
  29. #include <locale>
  30. #include <memory>
  31. #include <random>
  32. #include <string>
  33. #include <thread>
  34. #include <vector>
  35. #include <cassert>
  36. #include <climits>
  37. #include <clocale>
  38. #include <complex>
  39. #include <csetjmp>
  40. #include <csignal>
  41. #include <cstdarg>
  42. #include <cstddef>
  43. #include <cstdint>
  44. #include <cstdlib>
  45. #include <cstring>
  46. #include <ctgmath>
  47. #include <cwctype>
  48. #include <fstream>
  49. #include <iomanip>
  50. #include <numeric>
  51. #include <sstream>
  52. #include <ccomplex>
  53. #include <cstdbool>
  54. #include <iostream>
  55. #include <typeinfo>
  56. #include <valarray>
  57. #include <algorithm>
  58. #include <cinttypes>
  59. #include <cstdalign>
  60. #include <stdexcept>
  61. #include <typeindex>
  62. #include <functional>
  63. #include <forward_list>
  64. #include <system_error>
  65. #include <unordered_map>
  66. #include <unordered_set>
  67. #include <scoped_allocator>
  68. #include <condition_variable>
  69. // #include <conio.h>
  70. // #include <windows.h>
  71. using namespace std;
  72. typedef long long LL;
  73. typedef unsigned int ui;
  74. typedef unsigned long long ull;
  75. typedef float fl;
  76. typedef double ld;
  77. typedef long double LD;
  78. typedef pair<int,int> pii;
  79. #if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || (WINDOWS)
  80. #define lld "%I64d"
  81. #define llu "%I64u"
  82. #else
  83. #define lld "%lld"
  84. #define llu "%llu"
  85. #endif
  86. #define ui(n) ((unsigned int)(n))
  87. #define LL(n) ((long long)(n))
  88. #define ull(n) ((unsigned long long)(n))
  89. #define fl(n) ((float)(n))
  90. #define ld(n) ((double)(n))
  91. #define LD(n) ((long double)(n))
  92. #define char(n) ((char)(n))
  93. #define Bool(n) ((bool)(n))
  94. #define fixpoint(n) fixed<<setprecision(n)
  95. const int INF=1061109567;
  96. const int NINF=-1044266559;
  97. const LL LINF=4557430888798830399;
  98. const ld eps=1e-15;
  99. #define MOD (1000000007)
  100. #define PI (3.1415926535897932384626433832795028841971)
  101. /*
  102. #define MB_LEN_MAX 5
  103. #define SHRT_MIN (-32768)
  104. #define SHRT_MAX 32767
  105. #define USHRT_MAX 0xffffU
  106. #define INT_MIN (-2147483647 - 1)
  107. #define INT_MAX 2147483647
  108. #define UINT_MAX 0xffffffffU
  109. #define LONG_MIN (-2147483647L - 1)
  110. #define LONG_MAX 2147483647L
  111. #define ULONG_MAX 0xffffffffUL
  112. #define LLONG_MAX 9223372036854775807ll
  113. #define LLONG_MIN (-9223372036854775807ll - 1)
  114. #define ULLONG_MAX 0xffffffffffffffffull
  115. */
  116. #define MP make_pair
  117. #define MT make_tuple
  118. #define All(a) (a).begin(),(a).end()
  119. #define pall(a) (a).rbegin(),(a).rend()
  120. #define log2(x) log(x)/log(2)
  121. #define Log(x,y) log(x)/log(y)
  122. #define SZ(a) ((int)(a).size())
  123. #define rep(i,n) for(int i=0;i<((int)(n));i++)
  124. #define rep1(i,n) for(int i=1;i<=((int)(n));i++)
  125. #define repa(i,a,n) for(int i=((int)(a));i<((int)(n));i++)
  126. #define repa1(i,a,n) for(int i=((int)(a));i<=((int)(n));i++)
  127. #define repd(i,n) for(int i=((int)(n))-1;i>=0;i--)
  128. #define repd1(i,n) for(int i=((int)(n));i>=1;i--)
  129. #define repda(i,n,a) for(int i=((int)(n));i>((int)(a));i--)
  130. #define repda1(i,n,a) for(int i=((int)(n));i>=((int)(a));i--)
  131. #define FOR(i,a,n,step) for(int i=((int)(a));i<((int)(n));i+=((int)(step)))
  132. #define repv(itr,v) for(__typeof((v).begin()) itr=(v).begin();itr!=(v).end();itr++)
  133. #define repV(i,v) for(auto i:v)
  134. #define repE(i,v) for(auto &i:v)
  135. #define MS(x,y) memset(x,y,sizeof(x))
  136. #define MC(x) MS(x,0)
  137. #define MINF(x) MS(x,63)
  138. #define MCP(x,y) memcpy(x,y,sizeof(y))
  139. #define sqr(x) ((x)*(x))
  140. #define UN(v) sort(All(v)),v.erase(unique(All(v)),v.end())
  141. #define filein(x) freopen(x,"r",stdin)
  142. #define fileout(x) freopen(x,"w",stdout)
  143. #define fileio(x)\
  144. freopen(x".in","r",stdin);\
  145. freopen(x".out","w",stdout)
  146. #define filein2(filename,name) ifstream name(filename,ios::in)
  147. #define fileout2(filename,name) ofstream name(filename,ios::out)
  148. #define file(filename,name) fstream name(filename,ios::in|ios::out)
  149. #define Pause system("pause")
  150. #define Cls system("cls")
  151. #define fs first
  152. #define sc second
  153. #define PC(x) putchar(x)
  154. #define GC(x) x=getchar()
  155. #define Endl PC('\n')
  156. #define SF scanf
  157. #define PF printf
  158. inline int Read()
  159. {
  160. 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();
  161. return w?-X:X;
  162. }
  163. inline void Write(int x){if(x<0)putchar('-'),x=-x;if(x>9)Write(x/10);putchar(x%10+'0');}
  164. 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;}
  165. inline LL gcdll(LL a,LL b){return b?gcdll(b,a%b):a;}
  166. const int dx[]={1,1,2,2,-1,-1,-2,-2};
  167. const int dy[]={2,-2,1,-1,2,-2,1,-1};
  168. /************************************************************Begin************************************************************/
  169. const int maxn=10;
  170. int n,cnt[maxn][maxn];
  171. bool vis[maxn][maxn];
  172. pair<int,int> pre[maxn][maxn];
  173. vector<pair<int,int> > v;
  174. inline bool ok()
  175. {
  176. rep(i,n) rep(j,n) if(!vis[i][j]) return 0;
  177. return 1;
  178. }
  179. inline void print(int x,int y)
  180. {
  181. if(pre[x][y].fs!=-1) print(pre[x][y].fs,pre[x][y].sc);
  182. PF("%c%c\n",char(x+'a'),char(y+'1'));
  183. }
  184. inline bool cmp(pair<int,int> x,pair<int,int> y)
  185. {
  186. return cnt[x.fs][x.sc]<cnt[y.fs][y.sc];
  187. }
  188. inline void dfs(int x,int y)
  189. {
  190. vis[x][y]=1;
  191. if(ok())
  192. {
  193. print(x,y);
  194. exit(0);
  195. }
  196. vector<pair<int,int> > w;w.clear();
  197. rep(i,8)
  198. {
  199. int cx=x+dx[i],cy=y+dy[i];
  200. if(cx>=0&&cx<n&&cy>=0&&cy<n&&!vis[cx][cy]) w.push_back({cx,cy});
  201. }
  202. sort(All(w),cmp);
  203. repV(i,w)
  204. {
  205. int cx=i.fs,cy=i.sc;
  206. pre[cx][cy]={x,y};
  207. dfs(cx,cy);
  208. }
  209. vis[x][y]=0;
  210. }
  211. int main()
  212. {
  213. SF("%d",&n);
  214. rep(i,n) rep(j,n)
  215. {
  216. v.push_back({i,j});
  217. rep(k,8)
  218. {
  219. int ci=i+dx[k],cj=j+dy[k];
  220. if(ci>=0&&ci<n&&cj>=0&&cj<n) cnt[i][j]++;
  221. }
  222. }
  223. sort(All(v),cmp);
  224. repV(it,v)
  225. {
  226. int i=it.fs,j=it.sc;
  227. MC(vis);
  228. MC(pre);
  229. pre[i][j]={-1,-1};
  230. dfs(i,j);
  231. }
  232. PF("IMPOSSIBLE");
  233. return 0;
  234. }
  235. /*************************************************************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. 二十五、grub (Boot Loader) 以及修复grub

    双系统安装(先Windows后Linux,以免windows NTloader会覆盖Linux loader) GRUB Grand Uniform Bootloader CentOS5,6 grub ...

  2. vfork与fork的区别

    vfork()用法与fork()相似,但是也有区别,具体区别归结为以下3点: 1. fork():子进程拷贝父进程的数据段,代码段.vfork():子进程与父进程共享数据段. 2. fork():父子 ...

  3. elasticsearch shield(5.0以下版本 权限认证)

    elasticsearch 5.0以下的版本要用到权限控制的话需要使用shield.下载地址: https://www.elastic.co/downloads/shield5.0以上的版本则可以使用 ...

  4. Cesium入门-3-官方完整实例

    实例核心代码 //资源访问令牌 Cesium token Cesium.Ion.defaultAccessToken='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ ...

  5. win10+mysql8.0安装

    一.下载 mysql8.0 windows zip包下载地址: https://dev.mysql.com/downloads/mysql/   1540951981(1).png 二.安装 1.解压 ...

  6. 如何卸载oracle11g

    方法/步骤   .关闭oracle所有的服务.可以在windows的服务管理器中关闭:   打开注册表:regedit 打开路径:HKEY_LOCAL_MACHINE\SYSTEM\CurrentCo ...

  7. 初中知识回顾tan,sin,cos关系

    如果K=tan, sin 是X x=k/power(1+k*k,0.5)  开平方 cos是y y=1.0/power(1+k*k,0.5) 开平方 gisoracle总结 ============= ...

  8. mySQL 插入,更新和删除数据

    插入数据: 语法: INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN ); 如 ...

  9. Spring事务管理5-----声明式事务管理(3)

    声明式事务管理  基于注解 在配置文件中需要开启注解驱动<tx:annotation-driven transaction-manager="transactionManager&qu ...

  10. Python字符串逐字符或逐词反转方法

    Python字符串逐字符或逐词反转方法 这篇文章主要介绍了Python字符串逐字符或逐词反转方法,本文对逐字符或逐词分别给出两种方法,需要的朋友可以参考下 目的 把字符串逐字符或逐词反转过来,这个蛮有 ...