题目链接:http://codeforces.com/contest/811/problem/D

题意:现在给你一个n*m大小的图,你输出一个方向之后,系统反馈给你一个坐标,表示走完这步之后到的位子,我们需要在2*n*m步之内走到终点,问怎样走才行(多解输出任意一个即可)。我们一开始的位子是(1,1),终点位子是“F”,‘*’表示不能走的位子,游戏开始的时候,有一些小伙伴比较调皮,会将U和D互换,就是说假设我们操作了U,但是实际是走到了D.或者也可能将L和R互换,当然也可能都没有互换过,当然也可能都互换过。然你模拟整个过程。

题解:其实很简单,先确定路线,然后在模拟,方向看他反馈的来确定有没有变,然后就是简单的模拟一下。

#include <iostream>
#include <queue>
#include <stack>
#include <cstring>
using namespace std;
char mmp[110][110];
struct TnT {
int x , y;
}pre[110][110];
int dr[4][2] = {1 , 0 , -1 , 0 , 0 , 1 , 0 , -1} , n , m;
bool vis[110][110];
TnT bfs(TnT sta , TnT ed) {
memset(vis , false , sizeof(vis));
queue<TnT>q;
q.push(sta);
vis[sta.x][sta.y] = true;
while(!q.empty()) {
TnT gg = q.front();
q.pop();
if(gg.x == ed.x && gg.y == ed.y) {
return gg;
}
for(int i = 0 ; i < 4 ; i++) {
TnT gl = gg;
gl.x += dr[i][0];
gl.y += dr[i][1];
if(gl.x >= 0 && gl.x < n && gl.y >= 0 && gl.y < m && mmp[gl.x][gl.y] != '*' && !vis[gl.x][gl.y]) {
vis[gl.x][gl.y] = true;
pre[gl.x][gl.y].x = gg.x;
pre[gl.x][gl.y].y = gg.y;
q.push(gl);
}
}
}
return ed;
}
int main() {
cin >> n >> m;
TnT sta , ed;
for(int i = 0 ; i < n ; i++) {
cin >> mmp[i];
for(int j = 0 ; j < m ; j++) {
if(mmp[i][j] == 'F') {ed.x = i , ed.y = j;}
}
}
sta.x = 0 , sta.y = 0;
TnT gg = bfs(sta , ed);
stack<TnT>gl;
while(gg.x != 0 || gg.y != 0) {
gl.push(gg);
int x = gg.x , y = gg.y;
gg.x = pre[x][y].x;
gg.y = pre[x][y].y;
}
cout << endl;
TnT pos = sta;
char ff[4] = {'L' , 'U' , 'D' , 'R'};
bool flag[2];
flag[0] = flag[1] = false;
while(!gl.empty()) {
TnT gb = gl.top();
if(gb.y < pos.y) {
cout << ff[0] << endl;
int x , y;
cin >> x >> y;
x--,y--;
if(x == gb.x && y == gb.y) {gl.pop() , pos = gb; continue;}
else {
if(!flag[0]) {
char cp = ff[0];
ff[0] = ff[3];
ff[3] = cp;
flag[0] = true;
}
}
}
if(gb.y > pos.y) {
cout << ff[3] << endl;
int x , y;
cin >> x >> y;
x--,y--;
if(x == gb.x && y == gb.y) {gl.pop() , pos = gb; continue;}
else {
if(!flag[0]) {
char cp = ff[3];
ff[3] = ff[0];
ff[0] = cp;
flag[0] = true;
}
}
}
if(gb.x < pos.x) {
cout << ff[1] << endl;
int x , y;
cin >> x >> y;
x--,y--;
if(x == gb.x && y == gb.y) {gl.pop() , pos = gb; continue;}
else {
if(!flag[1]) {
char cp = ff[1];
ff[1] = ff[2];
ff[2] = cp;
flag[1] = true;
}
}
}
if(gb.x > pos.x) {
cout << ff[2] << endl;
int x , y;
cin >> x >> y;
x--,y--;
if(x == gb.x && y == gb.y) {gl.pop() , pos = gb; continue;}
else {
if(!flag[1]) {
char cp = ff[1];
ff[1] = ff[2];
ff[2] = cp;
flag[1] = true;
}
}
}
}
return 0;
}

