Codeforces 1089E - Easy Chess - [DFS+特判][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem E]
题目链接:https://codeforces.com/contest/1089/problem/E
Elma is learning chess figures.
She learned that a rook can move either horizontally or vertically. To enhance her understanding of rook movement Elma’s grandmother gave Elma an 8 × 8 chess board and asked her to find a way to move the rook from a1 to h8 making exactly n moves, so that all visited cells are different.
A visited cell is the initial cell a1 and each cell on which the rook lands after a move.
Input
The input contains a single integer n (2 ≤ n ≤ 63) — the desired number of moves.
Output
Output a space-separated list of n+ 1 visited cells in the order they are visited by the rook.
All cells must be different. The list should start with a1 and end with h8. A solution always exists.
题意:
给你 $8 \times 8$ 的棋盘,有一个棋子车在 $a1$ 位置,现在要经过 $n$ 步移动到达 $h8$ 位置。
要求不能第二次到达曾经到达过的格子,且必须恰好移动 $n$ 步,请给出一种移动方案。
题解:
上来先敲个DFS,万一能过呢?
然后本地跑了一下,发现在 $2 \le n \le 56$ 的范围内跑的飞快,剩下也没几种情况了,干脆特判了之后自己写一个固定走法即可。
AC代码:
#include<bits/stdc++.h>
#define mk(x,y) make_pair(x,y)
using namespace std;
typedef pair<int,int> pii;
int n;
bool vis[][],ok;
vector<pii> ans;
void dfs(int x,int y,int d)
{
if(ok) return;
if(d>n) return;
if(d==n && x== && y==) {ok=;return;}
for(int i=;i<=;i++)
{
if(!vis[i][y])
{
ans.push_back(mk(i,y)); vis[i][y]=;
dfs(i,y,d+);
if(ok) return;
ans.pop_back(); vis[i][y]=;
}
if(!vis[x][i])
{
ans.push_back(mk(x,i)); vis[x][i]=;
dfs(x,i,d+);
if(ok) return;
ans.pop_back(); vis[x][i]=;
}
}
}
int main()
{
while(cin>>n)
{
if(n<=)
{
ans.clear(); memset(vis,,sizeof(vis));
ans.push_back(mk(,)); vis[][]=;
ok=; dfs(,,);
for(int i=;i<ans.size();i++)
{
if(i>) printf(" ");
printf("%c%d",'a'+ans[i].first-,ans[i].second);
}
printf("\n");
}
else
{
for(int r=;r<=;r++)
{
if(r%==) {
for(int c=;c>=;c--) printf("%c%d ",'a'+c-,r);
} else {
for(int c=;c<=;c++) printf("%c%d ",'a'+c-,r);
}
}
printf("a7 a8 b8 b7 c7 c8 d8 d7 ");
switch(n)
{
case : printf("h7 h8\n"); break;
case : printf("e7 h7 h8\n"); break;
case : printf("e7 f7 h7 h8\n"); break;
case : printf("e7 f7 g7 h7 h8\n"); break;
case : printf("e7 e8 f8 f7 h7 h8\n"); break;
case : printf("e7 e8 f8 f7 g7 h7 h8\n"); break;
case : printf("e7 e8 f8 f7 h7 g7 g8 h8\n"); break;
}
}
}
}
Codeforces 1089E - Easy Chess - [DFS+特判][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem E]的更多相关文章
- Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]
题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...
- 记第一场atcoder和codeforces 2018-2019 ICPC, NEERC, Northern Eurasia Finals Online Mirror
下午连着两场比赛,爽. 首先是codeforses,我和一位dalao一起打的,结果考炸了,幸亏不计rating.. A Alice the Fan 这个就是记忆化搜索一下预处理,然后直接回答询问好了 ...
- [Codeforces 1214D]Treasure Island(dfs)
[Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...
- [Codeforces 163D]Large Refrigerator (DFS+剪枝)
[Codeforces 163D]Large Refrigerator (DFS+剪枝) 题面 已知一个长方体的体积为V,三边长a,b,c均为正整数,求长方体的最小表面积S V以质因数分解的形式给出 ...
- COCI 2018/2019 CONTEST #2 T4 Maja T5Sunčanje Solution
COCI 2018/2019 CONTEST #2 T4 T5 Solution abstract 花式暴力 #2 T5 Sunčanje 题意 按顺序给你1e5个长方形(左下角坐标&& ...
- Codeforces 2018-2019 ICPC, NEERC, Southern Subregional Contest
2018-2019 ICPC, NEERC, Southern Subregional Contest 闲谈: 被操哥和男神带飞的一场ACM,第一把做了这么多题,荣幸成为7题队,虽然比赛的时候频频出锅 ...
- 20172328 2018—2019《Java软件结构与数据结构》第二周学习总结
20172328 2018-2019<Java软件结构与数据结构>第二周学习总结 概述 Generalization 本周学习了第三章集合概述--栈和第四章链式结构--栈.主要讨论了集合以 ...
- Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)
Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) #include <bits/stdc++ ...
- CodeForces 937D 936B Sleepy Game 有向图判环,拆点,DFS
题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个 ...
随机推荐
- html5利用websocket完成的推送功能(tomcat)
html5利用websocket完成的推送功能(tomcat) 利用websocket和java完成的消息推送功能,服务器用的是tomcat7.0.42,一些东西是自己琢磨的,也不知道恰不恰当,不恰当 ...
- (七):处理MFC
(一):简单介绍 为了可以在一个Winelib应用中使用MFC,你须要首先使用Winelib又一次编译MFC. 在理论上,你应该为Windows的MFC编写一个封装(怎样编写在后面介绍).可是,在实践 ...
- Android Api 27 在 Android 8.0 上出现 Only fullscreen opaque activities can request orientation 的解决情况
刚上班,没有业务开发,对 App 的 Api 由 26 升级到了 27, 结果在 Android 8.0 的设备上会出现 crash . Log 如下: java java.lang.IllegalS ...
- [druid]大数据挑战——如何使用Druid实现数据聚合
-- 知道你为什么惧组件很多的一些开源软件? 因为缺乏阅读能力. 最近我接手了druid+kafka+elk一套等日志系统. 但是我对druid很陌生, 周旋了几天, 官网文档快速开始照着做了下. 看 ...
- [20180312]进程管理其中的SQL Server进程占用内存远远大于SQL server内部统计出来的内存
sql server 统计出来的内存,不管是这个,还是dbcc memorystatus,和进程管理器中内存差距很大,差不多有70G的差异. 具体原因不止,可能是内存泄漏,目前只能通过重启服务解决 ...
- Git入门到高级系列2-git高级操作
视频课程地址 腾讯课堂 git 清理 git clean命令用来从你的工作目录中删除所有没有tracked过的文件. 命令 说明 git clean -n 告诉你哪些文件会被删除. 记住他不会真正的删 ...
- sqoop 测试 --hive-delims-replacement 参数
在hive的官方文档中给的例子中字段分隔符是\001,但是在他的API文档中--hive-delims-replacement ,--hive-drop-import-delims 参数中会处理的 ...
- VM虚拟机下安装CentOS_6.5_x64
在开始Linux的学习之前,我们必须要有一个实验环境.这里我选择了安装CentOS-6.5-x86_64的Minimal版本,大家也可以选择安装其他的Linux发行版.选择CentOS主要还是因为它比 ...
- 【ML入门系列】(二)分类与回归
前言 在机器学习中,“分类”和“回归”这两个词经常听说,但很多时候我们却混为一谈.本文主要从应用场景.训练算法等几个方面来叙述两者的区别. 本质区别 分类和回归的区别在于输出变量的类型.分类的输出是离 ...
- Python3 File
open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError. 注意:使用 open() ...