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 < ...
随机推荐
- 洛谷大宁的邀请赛~元旦祭F: U17264 photo(线段树)
标程的写法稍微有点麻烦,其实不需要平衡树也是可以做的. 线段树上维护从左端点开始最远的有拍照的长度,以及区间的最大值. 考虑两段区间合并的时候,显然左区间必须取,右区间的第一个比左区间最大值大的数开始 ...
- 丁酉年六月十一ACM模拟赛
似乎该写题解了.今天模拟ACM,10道题(本来还有2道被删了),9道都来自BZOJ,中间我做过2道.那么说,今天Solv.便大大增多了(但还是不如强大的Amphetamine). 题单及一句话题解如下 ...
- python调用powershell、远程执行bat
python调用本地powershell方法 1.现在准备一个简陋的powershell脚本,功能是测试一个IP列表哪些可以ping通: function test_ping($iplist) { f ...
- ACM比赛_注意
ACM比赛_注意: 比赛前: 1.前一天早一点睡觉 2.避免参加激烈的活动,以免比赛时精力不足; 3.少喝水,并提前上厕所; 4.把账号,密码都准备好,放在txt中 5.提前创建多个程序(etc.10 ...
- ubuntu下访问其他盘出现挂在错误解决办法
Error mounting /dev/sda5 at /media Linux下不能进入windows的NTFS分区之挂载错误问题 电 脑安装了win8,后在另一个分区(在win8下未分配空间)安装 ...
- Kubernetes PV/PVC使用实践
转载于https://www.cnblogs.com/ericnie/p/7733281.html pv,pvc的概念不解释了,之前在registry中已经使用过PV和PVC,现在想把WebLog ...
- jquery.lazyload插件实现图片延迟加载详解
什么是LazyLoad技术? 在页面上图片比较多的时候,打开一张页面必然引起与服务器大数据量的交互.尤其是对于高清晰的图片,占了几百K的空间.Lazy Load 是一个用 JavaScript 编写的 ...
- grep与正则表达式详解和实例
转载自:http://www.jb51.net/article/31207.htm grep 工具,以前介绍过. grep -[acinv] '搜索内容串' filename -a 以文本文件方式搜索 ...
- NGINX配置PHP解析
<?php phpinfo(); ?> location ~ \.php$ { root html; fastcgi_pass ; fastcgi_index index.php; fas ...
- python学习笔记2--list
一.list操作 list=['xiaohei','xiaobai','xiaohong']#定义一个数组 name=[]#定义一个空数组 #查 #下标是从0开始 print(list[1]) #-1 ...