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个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都要出现在这个区间. 每个区间 ...
随机推荐
- cmd与monkey测试
monkey测试的相关命令 monkey是模拟用户触摸操作,不支持条件判断.monkey命令格式: 启动安卓模拟器/真机 点击运行->输入cmd->进入命令行界面 查看设备连接情况,ad ...
- linux字符设备驱动中内核如何调用驱动入口函数 一点记录
/* 内核如何调用驱动入口函数 ? *//* 答: 使用module_init()函数,module_init()函数定义一个结构体,这个结构体里面有一个函数指针,指向first_drv_init() ...
- oracle-11g2下载安装笔记
一.下载链接地址 http://download.oracle.com/otn/nt/oracle11g/112010/win64_11gR2_database_1of2.zip http://dow ...
- [Abp vNext 源码分析] - 7. 权限与验证
一.简要说明 在上篇文章里面,我们在 ApplicationService 当中看到了权限检测代码,通过注入 IAuthorizationService 就可以实现权限检测.不过跳转到源码才发现,这个 ...
- 把Jar包加入windows系统服务
之前在服务器上不一个Java服务时候,总是开着一堆黑框框,非常不雅,重点是极其容易误关,所以把可执行Jar文件加入Windows系统服务,看起来是个非常不错的选择!(实际上也确实是非常不错的选择) ! ...
- WEB基础(二)--servlet的生命周期
Servlet的生命周期一般可以用三个方法来表示: init():仅执行一次,负责在装载Servlet时初始化Servlet对象 service() :核心方法,一般HttpServlet中会有get ...
- redhat linux 5.3安装activeMQ
安装环境:linux redhat enterprise 5.3 activemq版本:5.9.01.从http://activemq.apache.org/download.html地址下载apac ...
- Streaming+Sparksql使用sql实时分析 rabbitmq+mongodb+hive
SparkConf sparkConf = new SparkConf()//此处使用一个链接切记使用一个链接否则汇报有多个sparkcontext错误 .setAppName("Spark ...
- SQL中一些实用的快捷键
Ctrl+A全选 快速选中一行: 若光标在这条语句末尾用Shift+Home 若光标在这条语句开头用Shift+End Ctrl+K+U快捷注释本行 Ctrl+K+C反注释 Ctrl+R 关闭下面的 ...
- HashMap这些问题你知道吗?
HashMap是Java面试中的常考点之一,而且其<Key,Value>结构也是开发中常常用到的结构之一.或许你使用过HashMap,但是你知道下面这些问题吗? HashMap的底层结构是 ...