题目链接: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. java 根据系统日期获取前一天、后一天时间(根据初始日期推算出期望(向前/向后)日期)

      1.情景展示  java 根据系统当前日期获取前一天日期.后一天日期,或者根据初始日期推算出期望(向前/向后)日期. 2.解决方案 导包 import java.text.ParseExcepti ...

  2. 微信小程序--canvas画布实现图片的编辑

    技术:微信小程序   概述 上传图片,编辑图片大小,添加文字,改变文字颜色等 详细 代码下载:http://www.demodashi.com/demo/14789.html 概述 微信小程序--ca ...

  3. MySQL技术内幕读书笔记(五)——索引与算法

    索引与算法 INNODB存储引擎索引概述 ​ INNODB存储引擎支持以下几种常见的索引: B+树索引 全文索引 哈希索引 ​ InnoDB存储引擎支持的哈希索引是自适应的.会根据表的情况自动添加 ​ ...

  4. notepad++ 复制代码--高亮 - 带颜色

    思路来源:http://blog.csdn.net/super828/article/details/72826024 选择代码,然后右键选择菜单命令

  5. SNF快速开发平台2019-APP移动端实际应用效果

    废话不多说,直接上效果图: 这是我们移动端框架基本程序+组件库+标准业务程序,当然了还需要配合上我们的代码生成器则更如虎添翼.https://www.cnblogs.com/spring_wang/p ...

  6. SNF软件开发机器人-子系统-导出-导入功能-多人合作时这个功能经常用到

    导出 导出可以将资源表和子系统导出并形成一个json文件. 1.效果展示: 2.使用说明: 点击导出按钮后会弹出一个导出页面.页面的左侧可以选择功能,右侧可以选择资源表,选择功能的同时右侧中功能所需的 ...

  7. Linux常用指令笔记

    目标:统计当前目录下java文件的个数 指令:`ls -R ./ | grep .java$ | wc -l` 原理:`ls -R ./`列出当前文件夹下的所有FILE,包括目录以及文件;`grep ...

  8. VirtualBox通过Host-Only网络连接方式实现宿主机与虚拟机通信

    适用情况 (1)没有联网, 不插网线 (2)宿主机直接连接宽带(无路由器) 情景: 宿主机 Windows 7 虚拟机 Windows XP 虚拟机安装了SQLServer2005,宿主机想连接使用虚 ...

  9. Java中Comparable和Comparator区别

    很好的一篇博客:http://blog.csdn.net/jq_ak47/article/details/61203817 http://www.cnblogs.com/cmxwt/p/6215253 ...

  10. C#.NET接收JSON数组

    如果要在后台接收类似以下的JSON数据 试了object.Array.Hashtable等类型都收不到,string[]数组的话只收到长度,内容还是空的,最后想到用List<string> ...