题目链接: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]的更多相关文章

  1. 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 ...

  2. 记第一场atcoder和codeforces 2018-2019 ICPC, NEERC, Northern Eurasia Finals Online Mirror

    下午连着两场比赛,爽. 首先是codeforses,我和一位dalao一起打的,结果考炸了,幸亏不计rating.. A Alice the Fan 这个就是记忆化搜索一下预处理,然后直接回答询问好了 ...

  3. [Codeforces 1214D]Treasure Island(dfs)

    [Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...

  4. [Codeforces 163D]Large Refrigerator (DFS+剪枝)

    [Codeforces 163D]Large Refrigerator (DFS+剪枝) 题面 已知一个长方体的体积为V,三边长a,b,c均为正整数,求长方体的最小表面积S V以质因数分解的形式给出 ...

  5. COCI 2018/2019 CONTEST #2 T4 Maja T5Sunčanje Solution

    COCI 2018/2019 CONTEST #2 T4 T5 Solution abstract 花式暴力 #2 T5 Sunčanje 题意 按顺序给你1e5个长方形(左下角坐标&& ...

  6. Codeforces 2018-2019 ICPC, NEERC, Southern Subregional Contest

    2018-2019 ICPC, NEERC, Southern Subregional Contest 闲谈: 被操哥和男神带飞的一场ACM,第一把做了这么多题,荣幸成为7题队,虽然比赛的时候频频出锅 ...

  7. 20172328 2018—2019《Java软件结构与数据结构》第二周学习总结

    20172328 2018-2019<Java软件结构与数据结构>第二周学习总结 概述 Generalization 本周学习了第三章集合概述--栈和第四章链式结构--栈.主要讨论了集合以 ...

  8. 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++ ...

  9. CodeForces 937D 936B Sleepy Game 有向图判环,拆点,DFS

    题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个 ...

随机推荐

  1. [jvm][面试]JVM 调优总结

    https://blog.csdn.net/wfh6732/article/details/57422967 堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-b ...

  2. 机械臂运动学逆解(Analytical solution)

    计算机器人运动学逆解首先要考虑可解性(solvability),即考虑无解.多解等情况.在机器人工作空间外的目标点显然是无解的.对于多解的情况从下面的例子可以看出平面二杆机械臂(两个关节可以360°旋 ...

  3. C#/.NET基础视频[2018年][195集完]

    B站观看地址-无广告观看 https://www.bilibili.com/video/av21896829/ 前一两集 声音有点大 ,可以调大一点音量. 百度网盘下载地址 https://pan.b ...

  4. 关于TF(词频) 和TF-IDF(词频-逆向文件频率 )的理解

    ##TF-IDF TF(词频):  假定存在一份有N个词的文件A,其中‘明星‘这个词出现的次数为T.那么 TF = T/N; 所以表示为: 某一个词在某一个文件中出现的频率. TF-IDF(词频-逆向 ...

  5. H+ 显示并激活menuTab 根据tabName

    //注:在contabs.js文件中 $(function () { }); 方法外 加入//注: data-name="' + menuName + '" 这句是加入的自定义属性 ...

  6. Atitit orm的实现模式 data-mapper模式和active-record模式有什么区别

    Atitit orm的实现模式  data-mapper模式和active-record模式有什么区别 1.1. 这是来自Node.js路线有关混合两种ORM模式Active Record(活动记录模 ...

  7. go 资料

    channel 底层原理 https://www.youtube.com/watch?v=KBZlN0izeiY go concurrency https://www.youtube.com/watc ...

  8. 不得不看,只有专家才知道的17个SQL查询提速秘诀!

    不得不看,只有专家才知道的17个SQL查询提速秘诀! 原创 2018-01-23 布加迪编译 51CTO技术栈 “ 除非你遵循本文介绍的这些技巧,否则很容易编写出减慢查询速度或锁死数据库的数据库代码. ...

  9. vue-router的router.go(n)问题?

    <template> <div> <mt-navbar v-model="selected" class="container" ...

  10. 消息中间件系列三:使用RabbitMq原生Java客户端进行消息通信(消费者(接收方)自动确认模式、消费者(接收方)自行确认模式、生产者(发送方)确认模式)

    准备工作: 1)安装RabbitMQ,参考文章:消息中间件系列二:RabbitMQ入门(基本概念.RabbitMQ的安装和运行) 2.)分别新建名为OriginalRabbitMQProducer和O ...