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 < ...
随机推荐
- npm publish gives “unscoped packages cannot be private”
解决方法: npm publish --access public 详细参考此处
- Apache+tomcat配置动静分离(一个apache一个tomcat,没有做集群)
1. 下载apache http server,tomcat,mok_jk.so apache下载地址:http://httpd.apache.org/download.cgi tomcat下载地址: ...
- 获取异常信息e.printStackTrace()的内容
获取异常信息e.printStackTrace()的内容 最近做项目的时候需要记录操作的日志,但是记录异常信息的是发现使用e.getMessage()根本无法满足需要,并且e.getMessage() ...
- 转:Block原理及引用循环问题
2010年WWDC发布iOS4时Apple对Objective-C进行了一次重要的升级:支持Block.说到底这东西就是闭包,其他高级语音例如Java和C++已有支持,第一次使用Block感觉满简单好 ...
- 用canvas绘制验证码
在通常的登录界面我们都可以看到验证码,验证码的作用是检测是不是人在操作,防止机器等非人操作,防止数据库被轻而易举的攻破. 验证码一般用PHP和java等后端语言编写: 但是在前端,用canva或者SV ...
- python字符中加入变量
有时候,我们需要在字符串中加入相应的变量,以下提供了几种字符串加入变量的方法: 1.+ 连字符 name = 'zhangsan' print('my name is '+name) #结果为 my ...
- jmeter乱码问题
JMeter 的版本由 2.13 升级到了 3.0 发现之前接口脚本 POST 请求主体中的中文无法正确显示,现象如下图所示:
- pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode"
pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode" 经查询, 看到 ...
- Hbase建模选择
日期 2017年3月17日 HBase建模记录 OLTP 应用场景: OLAP 应用场景: 语音分析系统的应用场景 基于HBase的建模考虑 1.话单为主来考虑hbase的rowkey的生成规则: 1 ...
- jdk1.8.0_45源码解读——LinkedList的实现
jdk1.8.0_45源码解读——LinkedList的实现 一.LinkedList概述 LinkedList是List和Deque接口的双向链表的实现.实现了所有可选列表操作,并允许包括null值 ...