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个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都要出现在这个区间. 每个区间 ...
随机推荐
- oracle的JDBC连接
package com.xian.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pr ...
- spring学习笔记之---bean管理
bean管理(xml) (一)spring的工厂类 FileSystemXmlApplicationContext 读取磁盘配置文件 (二)bean实例化的三种方式 (1)使用类构造器实例化(默认无参 ...
- AWS Aurora数据库 Multi-Master 小测
AWS Aurora Mysql终于推出了Multi-Master,直面硬刚Oracle RAC.在多一份数据库产品选择的小兴奋之余,我们也看看新推出的Multi-Master的特点(包括优缺点). ...
- 基于hprose-golang创建RPC微服务
Hprose(High Performance Remote Object Service Engine) 是一款先进的轻量级.跨语言.跨平台.无侵入式.高性能动态远程对象调用引擎库.它不仅简单易用, ...
- Linux系统与程序监控工具atop教程
引言 Linux以其稳定性,越来越多地被用作服务器的操作系统(当然,有人会较真地说一句:Linux只是操作系统内核:).但使用了Linux作为底层的操作系统,是否我们就能保证我们的服务做到7*24地稳 ...
- 精通Android4.0开发视频【张泽华】-完整版下载
观看须知: 本视频教程为黑马程序员 张泽华老师历经2年时间整理 适合有JavaWeb基础同学学习,教程采用的AVI方式发布,所以看起来很流畅. 视频概括: 1. 本套视频不同于市面上任何一套andro ...
- bootstrap实战练习中涉及的知识点(很有用哦!)
看的有关视频做的笔记,对bootstrap中涉及的知识点做了一定的解析,很有用哦!(新手上路,有不合适的地方可以指出哦!) 下面进入正题: Bootstrap是当下最流行的前端框架(界面工具集) 特点 ...
- Markdown的最常用标记符号
Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. md就是markdown 如果你要把这段文字定义成标题,只需要在前面加上一个#号, ...
- AutoCAD二次开发(.Net)之获取LSP变量的值
//https://blog.csdn.net/qq_21489689/article/details/78973787 [System.Security.SuppressUnmanagedCodeS ...
- SpringDataJpa在一对多、多对多关系映射时出现StackOverflowError
在使用spring-data-jpa时,进行一对多配置后,在调用save方法时,出现内存溢出. 产生原因一:为了方便看信息,在两类中分别重写了 toString 方法,导致查询加载时两类在互相调用对方 ...