codeforces 811 D. Vladik and Favorite Game(bfs水题)
题目链接: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水题)的更多相关文章
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- hdu1240 bfs 水题
原题链接 思路:水题,直接搜 #include "map" #include "queue" #include "math.h" #incl ...
- 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 ...
- 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 ...
- 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. ...
- codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]
就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...
- Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...
- codeforces 811 E. Vladik and Entertaining Flags(线段树+并查集)
题目链接:http://codeforces.com/contest/811/problem/E 题意:给定一个行数为10 列数10w的矩阵,每个方块是一个整数, 给定l和r 求范围内的联通块数量 所 ...
- codeforces 811 C. Vladik and Memorable Trip(dp)
题目链接:http://codeforces.com/contest/811/problem/C 题意:给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都要出现在这个区间. 每个区间 ...
随机推荐
- Dubbo里面线程池的拒绝策略
Dubbo里面线程池的拒绝策略 public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy { protecte ...
- Thinkphp5.0快速入门笔记(2)
学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. 示例建立新的模块和控制器 在a ...
- 禅道、jenkins部署记录
禅道部署1.检查你linux系统的位数(uname -a)2.下载对应位数的禅道包3.通过xftp工具将禅道包拷贝到虚拟机的/opt目录4.tar 对禅道包进行解压5.改配置:vi /opt/zbox ...
- 【Java例题】7.3 线程题3-素数线程
3.素数线程.设计一个线程子类,依次随机产生10000个随机整数(100-999):再设计另一个线程子类,依次对每一个随机整数判断是不是素数,是则显示:然后编写主类,在主函数中定义这两个线程类的线程对 ...
- 学习Qt的一点小感想
作为一名电子信息工程的学生,嵌入式似乎是不二的选择,然后我便学习了一下在嵌入式广泛应用的QT软件,刚开始就是学学控件,觉得还是简单,也觉得比较新颖,可是到了做一些具体的小东西就会发现学的东西远远不够, ...
- 算法与数据结构基础 - 二叉树(Binary Tree)
二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...
- windwos环境下安装python2和python3
一 python安装 下载地址: https://www.python.org/downloads/ 环境变量:Path中添加C:\Python27\Scripts\;C:\Python27\; C: ...
- java之异常详解
一.什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错.在java中,阻止当前方法或作用域正常运行的情况,称之为异常. 二.异常体系 Java把异常当作对象来处理,并定义一个基类java. ...
- Unable to load template file 'rj\ThinkPHP/Tpl/dispatch_jump.tpl'----thinkphp3.2.3
Unable to load template file 'rj\ThinkPHP/Tpl/dispatch_jump.tpl'----thinkphp3.2.3 1.报错原因:将thinkphp默认 ...
- linux+jenkins+python+svn 自动化测试集成之路
本文背景: 背景1---个人基础: 本机win7上安装pycharm,使用python搭建API自动化测试框架,本地运行Pass.本机上搭建jenkins,创建测试任务,定时构建Pass. 背景2-- ...