一句话题意:
在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】的更多相关文章

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

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

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

  4. 题解 CF938G 【Shortest Path Queries】

    题目让我们维护一个连通无向图,边有边权,支持加边删边和询问从\(x\)到\(y\)的异或最短路. 考虑到有删边这样的撤销操作,那么用线段树分治来实现,用线段树来维护询问的时间轴. 将每一条边的出现时间 ...

  5. Shortest path of the king

    必须要抄袭一下这个代码 The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose h ...

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

  7. node搜索codeforces 3A - Shortest path of the king

    发一下牢骚和主题无关: 搜索,最短路都可以     每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...

  8. 3A. Shortest path of the king

    给你一个的棋盘, 问:从一个坐标到达另一个坐标需要多少步? 每次移动可以是八个方向.   #include <iostream> #include <cmath> #inclu ...

  9. Codeforces Beta Round #3 A. Shortest path of the king

    标题效果: 鉴于国际棋盘两点,寻求同意的操作,是什么操作的最小数量,在操作过程中输出. 解题思路: 水题一个,见代码. 以下是代码: #include <set> #include < ...

随机推荐

  1. EOJ 262 润清的烦恼

    ——题目出处zhoutb2333 题解: 3e6可以带一个log 又是下取整问题.但是分块会TLE. 这样考虑,我们把式子拆成两个部分. 我们先算出来每一个x的[ai/x]项,再算出来[x/ai]项. ...

  2. bug3 乱码问题

    出现乱码问题是因为各软件之间的编码方式不同导致 1.tomcat修改编码方法: 2.myeclipse中修改编码方式的方法: window----preference ----general----- ...

  3. bashttpd使用手册

    http://note.youdao.com/noteshare?id=15775dca9fcdc7326e80158082572ed5

  4. Java基础-包(package)的声明与访问

    Java基础-包(package)的声明与访问 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.包的概念 Java中的包,其实就是我们电脑系统中的文件夹,包里存放的是程序员生成的 ...

  5. Hibernate基础知识详解

    一.Hibernate框架        Hibernate是一个开放源代码的对象关系映射框架,它对 JDBC进行了非常轻量级的对象封装,它将POJO类与数据库表建立映射关系,是一个    全自动的O ...

  6. CAAnimation保持动画结束时的效果

    配置动画时,加上一下两句 animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards;

  7. go build 不同系统下的可执行文件

    Golang 支持在一个平台下生成另一个平台可执行程序的交叉编译功能. 1.Mac下编译Linux, Windows平台的64位可执行程序: 1 2 $ CGO_ENABLED=0 GOOS=linu ...

  8. jvm内存模型(运行时数据区)

    运行时数据区(runtime data area) jvm定义了几个运行时数据区,这些运行时数据区存储的数据,供开发者的应用或者jvm本身使用.按线程共享与否可以分为线程间共享和线程间独立. 线程间独 ...

  9. 最长递增子序列(LIS)(转)

    最长递增子序列(LIS)   本博文转自作者:Yx.Ac   文章来源:勇幸|Thinking (http://www.ahathinking.com)   --- 最长递增子序列又叫做最长上升子序列 ...

  10. cmmusic:小巧而实用的mplayer音乐播放前端

    Author: Jiqing (jiqingwu@gmail.com) home: http://hi.baidu.com/jiqing0925 create: 2011-03-10 update: ...