CF3A 【Shortest path of the king】
一句话题意:
在8 * 8的棋盘上,输出用最少步数从起点走到终点的方案
数据很小,可以广搜无脑解决
定义数据结构体
struct pos{
int x,y,s; //x、y表示横纵坐标,s表示步数
string move[]; //存储每一步的方案
};
移动时新旧状态传递
pos u=q.front();
q.pop();
for(int i=;i<;i++)
{
pos th;
th.x=u.x+dx[i];
th.y=u.y+dy[i];
th.s=u.s+;
for(int i=;i<=u.s;i++)
th.move[i]=u.move[i];
th.move[th.s]=st[i];
}
判断是否可以拓展
if(th.x<||th.x>||th.y<||th.y>||vis[th.x][th.y])
//越界或已访问
continue;
打标记,入队
vis[th.x][th.y]=; //标记为已访问
q.push(th);
完整代码
#include<iostream>
#include<queue>
using namespace std;
struct pos{
int x,y,s;
string move[];
};
queue<pos>q;
int dx[]={-,,,,-,-,,};
int dy[]={,,,-,,-,,-};
string st[]={"L","R","U","D","LU","LD","RU","RD"};
bool vis[][];
int x,y;
int main()
{
string s1,s2;
cin>>s1>>s2;
q.push((pos){s1[]-'a'+,s1[]-'',,""});
vis[s1[]-'a'+][s1[]-'']=;
x=s2[]-'a'+,y=s2[]-'';
if(vis[x][y])
{
cout<<<<endl;
return ;
}
while(!q.empty())
{
pos u=q.front();
q.pop();
for(int i=;i<;i++)
{
pos th;
th.x=u.x+dx[i];
th.y=u.y+dy[i];
th.s=u.s+;
for(int i=;i<=u.s;i++)
th.move[i]=u.move[i];
th.move[th.s]=st[i];
if(th.x<||th.x>||th.y<||th.y>||vis[th.x][th.y])
continue;
vis[th.x][th.y]=;
if(th.x==x&&th.y==y)
{
cout<<th.s<<endl;
for(int j=;j<=th.s;j++)
cout<<th.move[j]<<endl;
return ;
}
q.push(th);
}
}
return ;
}
CF3A 【Shortest path of the king】的更多相关文章
- Codeforces-A. Shortest path of the king(简单bfs记录路径)
A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...
- Codeforces Beta Round #3 A. Shortest path of the king 水题
A. Shortest path of the king 题目连接: http://www.codeforces.com/contest/3/problem/A Description The kin ...
- CF3A Shortest path of the king
The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, becaus ...
- 题解 CF938G 【Shortest Path Queries】
题目让我们维护一个连通无向图,边有边权,支持加边删边和询问从\(x\)到\(y\)的异或最短路. 考虑到有删边这样的撤销操作,那么用线段树分治来实现,用线段树来维护询问的时间轴. 将每一条边的出现时间 ...
- Shortest path of the king
必须要抄袭一下这个代码 The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose h ...
- A - Shortest path of the king (棋盘)
The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, becaus ...
- node搜索codeforces 3A - Shortest path of the king
发一下牢骚和主题无关: 搜索,最短路都可以 每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...
- 3A. Shortest path of the king
给你一个的棋盘, 问:从一个坐标到达另一个坐标需要多少步? 每次移动可以是八个方向. #include <iostream> #include <cmath> #inclu ...
- Codeforces Beta Round #3 A. Shortest path of the king
标题效果: 鉴于国际棋盘两点,寻求同意的操作,是什么操作的最小数量,在操作过程中输出. 解题思路: 水题一个,见代码. 以下是代码: #include <set> #include < ...
随机推荐
- 2018.9.22 Bubble Cup 11-Finals(Online Mirror,Div.2)
感受了一下ACM的感觉,然后被神题和神犇们暴踩了 夭寿啦,机房大佬非法组队啊 比赛前i207M插的“怕不是不到九点就要弃疗”的flag成功生效 一开始先扫了一遍题,我一开始以为A题是个扫一遍的题,然后 ...
- CentOS-5的yum源无法使用问题
CentOS-5的yum源无法使用问题 [root@37wan ~]# yum -y install gcc Loaded plugins: fastestmirror Determining fas ...
- 团体程序设计天梯赛L3-019 代码排版(23分)
打算学完编译原理后再次实现它... 以下为比较“杂乱”的方法: 海量数据: https://pan.baidu.com/s/1Prd0ZqNLoCLLvXyJjCef3w 如果大家有发现这个程序的问题 ...
- php 通过链接生成二维码,扫码支付用到
$(".good_info").on('click',function () { var id = $(this).data('id'); var string='http://q ...
- Guava之CaseFormat
com.google.common.base.CaseFormat是一种实用工具类,以提供不同的ASCII字符格式之间的转换. 其对应的枚举常量 从以上枚举中可以看出,java程序员最常用的转换类型为 ...
- pyqt5的代码
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- P1022 计算器的改良
P1022 计算器的改良 题目背景 NCL 是一家专门从事计算器改良与升级的实验室,最近该实验室收到了某公司所委托的一个任务:需要在该公司某型号的计算器上加上解一元一次方程的功能.实验室将这个任务交给 ...
- 20155236 2016-2017-2 《Java程序设计》第四周学习总结
20155236 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 1.继承基本上就是避免多个类间重复定义共同行为. 继承的三个好处:减少代码冗余:维护变得简单 ...
- 《C语言程序设计基础I》秋季学习总结
希望下学期比这学期轻松,学习能力上升,只是越发丰富. 一步一步的走踏实了
- Mother's Mil 母亲的牛奶
Description 农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另一个桶中,直到被灌桶装 ...