codeforces 811 D. Vladik and Favorite Game(bfs水题)的更多相关文章

  1. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  2. hdu1240 bfs 水题

    原题链接 思路:水题,直接搜 #include "map" #include "queue" #include "math.h" #incl ...

  3. Codeforces Round #334 (Div. 2) A. Uncowed Forces 水题

    A. Uncowed Forces Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/pro ...

  4. Codeforces Round #115 B. Plane of Tanks: Pro 水题

    B. Plane of Tanks: Pro Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/17 ...

  5. Codeforces Beta Round #14 (Div. 2) A. Letter 水题

    A. Letter 题目连接: http://www.codeforces.com/contest/14/problem/A Description A boy Bob likes to draw. ...

  6. codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]

    就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...

  7. Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...

  8. codeforces 811 E. Vladik and Entertaining Flags(线段树+并查集)

    题目链接:http://codeforces.com/contest/811/problem/E 题意:给定一个行数为10 列数10w的矩阵,每个方块是一个整数, 给定l和r 求范围内的联通块数量 所 ...

  9. codeforces 811 C. Vladik and Memorable Trip(dp)

    题目链接:http://codeforces.com/contest/811/problem/C 题意:给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都要出现在这个区间. 每个区间 ...

随机推荐

  1. mule发布调用webservice

    mule发布webservice 使用mule esb消息总线发布和调用webservice都非常精简,mule包装了所有操作,你只需要拖控件配置就可以,下面讲解mule发布: 1.下面是flow,h ...

  2. kylin Retrieving hive dependency...

    由于公司环境配置hive默认连接hiveserver2 ,不管hive cli 还是beeline cli都默认使用beeline cli,连接hive需要输入账号密码; 启动kylin 时会Retr ...

  3. 【译】尝试使用Nullable Reference Types

    随着.NET Core 3.0 Preview 7的发布,C#8.0已被认为是“功能完整”的.这意味着它们的最大亮点Nullable Reference Types,在行为方面也被锁定在.NET Co ...

  4. A solution to the never shortened to-do list

    I once told my younger sister my learning system, and the basic five doctrines of my methodology. Bu ...

  5. 3、数组的声明及初始化(test1.java)

    今天学习了,一位数组和二维数组,先学习了数组的申请,数组的初始化,数组的拷贝等.对于数组我认为,和C\C++中的数组,没有什么太大的区别,但是在JAVA中,大家都知道JAVA是面向对象的编程语言,每一 ...

  6. 史上最全面的SignalR系列教程-2、SignalR 实现推送功能-永久连接类实现方式

    1.概述 通过上篇史上最全面的SignalR系列教程-1.认识SignalR文章的介绍,我们对SignalR技术已经有了一个全面的了解.本篇开始就通过SignalR的典型应用的实现方式做介绍,例子虽然 ...

  7. 使用PowerShell 测试DNS

    运行环境:Windows Server 2012 R2 获取服务器DNS命令,下面的仅获取一个dns (nslookup sql.ciras.com)[1].split(':')[1].trim() ...

  8. Apache性能测试工具ab使用详解~转载

    Apache自带性能测试工具ab使用详解 一. Apache的下载 1. http://www.apache.org/,进入Apache的官网 2. 将页面拖到最下方“Apache Project L ...

  9. python使用zabbix的API接口

    一.实验环境 python3.6.6 zabbix 3.0.9 二.实验目的 了解Zabbix的API接口格式 通过python实现登陆zabbix服务,获得登陆token 通过python检索zab ...

  10. SpringBoot与Shiro整合权限管理实战

    SpringBoot与Shiro整合权限管理实战 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] *观看本文章需要有一定SpringBoot整合经验* Shiro框架简介 Apach ...