Codeforces 811D Vladik and Favorite Game[模拟][bfs]
来源:互联网 发布:sql中出现次数最多的 编辑:程序博客网 时间:2024/06/06 18:00
题意:说有四个按钮 L,R可能是反的,U,D可能是反的,然后对于你每次输出的(L,R,U,D),会返回一个坐标,到达给定的F 的位置停止。
分析:关键就是判断L,R 是否是反的,U,D 是否是反的,刚开始的位置是(1,1),贴墙的,为了防止走到 * 上,我们可以找一个右边是空的位置,然后输出一个L,如果在原地,那么说明 不反,如果到了右边,说明反了,对于U,D 也同样,知道反没反之后就是当前位置到目标位置的bfs了,将路径存下来,按照是否相反进行输出。
以下是代码:
char mp[105][105];bool vis[105][105];pii pre[105][105];int n, m, x, y;int tomove[4][2] = { { 0,-1 },{ 0,1 },{ -1,0 },{ 1,0 } };bool judge(int x, int y) {if (x<1 || x>n || y<1 || y>m || vis[x][y] || mp[x][y] == '*')return false;else return true;}void bfs(int posx, int posy) {queue<pii>qe;qe.push(pii(posx, posy));vis[posx][posy] = 1;while (!qe.empty()){pii now = qe.front(); qe.pop();if (now.first == x && now.second == y) break;for (int i = 0; i < 4; ++i) {int fx = now.first + tomove[i][0], fy = now.second + tomove[i][1];if (judge(fx, fy)) {pre[fx][fy] = now;vis[fx][fy] = 1;qe.push(pii(fx, fy));}}}}int main() {memset(vis, 0, sizeof(vis));bool f1 = 0, f2 = 0;scanf("%d%d", &n, &m);for (int i = 1; i <= n; ++i) {getchar();for (int j = 1; j <= m; ++j) {scanf("%c", &mp[i][j]);if (mp[i][j] == 'F')x = i, y = j;}}if (x == 1 && y == 1) {return 0;}int px = 1, py = 1, a, b;if (mp[px][py + 1] == '.') {printf("L\n");cin >> a >> b;if (mp[a][b] == 'F')return 0;if (a == px && b == py)f1 = 0;else f1 = 1;if (f1) {printf("R\n");cin >> a >> b;}px = a, py = b;if (mp[px][py] == 'F')return 0;while (mp[px + 1][py] == '*') {if (f1) {printf("L\n");cin >> a >> b;px = a, py = b;}else {printf("R\n");cin >> a >> b;px = a, py = b;}if (mp[px][py] == 'F')return 0;}printf("U\n");cin >> a >> b;if (a == px && b == py)f2 = 0;else f2 = 1;if (mp[px][py] == 'F')return 0;if (f2) {printf("D\n");cin >> a >> b;px = a, py = b;}}else{printf("U\n");cin >> a >> b;if (mp[a][b] == 'F')return 0;if (a == px && b == py)f2 = 0;else f2 = 1;if (f2) {printf("D\n");cin >> a >> b;}px = a, py = b;if (mp[px][py] == 'F')return 0;while (mp[px][py + 1] == '*') {if (f2) {printf("U\n");cin >> a >> b;px = a, py = b;}else {printf("D\n");cin >> a >> b;px = a, py = b;}if (mp[px][py] == 'F')return 0;}printf("L\n");cin >> a >> b;if (a == px && b == py)f1 = 0;else f1 = 1;if (mp[px][py] == 'F')return 0;if (f1) {printf("R\n");cin >> a >> b;px = a, py = b;}}bfs(px, py);int nowx = x, nowy = y;stack<char>st;while (nowx != px || nowy != py) {for (int i = 0; i < 4; ++i) {if (pre[nowx][nowy].first + tomove[i][0] == nowx&& pre[nowx][nowy].second + tomove[i][1] == nowy) {int tmpx = pre[nowx][nowy].first, tmpy = pre[nowx][nowy].second;nowx = tmpx, nowy = tmpy;if (i == 0)st.push('L');else if (i == 1)st.push('R');else if (i == 2)st.push('U');else st.push('D');break;}}}while (!st.empty()) {if (st.top() == 'U') {if (f2)printf("D\n");else printf("U\n");}else if (st.top() == 'D') {if (f2)printf("U\n");else printf("D\n");}else if (st.top() == 'L') {if (f1)printf("R\n");else printf("L\n");}else {if (f1)printf("L\n");else printf("R\n");}cin >> a >> b;st.pop();}}
阅读全文
0 0
- Codeforces 811D Vladik and Favorite Game[模拟][bfs]
- codeforces 811D——Vladik and Favorite Game(bfs)
- Codeforces 811D Vladik and Favorite Game【思维+Bfs】
- Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game【交互题+BFS】
- Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game 交互, BFS
- Vladik and Favorite Game CodeForces
- Codeforces 676D Theseus and labyrinth 模拟+bfs
- Codeforces 676D Theseus and labyrinth【模拟+Bfs】
- Codeforces 676D Theseus and labyrinth 模拟+bfs
- Codefoces-811A Vladik and Courtesy [模拟]
- codeforces 811C Vladik and Memorable Trip
- Codeforces 811A Vladik and Courtesy
- Codeforces 811B Vladik and Complicated Book
- codeforces 811A Vladik and Courtesy
- Codeforces 811 A Vladik and Courtesy
- Codeforces 811 B Vladik and Complicated Book
- CodeForces 811A ——Vladik and Courtesy——暴力,模拟
- CodeForces 811B——Vladik and Complicated Book ——思维,模拟
- 我将进化成一条狗(10)——长寿秘诀
- Math,Date.Numbr
- 练习36
- 不相关性和独立性
- 51Nod-1082 与7无关的数【进制+打表】
- Codeforces 811D Vladik and Favorite Game[模拟][bfs]
- 练习37
- CSU
- 关于ionic 降级版本操作
- 练习38
- java web 创建举例
- Android 自定义View (二) 进阶
- 我将进化成一条狗(11)——工业机器人
- 负熵作为非高斯的度量